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

`cat file` is maybe slower, maybe not good style, but gives you list of lines from without having to bohter about open, close and so on. But if you need such precise control -- let somebody else do it -- that is Path::Tiny.

#!/usr/bin/perl -CSDA use utf8; use Modern::Perl; no warnings qw{uninitialized}; use Data::Dumper; use Path::Tiny; my %myhash = map { ($$_[0], $$_[2]) } map { [split /\s+/, $_] } `cat myfile`; # maybe slower, but don't care +about open # path("myfile")->lines_utf8; # very reliable print Dumper(\%myhash); result: $VAR1 = { 'AC067940.1' => 'up', 'CCT5P2' => 'up', 'GeneName' => 'Regulation', 'C20orf204' => 'up', 'NANOS3' => 'up', 'MIR200A' => 'down', 'SREK1IP1P1' => 'down', 'AC091607.1' => 'up', 'CYP2C8' => 'down', 'AL662791.1' => 'up', 'FAR1P1' => 'NA', 'MIR200B' => 'down', 'CHTF8P1' => 'NA', 'RPL19P20' => 'up', 'MIR429' => 'up', 'APOL4' => 'up', 'CFL1P4' => 'down', 'NAALADL2' => 'NA' };

counting:

#!/usr/bin/perl -CSDA use utf8; use Modern::Perl; no warnings qw{uninitialized}; use Data::Dumper; use Path::Tiny; my %count; $count{$_}++ for map { $$_[2] } map { [split /\s+/, $_] } path("myfile")->lines_utf8; print Dumper(\%count); $VAR1 = { 'down' => 5, 'NA' => 3, 'up' => 9, 'Regulation' => 1 };

Replies are listed 'Best First'.
Re^2: ead a file which has three columns and store the content in a hash
by haukex (Archbishop) on Apr 14, 2020 at 18:28 UTC
    `cat file` is maybe slower, maybe not good style, but gives you list of lines from without having to bohter about open, close and so on.

    It also doesn't catch any errors that might happen, may have issues regarding encoding, and isn't portable. And with variables interpolated into the backticks, it becomes a security risk. So yes, it's not good style. Path::Tiny is definitely the better solution.