in reply to script assistance please

Some more less or more useful little hints:

Useful pragmas:

use strict; use warnings; use feature qw(say); use Data::Dump;

Split (if you really need it):

my $moyr = "12/2014"; my ( $month, $year ) = split /\//, $moyr; say for ( $month, $year );

Building the hash:

my %csga; while (<$in>) { my @record = split /\t/; chomp @record; $csga{ $record[0] } = $record[1]; } close $in; dd \%csga;

Skipping headers when reading a file:

open( my $in, "<", q(cc.txt) ) || die $!; while (<$in>) { next if $. <= 8; # print; } close $in;

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^2: script assistance please
by Laurent_R (Canon) on Feb 20, 2015 at 20:03 UTC
    Hmm, for skipping a header, I usually prefer to do it before entering the main loop. For example:
    open( my $in, "<", q(cc.txt) ) || die $!; $useless_header_line = <$in> for 1..8; while (<$in>) { # print; }
    Two reasons for that. It is more self-documenting. If my file has 500 million lines (and, yes, I am commonly dealing with such file sizes), I do not like the idea to make a useless test so many times. The test is pretty fast and has probably relatively limited impact of performance, but, still, it is still against my sense of economy or ecology.

    Je suis Charlie.

      When not using <> that makes sense. Though could also do it when using <> when also using continue

      while (<>) { last unless (/^#/); } procline; while (<>) { procline; } continue { if (eof) { while (<>) { last unless (/^#/); } procline; } }
      "...it is still against my sense of economy or ecology"

      Yes sure. But as i wrote:

      "...less or more useful little hints"

      IMHO it wasn't a bad idea to point the OP to $.

      "...The test is pretty fast and has probably relatively limited impact of performance"

      So let us compare:

      #!/usr/bin/env perl use strict; use warnings; use Benchmark qw ( :hireswallclock cmpthese timethese ); our $file = qq (huge.file); our $amount = 1_000_000; sub karl { our ( $file, $amount ); open( my $in, "<", $file ); while (<$in>) { next if $. <= $amount; 1; } close $in; } sub laurent { our ( $file, $amount ); open( my $in, "<", $file ); my $useless_header_line = <$in> for 1 .. $amount; while (<$in>) { 1; } close $in; } my $results = timethese( 10, { 'karlgoethebier' => 'karl', 'Laurent_R' => 'laurent', } ); cmpthese($results); __END__ karls-mac-mini:monks karl$ perl -e 'print qq(Lorem ipsum kizuaheli\n) +for 1..50_000_000;' > huge.file karl-mac-mini:monks karl$ ls -hl huge.file -rw-r--r-- 1 karl karl 1,0G 21 Feb 17:24 huge.file karls-mac-mini:monks karl$ wc -l huge.file 50000000 huge.file karls-mac-mini:monks karl$ ./1117372.pl Benchmark: timing 10 iterations of Laurent_R, karlgoethebier... Laurent_R: 65.9521 wallclock secs (63.43 usr + 2.34 sys = 65.77 C +PU) @ 0.15/s (n=10) karlgoethebier: 127.837 wallclock secs (124.90 usr + 2.55 sys = 1 +27.45 CPU) @ 0.08/s (n=10) s/iter karlgoethebier Laurent_R karlgoethebier 12.7 -- -48% Laurent_R 6.58 94% --
      "...relatively limited impact of performance"

      This is relative. It is like it is ;-)

      Thanks for your reply and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»