If we're just going for confusing concise one liners then I would use

perl -ne '$$_||=print'

You save three whole characters by using the symbol table instead of a hash :)

On 1/11/07, Dr.Ruud <[EMAIL PROTECTED]> wrote:
oryann9 schreef:
> "Dr.Ruud" <[EMAIL PROTECTED]> wrote:   beast schreef:
>> beast:

>>> a 100
>>> a 102
>>> c 100
>>> a 102
>>> b 111
>>> c 100
>>> c 102
>>> c 100
>>> c 100
>>> a 102
>>> ...
>>>
>>> I would like to have a list (either array or hash) with unique line
.
>>
>> perl -ne'$_{$_}||=print' datafile
>
> what are these exactly doing in plain english?
>  1st line is not printing and second it is, but gets confusing at ||=
in the 1st line and at !$ in 2nd line

First see the output of

  perl -MO=Deparse -ne'$_{$_}||=print' datafile

which returns:

  LINE: while (defined($_ = <ARGV>)) {
    $_{$_} ||= print($_);
  }


See `perldoc perlrun` for the meaning of the -n option.


Let me simplify the code, to this equivalent:

  while ( <ARGV> )
  {
    $d{$_} ||= print($_);
  }

For every unique line of the input (the datafile) an entry to the
hash-table %d is added, with the whole line as the key-value. The
belonging value is set to the return value of print, which is 1 (if the
print went OK).

But if the key already exists, this is all skipped, because of the ||=
operator, see `perldoc perlop`. This operator sets the lvalue to the
rvalue, but only if the lvalue isn't true. So the loop is similar to

  while ( <ARGV> )
  {
    next if $d{$_};
    print;
    $d{$_} = 1;
  }

And that was my explanation of perl -ne'$_{$_}||=print'.

--
Affijn, Ruud

"Gewoon is een tijger."


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to