Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: map to hash of arrays

by emilford (Friar)
on Jul 26, 2004 at 14:12 UTC ( [id://377420]=note: print w/replies, xml ) Need Help??


in reply to Re: map to hash of arrays
in thread map to hash of arrays

Not exactly. Let me try my explanation again. Here is a chunk of sample data to illustrate what I'd like to do.
Row 17 object_label : yang object_id : 123456 Row 18 object_label : yang object_id : 789101112 Row 19 object_label : yang object_id : 13141516171819
The data comes in exactly like that, line by line. I need to ignore the Row \d+ lines and grab the rest of the information, so that I end up with a hash of arrays where 'object_label' are used as keys and 'object_id' with the same 'object_label' are items in an array.
%hash = { 'yang' => [ '123456', '789101112', '13141516171819' ] }

Replies are listed 'Best First'.
Re^3: map to hash of arrays
by ccn (Vicar) on Jul 26, 2004 at 14:26 UTC

    please try this: push @{$hash{$1}}, $2 while $data =~ /object_label\s+:\s+(\S+)[^:]+:\s+(\S+)/g;

      That worked perfectly, thanks. So is the regex treating the data read in as a single string? There are line breaks, which I thought would require the /m modifier to get away with something like this. I understand the [^:]+ part that continues matching on anything that is not a :, but I thought the regex would end at the first line break?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found