in reply to map to hash of arrays

Is it what you want?

use Data::Dumper; my $data = <<EOT; Row 1 label1 : item1 label2 : item1 Row 2 label1 : item2 label2 : item2 EOT push @{$hash{$1}}, $2 while $data =~ /(\S+)\s*:\s*(\S+)/g; print Dumper(\%hash);

$VAR1 = { 'label1' => [ 'item1', 'item2' ], 'label2' => [ 'item1', 'item2' ] };

Replies are listed 'Best First'.
Re^2: map to hash of arrays
by emilford (Friar) on Jul 26, 2004 at 14:12 UTC
    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' ] }

      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?