Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Help understand why this grep does not work

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


in reply to Re^4: Help understand why this grep does not work
in thread Help understand why this grep does not work

Yes, this is why I didn't do that in the first place. Although with the data I have listed I think this would work, if I had to expand it and there weren't unique values, you're right - this wouldn't work so well. I think your 'or-ed regex' is indeed better.

For the sake of argument (I'm just playing around trying to get better a handling data in Perl and the map function), what's the best way to create a hash of arrays with this data? I can't seem to create the data structure with a map. I know this won't work as it overwrites the values on each pass:

my %data_hash = map { /^(\w\s+\d)/ => [$_] } @data2;

But, I can't seem to figure out how to create a hash of arrays with the map function. Maybe it's not possible or not the best way?


Comment on Re^5: Help understand why this grep does not work
Download Code
Re^6: Help understand why this grep does not work
by LanX (Canon) on Nov 02, 2013 at 16:59 UTC
    OK, these two three possibilities seem to do it, but by far not as performant as the or-ed regex.

    DB<126> %hash = map { my ($m)=/^(\w\s+\d)/; $m => [ grep {/^$m/} @da +ta2 ] } @data1 => ("a 1", [], "a 2", ["a 2 Y"], "a 3", ["a 3 R"]) DB<127> %hash = map { my $k=$_ ; $k => [ grep {/^$k/} @data2 ] } map + { /^(\w\s+\d)/} @data1 => ("a 1", [], "a 2", ["a 2 Y"], "a 3", ["a 3 R"]) DB<128> %hash = map { /^(\w\s+\d)/; $1 => [ grep {/^$1/} @data2 ] } +@data1 => ("a 1", [], "a 2", ["a 2 Y"], "a 3", ["a 3 R"])

    update

    see also Generating Hash-of-List using map?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Very informative; thank you! This definitely starts to drift into the 'difficult to read and maintain' territory, though. You've been a big help giving me some insight on how to play around with 'map' a little more, though. Thanks again!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-07-11 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls