in reply to Re^2: array processing
in thread array processing

Yes, it would be possible, but wasteful.

use List::MoreUtils qw(uniq); my @client_list; while (my $line = <DATA>) { chomp $line; next unless ($line =~ /Login succeeded/); push (@client_list,(split(' ',$line))[6]); } @client_list = uniq(@client_list); print join("\n",@client_list);

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Replies are listed 'Best First'.
Re^4: array processing
by inman (Curate) on Dec 06, 2005 at 11:47 UTC
    You can even do the uniqe test using grep as you add items. This is also a wasteful approach but maybe easier on memory usage if the log file that you are processing is large.
    if ($line =~ /Login succeeded. User: (\w+)/i) { my $user = $1; push @users, $user unless grep (/$user/i, @users); }
    Using a hash is the best plan.

      Well, that's a lot more wasteful IMO, because you're grepping the entire array every time you find a login line. You're right that the resulting array never grows to the size it does in my last post, but unless you only have a handful of user who log in extremely often (like tens of thousands of times) I would say this is a bad idea.


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan