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


Dear Monks,

I have a list of elements (A, B, C, ...). They have a value :1, 2, 5 . These elements A, B, C can appear more than one time in the list.

I would like to supress the cases that appear more than one time in my list, and for those that appear more than one time, I would like that the values that appear in the left column are added

Here is the start file ("delay_${day}.csv")
1 A 0 B 3 C 0 A 2 C 5 A 3 E 1 E

Here is the file I would like to have in the end ("unique_delay_${day}.csv")
6 A 0 B 5 C 4 E

I have made a code, but it does not seem to work. Could you tell me where did I go wrong ?
#!/usr/bin/perl use strict; use warnings; ################################## ## ARGUMENTS OF THE SCRIPT ################################## # the argument due to be used is # the ALL_FT file ##################################### # do not forget the $ before the ARGV ##################################### # the format of the ALL_FT file has to be : $date.ALL_FT (ex: 20060904 +.ALL_FT) my $ALL_FT_file = "$ARGV[0]"; ################# END OF THE DECLARATION OF THE ARGUMENTS my $Current_Dir = `pwd`; # print STDOUT "the current directory is $Current_Dir"; # name of the OUTFILE # the day appears in the name of the OUTFILE my $day = $ALL_FT_file; # the following command is used to skip the .ALL_FT in the name of the + ALL_FT_file # and thus to have the date $day =~ s/.ALL_FT//; # open the ALL_FT file open(INFILE,"<delay_${day}.csv") or die "Can't open : delay_${day}.csv +: $!"; # to open the file # OUTFILE is the name of the HANDLE in this case open (OUTFILE, ">unique_delay_${day}.csv") ; # creation of a table my @New_Table; while (<INFILE>) { # the lines are composed of elements separated by a point comma my $Line = $_; chomp($Line); my @Elements = split(";", $Line); push(@New_Table, $Elements[2]); } print @New_Table; my %Hash = map { $_, 1 } @New_Table; my @unique = keys %Hash; close INFILE; close OUTFILE;

Replies are listed 'Best First'.
Re: suppress the elements that appear more than one time in a list
by FunkyMonk (Bishop) on Aug 01, 2007 at 12:12 UTC

    Build your hash inside your while loop, and get rid of @New_Table:

    my %Hash; while (<INFILE>) { # the lines are composed of elements separated by a point comma <- +- it's a called a semicolon! chomp; my ( $k, $v )= split /;/; $Hash{$k} += $v; } my @unique = keys %Hash;


      Thanks a lot FunkyMonk

      I have tried your method.

      However, I did not understand the line :$Hash{$k} += $v;
      I replaced with line :$Hash{$v} = $k;
      because I would like o have a unique list of the letters (not the numbers)
        It would perhaps be better from a maintenance point of view to leave the line

        $Hash{$k} += $v;

        as it is and change the split to reflect the fact that your value comes before the key on the line

        my ($v, $k) = split /;/;

        You still need to use += since you wish to accumulate the values.

        Cheers,

        JohnGG