Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Help understand why this grep does not work

by drmrgd (Beadle)
on Nov 02, 2013 at 15:47 UTC ( #1060947=note: print w/ replies, xml ) Need Help??


in reply to Help understand why this grep does not work

Oh my! What a dumb mistake! Thank you both for pointing that out. I can't believe I missed something so obvious!


Comment on Re: Help understand why this grep does not work
Re^2: Help understand why this grep does not work
by LanX (Canon) on Nov 02, 2013 at 15:54 UTC
    if speed matters you should better consider something like building a hash for array2 for simple lookups or at least a complex OR-ed regex looping just once over array2.

    edit
    DB<102> @data1 = ( "a 1 a", "a 2 T", "a 3 C" ); => ("a 1 a", "a 2 T", "a 3 C") DB<103> @data2 = ( "a 2 Y", "a 3 R", "a 4 Q", "b 5 R" ); => ("a 2 Y", "a 3 R", "a 4 Q", "b 5 R") DB<104> $regex = join "|" , map {/^(\w\s+\d)/} @data1 => "a 1|a 2|a 3" DB<105> grep { /^($regex)/ } @data2 => ("a 2 Y", "a 3 R")

    think I spotted (and corrected) a bug in your logic, you forgot to anchor to string start '^' in your grep regex.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I wondered about building a hash for this. What would be the best way? Are you thinking something like this is better:
      my %data_hash = map { /(\w\s+\d)/ => $_ } @data2; for my $key ( %data_hash ) { print "$data_hash{$key}\n" if grep { /^$key/ } @data1; }

      I also wondered about anchoring that match. In earlier versions, I did anchor it. But, I wasn't sure if it was necessary given the data set. I think I'll add it back as it's probably safer.

      Thanks for the advice!

        well kind of ... but your code only works if the first 2 chars combinations are unique otherwise you need to push into a HoA.

        I think the or-ed regex I've shown you is better, as long as @data1 is known from the beginning and doesn't change dynamically.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (19)
As of 2014-12-18 14:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (56 votes), past polls