in reply to Re^2: Creating a Hash using only one column in an imported data file
in thread Creating a Hash using only one column in an imported data file

Hi Koda1234,

The preferred (because it's safest ) way to open a file is the "three-argument form" (see open [suggestion: as a beginner, read the docs for the various functions; don't just copy examples you may see in the wild ]).

Also:

use strict; use warnings; use feature 'say'; my $filename = $ARGV[0] or die "You must supply a filename"; -f $filename or die "You must supply the name of a file that exists!"; open my $IN, '<', $filename or die "Can't open < $filename: $!"; my @col9; while ( my $line = <$IN> ) { chomp $line; push @col9, (split / /, $line)[8]; } close $IN or die "Can't close $filename: $!"; foreach my $test ( 1, 9, 42, 666 ) { my $count = scalar grep { $_ >= $test } @col9; say sprintf "%d values were >= %d", $count, $test; } __END__

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^4: Creating a Hash using only one column in an imported data file
by Koda1234 (Initiate) on Feb 13, 2017 at 22:25 UTC

    Thank you again. I will have to get used to manipulating data sets. Also, when trying to run your code, I am getting a syntax error around say sprintf, although it it looks identical to the one before.

      I suppose you are copy/pasting and forgot use feature 'say'; ? Is that what the error tells you, or does it tell you something else?


      The way forward always starts with a minimal test.

        Oh yes, part of the use feature say wasn't properly stated. Now that the feature is working, the values i'm getting are very different, and much larger, compared to when I had pasted the file into the script. Does this have something to do with the push command?