"Dr.Ruud" <[EMAIL PROTECTED]> wrote:    oryann9 schreef:
> "Dr.Ruud" 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($_ = )) {
$_{$_} ||= print($_);
}


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


Let me simplify the code, to this equivalent:

while ( )
{
$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 ( )
{
next if $d{$_};
print;
$d{$_} = 1;
}

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

-- 
Affijn, Ruud

************************
   
  cool!  thanks, now I understand.


 
---------------------------------
Want to start your own business? Learn how on Yahoo! Small Business.

Reply via email to