Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: array processing

by Anonymous Monk
on Dec 06, 2005 at 11:19 UTC ( [id://514406]=note: print w/replies, xml ) Need Help??


in reply to Re: array processing
in thread array processing

I obviously have a lot to learn. Out of interest could this have been done with an array instead of a hash, or would it have been too messy ?

Thanks for your help

Replies are listed 'Best First'.
Re^3: array processing
by tirwhan (Abbot) on Dec 06, 2005 at 11:34 UTC

    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
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://514406]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found