in reply to ead a file which has three columns and store the content in a hash

You already split into three values, but you then throw the third one away. Don't do that:
my ($first, $second, $third) = split ' ', $line;

If you don't want to populate a variable that's never used, either replace it with undef in the assignment:

my ($first, undef, $third) = split ' ', $line;

or extract only the columns you're interested in using a list slice subscript:

my ($first, $third) = (split ' ', $line)[0, 2];

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Split a column
by shabird (Sexton) on Apr 13, 2020 at 16:35 UTC

    i did that and it works but the output is irregular, i want the data to be in order but it is irregular like this. output:

    up up NA up down NA NA Regulation up down down up down up down up up up

    I want the regulation first and the data in order as they are in column, how can i do that?

      Hashes are (by definition) unordered so when you call values you're getting the values in a (pseudo)random order. If you want them back in the order of the keys you either need to store the keys off into an array as they come in, or use keys on your hash (which will return items in the same order as the corresponding values; alternately sort the keys and iterate over that to pull out and print the corresponding value).

      Update: Just to expand merging the suggestion above roughly something like this.

      my %hash; my @key_order; while( defined( my $line = <> ) ) { my ($first, $third) = (split ' ', $line)[0, 2]; $hash{ $first } = $third; push @key_order, $first; } ## In the order they appeared . . . for my $key (@key_order) { say $hash{ $key }; } ## Or in their random ordering but with the match . . . for my $key ( keys %hash ) { say qq{$key => $hash{ $key }}; }

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      Hashes in Perl are unordered. You can use Hash::Ordered instead. See also the module's documentation for other options and their comparison.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Now i am counting the values in the third column i.e how many up words how many down words and how many NA. For that i wrote the following program:

        @values = values(%hash); for $element (@values){ if ($element =~ /up/){ $sumA++; }if(($element =~ /down/)){ $sumB++ }if(($element =~ /NA/)){ $sumC++ } } print "Number of up is: $sumA\n"; print "Number of down is: $sumB\n"; print "Number of NA is: $sumC\n";
        it is doing a great job but when i add a same row in the last of the file it doesn't count its up, down or NA word. why is that?