http://qs321.pair.com?node_id=854237

changma_ha has asked for the wisdom of the Perl Monks concerning the following question:

Hi all i am trying to solve a problem for a long time. where i have data table in a text file like

Apple Grape 100 Ginger Fry 200 Apple Grape 80 Ginger Banana 800 Ginger Fry 150 Ginger Banana 45

I want my output to be like this, where the duplicate data values are added

Apple Grape 180 Ginger Fry 350 Ginger Banana 845

I am trying to do it using array of arrays but not able to proceed much.here is my code

#! /usr/bin/perl use strict; use warnings; use Data::Dumper; my @AoA; open (my $fh ,"<","C:/excl7.txt") or die "Can't open the file"; while (<$fh>){ #chomp ($line); push @AoA,[split]; } foreach my $i (@AoA){ #** how do i compare the array data?... if (@{$i print "@$i\n"; } #my @new =shift (@AoA); #print $AoA[1][0]; #print Dumper \@AoA;
Is there other way which will be easier to do? plz help..thaanks in advance <

Replies are listed 'Best First'.
Re: Adding the duplicate data using arrays
by Ratazong (Monsignor) on Aug 11, 2010 at 07:31 UTC

    Why do you want to use an array? The natural thing when you hear duplicate or unique is to think of hashes.

    With that, use the following approach:

    • loop through the file line-by line
      • split the line into fruit-name and number (using a regex)
      • use the fruit-name as key for a hash and add the number (e.g. $fruits{$fruit_name} += $number;)
    • print the hash

    That's all. Not too hard, isn't it?! ;-)

    Rata
Re: Adding the duplicate data using arrays
by biohisham (Priest) on Aug 11, 2010 at 07:49 UTC
    Just as Rata mentioned, a hash is more logical a solution in this case, a very simple hash approach can perform the job quite efficiently...

    how do i compare the array data?...
    Using a hash you don't have to compare data because all you need is just a hash key to hold all the data that belong to that type of fruit...

    Consider
    use strict; use warnings; use Data::Dumper; my %hash; while(<DATA>){ my ($key, $number)=split; $hash{$key} +=$number; } print Dumper (\%hash); __DATA__ apples 10 oranges 3 apples 3 oranges 7


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Adding the duplicate data using arrays
by wwe (Friar) on Aug 11, 2010 at 07:46 UTC
    This should work as expected. I used hash of hashes, as it looks simpler for me:
    #! /usr/bin/perl use strict; use warnings; use Data::Dumper; my %HoH; while (<DATA>) { chomp $_; #print "<$_>\n"; my ($lev1, $lev2, $amount) = (split /\s+/, $_); #print "<$lev1>, <$lev2>, <$amount>, \n"; if (exists $HoH{$lev1}{$lev2}) { $HoH{$lev1}{$lev2} = $HoH{$lev1}{$lev2} + $amount; } else { $HoH{$lev1}{$lev2} = $amount; } } #print Dumper(\%HoH); while (my ($lev1, $inter) = each %HoH) { while (my ($lev2, $amount) = each %$inter) { printf ("%-10s%-10s%-5d\n", $lev1,$lev2,$amount); } } __DATA__ Apple Grape 100 Ginger Fry 200 Apple Grape 80 Ginger Banana 800 Ginger Fry 150 Ginger Banana 45
    prints on my system:
    Ginger Fry 350 Ginger Banana 845 Apple Grape 180
    maybe you want to change formatting.