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

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

Replies are listed 'Best First'.
Re^5: merge with multiple row with same key
by Laurent_R (Canon) on Nov 27, 2014 at 07:12 UTC
    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.
Re^5: merge with multiple row with same key
by davido (Cardinal) on Nov 27, 2014 at 07:18 UTC

    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