Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Split a column

by shabird (Sexton)
on Apr 13, 2020 at 16:35 UTC ( [id://11115470]=note: print w/replies, xml ) Need Help??


in reply to Re: Split a column
in thread ead a file which has three columns and store the content in a hash

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?

Replies are listed 'Best First'.
Re^3: Split a column
by Fletch (Bishop) on Apr 13, 2020 at 16:51 UTC

    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.

Re^3: Split a column
by choroba (Cardinal) on Apr 13, 2020 at 17:08 UTC
    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?

        Presuming you mean that you're adding a row which duplicates the key and value from another row you're not going to change your hash. If you've seen a key "foo" with a value "up", setting $hash{ "foo" } = "up" again doesn't really change anything in %hash. Hashes are unordered and maintain a single value for each key slot. If you need the count of rows with a particular value then count them as you read things.

        my %counts_per_value; [... inside your while loop ...] $hash{ $first } = $third; $counts_per_value{ $third }++; [...] for my $value ( sort keys %counts_per_value ) { say qq{$value => $counts_per_value{ $value }; }

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

        Because the hash contains each key just once.
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        You seem to misunderstand or lack understanding of the behavior of hashes, or associative arrays to use their formal name. Please see the discussions of keys, values and each in the Perl documentation.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found