in reply to merge with multiple row with same key

please post real data using Data::Dumper

this pseudo code doesn't make much sense and I can't even spot any difference between before and after.

Hint: curlies denote literal anonymous hashes

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

  • Comment on Re: merge with multiple row with same key

Replies are listed 'Best First'.
Re^2: merge with multiple row with same key
by tcheungcm (Novice) on Nov 27, 2014 at 02:51 UTC
    Here is output using Dumper
    'abc,def' => { ',,,item4' => 1, 'item1,,,' => 1, ',item2,,' => 1 }, 'kln,mno' => { ',,,item4' => 1 'item1,,,' => 1 },

    And the result should look like

    'abc,def' => { 'item1,item2,,item4 }, 'kln.mno' => { 'item1,,,item4 },

      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

        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
      Please have a look at perldsc and stop waisting our time with fake code.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)