At 3:20 pm -0500 30/4/02, Teresa Raymond wrote:
| JD,
| Could you please explain the last 2 lines for me?
| 
| >$_ = q(
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy00
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy01
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy05
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy01
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy00
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy02
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy05
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy01
| >Apr 21 01:03:01 pandora ipop3d[28245]: Login user=holy00
| >);
| >for (split/\n/) {s~(.+user=)(.+)~$2~; $users{$_} = x}
| >for (sort keys %users) {print "$_$/"}

for (split/\n/) {
     s~(.+user=)(.+)~$2~;
     $users{$_} = x
     }
for (sort keys %users) {
     print "$_$/"
     }

Our variable is $_.  This begins as a string containing ten lines or so.
We convert this string into an array by 'splitting' it at each "\n" newline.

We then loop through this array, or list, of lines.  For each line we
discard the contents of the first pair of parentheses and replace the
contents of the line with that part which matches the contents of the
second pair of parentheses

  s  /   (don't want this) (want this) /  $2 = second group  /

[ s~~~ = s/// ]

which is the username, now in $_.

We then create a hash (record or paired list) %users.  At each iteration we
use the remainder of the line (the username -- $_) as a key in this hash --
$users{$_} -- to which we assign the arbitrary value x, which will serve no
further purpose.

If a username crops up more than once, it cannot form a duplicate key and
will simply be assigned the value x again.

We now create an array from the keys, which we sort in the process, and
loop through this array, printing each item (was key) followed by a new
line $/.

Whew!  I'd rather speak perl I think :-)

JD













Reply via email to