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

Re^3: merge with multiple row with same key

by davido (Cardinal)
on Nov 27, 2014 at 03:34 UTC ( [id://1108524]=note: print w/replies, xml ) Need Help??


in reply to Re^2: merge with multiple row with same key
in thread merge with multiple row with same key

That cannot be the output from Data::Dumper, because it lacks commas between some of the elements. When you post imprecise samples and claim that they're the output of something that generates precise, and syntactically correct data structures, we're unable to trust your problem description, much less know how to fill in the missing parts.

Your expected result is also impossible, unless we should be interpreting it as simple text. From a Perl data structure point of view, 'abc,def' => { 'item1,item2,,item4 }, is nonsensical; the curly braces mean "anonymous hash", and the leading single quote isn't balanced by any closing single quote. Given the ambiguity introduced by leaving quotes unbalanced, we also can't determine which of the commas should be interpreted as literal text, or as operators.

Also, since hash elements have no predictable or useful order, it would be helpful to specify how the "item1" and so on elements should be ordered (assuming you really intend for them to be array elements).

With those flaws in the question making it impossible to precisely know what is being asked, it is also impossible to give an answer that is guaranteed to precisely match your needs. But this is an attempt at getting close:

use Data::Dumper; my %orig = ( 'abc,def' => { ',,,item4' => 1, 'item1,,,' => 1, ',item2,,' => 1, }, 'kln,mno' => { ',,,item4' => 1, 'item1,,,' => 1, }, ); my %new; foreach my $top ( keys %orig ) { push @{$new{$top}}, $_ for keys %{$orig{$top}}; } print Dumper \%new;

The output that produces is this:

$VAR1 = { 'abc,def' => [ 'item1,,,', ',item2,,', ',,,item4' ], 'kln,mno' => [ 'item1,,,', ',,,item4' ] };

Dave

Replies are listed 'Best First'.
Re^4: merge with multiple row with same key
by tcheungcm (Novice) on Nov 27, 2014 at 04:05 UTC

    My apologies. Let me give you more details on this. Basically, I have routine to read a file(test.out) where the file format is:

    A,B*,item2,, A,B*item1,,, A,B*,,,item4 C,D*item1,,, C,D*,,item3,
    Then my code is
    sub merge { open(INFILE, "test.put"); my %data; while (<INFILE>) { chomp; my ($key,@items) = split(/\*/); $data{$key}{$_}++ for @items; } close INFILE; print Dumper(\%data); }

    The Dumper shows

    $VAR1 = { 'C,D' => { ',,item3,' => 1, 'item1,,,' => 1 }, 'A,B' => { ',,,item4' => 1, ',item2,,' => 1, 'item1,,,' => 1 } };

    If I want to merge the rows under key (e.g. 'C,D") to reformat and write into file, what should I do then? many thanks

    C,D,item1,,item3, A,B,item1,item2,,item4
      OK, this is a bit clearer, but we still lack information. I think that you should probably reformat the data prior to storing it into the hash.

      The problem is that you don't give any clue as to how to obtain:

      'abc,def' => {item1,item2,,item4 ,}
      I can see that you want your array of items in a specific order and have an empty slot when an item is missing, but this is obviously dummy data, we would need to see real data to understand how to really order the elements ands how to figure out where there is supposed to be an empty slot.

      So is it correct that, as your example appears to show, you wish to drop commas that come before alphanumeric characters, but retain commas that come after? ...and that you also wish for the alphanumeric items to be sorted without counting commas in the sort comparison?


      Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-03-29 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found