Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Comparing two arrays...

by wallisds (Beadle)
on Apr 20, 2011 at 17:36 UTC ( [id://900392]=note: print w/replies, xml ) Need Help??


in reply to Comparing two arrays...

I understand using grep can hinder performance but for your purposes it would work well:

foreach my $service (@ServiceCheckList) { if (grep($service, @ServicesOnMachine)) { print "$service matches\n"; } }

Oops! I didn't spend enough time examining the output. Lame! thanks for pointing it out. This would be better:

foreach my $service (@ServiceCheckList) { if (grep(/^$service$/, @ServicesOnMachine)) { print "$service matches\n"; } }

I'm learning a lot on PerlMonks! Thanks!!!
Dawn

Replies are listed 'Best First'.
Re^2: Comparing two arrays...
by LanX (Saint) on Apr 20, 2011 at 21:24 UTC
    did you test your code?

    I think you mean grep /^$service$/, @ServicesOnMachine or grep {$service eq $_} @ServicesOnMachine

    Cheers Rolf

      I did test it (see code below). It seemed to work for me but I see from some of the comments that it can be used in a much better way.

      #!/usr/bin/perl -w use strict; my @ServicesOnMachine = qw(NetworkManager acpid anacron atd auditd aut +ofs avahi-daemon avahi-dnsconfd bluetooth capi conman cpuspeed crond +cups dnsmasq dund firstboot gpm haldaemon hidd hplip httpd ip6tables +iptables irda irqbalance iscsi iscsid isdn kudzu lvm2-monitor mcstran +s mdmonitor mdmpd messagebus multipathd netconsole netfs netplugd net +work nfs nfslock nscd ntpd oddjobd pand pcscd portmap psacct rawdevic +es rdisc readahead_early readahead_later restorecond rpcgssd rpcidmap +d rpcsvcgssd saslauthd sendmail smartd sshd svnserve syslog tcsd vbox +add vboxadd-service vboxadd-x11 vncserver wdaemon winbind wpa_supplic +ant xfs ypbind yum-updatesd); my @ServiceCheckList = ("spray", "echo", "daytime", "discard", "charge +n", "ttdb", "ypbind", "ypserv", "yppasswdd", "ypxfrd", "tooltalk", "r +statd", "comsat", "talk", "uucp", "finger", "netstat", "systat", "rus +ers", "walld", "rexd", "rje", "netbios", "lpd", "http", "gopher", "tc +pmux", "news", "nntp", "snmp", "mail", "smtp", "pop2", "pop3", "sendm +ail", "httpd");
      foreach my $service (@ServiceCheckList) { if (grep($service, @ServicesOnMachine)) { print "$service matches\n"; } }

      Correction

      foreach my $service (@ServiceCheckList) { if (grep(/^$service$/, @ServicesOnMachine)) { print "$service matches\n"; } }

      Thanks!

        your misinterpreting this test, all $services will seemingly "match", just try my @ServicesOnMachine=("nonsense").

        The first argument to grep is a boolean condition, your putting a string there which evaluates to true.

        Cheers Rolf

Re^2: Comparing two arrays...
by Marshall (Canon) on Apr 20, 2011 at 22:20 UTC
    Grep is not the performance issue here. Perl grep is actually way cool and super fast.

    The performance issue here appears to me to be that you are iterating in an outer loop over each thing in @ServiceCheckList AND THEN iterating again over every element in @ServicesOnMachine for every thing in @ServiceCheckList in an inner loop. So this takes CheckList * OnMachine iterations.

    My code takes one pass through each item in CheckList and OnMachine to build the hash. Then one more time through essentially both to generate the intersection. That is (CheckList+OnMachine)*2 operations, less than (CheckList * OnMachine) operations.

    On another point, I personally prefer the grep BLOCK LIST syntax - for me, it is easier to read and understand.
    @output = grep{ TRUE OR FALSE }@input;

    If what is inside the grep BLOCK evaluates to "TRUE", pass the input to the output on the left. What is inside the grep BLOCK can be arbitrarily complex, but it all comes down to essentially "true" or "false". Perl grep could have been called "filter" because a subset of @input appears on the @output. A regex can be within the BLOCK, @output = grep{/^abc/}@input passes things that begin with "abc" from the input to the output because the regex evaluates to "true" in that case.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-18 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found