in reply to Extracting string from a file

One more remark.
#!/usr/bin/perl use strict; use warnings; my @dump; my $false_count = 0; open (FH, "<file_name") || die "Cant open : $!"; while (<FH>){ if (/^.\|TOTAL.*$/i){ my @tmp = $_ =~ /([0-9\.\-]+)/g; push @dump, "@tmp"; }else{ $false_count++; } } print 'Counted matches-> '.$#dump."\tUnmatched lines-> ".$false_count. +"\n"; foreach (@dump){print $_."\n";}

Replies are listed 'Best First'.
Re^2: Extracting string from a file
by ww (Archbishop) on Nov 11, 2013 at 21:28 UTC

    builat: Close but 'no cigar.' No downvote, but pls test your code before posting and thereby implying that it constitutes a correct answer.

    Sorry, but numerous minor problems, including unnecessary complication of your code and (not exactly minor) your use -- in your Ln 6, open (FH, "<file_name")... -- of data not shown or referenced in your post. I realize it may be the same as OP's, or mine, but if you don't show it or otherwise make it unambiguous, future readers can't be sure.

    Then there's an actual code problem: $#array does NOT count the elements in the array; returns the last element's index. Since array indices start with 0, $#array is 1 less than the count of elements (or count of indices, if you prefer to think of it that way).

    #!/usr/bin/perl use 5.016; use warnings; # 1062018 builat in same thread as #1061986 my @dump; my $false_count = 0; while (<DATA>){ chomp ($_); if ($_ =~ /~\|(TOTAL.*)/ ) { my $tmp = $1; push @dump, $tmp; } else { say "False: |\"$_\"| does not match pattern"; $false_count++; } } say "\n\t DEBUG \$#dump: $#dump"; say "\t NB: last index of the array and thus 1 less than the count of +array elements!\n"; say 'Counted matches-> '. ($#dump + 1) . "\tUnmatched lines-> " . $fal +se_count; for (@dump){ say $_."\n"; } =head execution C:\> 1062018.pl False: |"~|first"| does not match pattern False: |"~|not a total 11%, "| does not match pattern False: |"FOOBAR, "| does not match pattern DEBUG $#dump: 3 NB: last index of the array and thus 1 less than the count of + array elements! Counted matches-> 4 Unmatched lines-> 3 TOTAL 24.1% 0.4%, TOTAL 21.0% 0.7%, TOTAL 13.7% 10.2%, TOTAL last5 6 =cut __DATA__ ~|first ~|TOTAL 24.1% 0.4%, ~|not a total 11%, ~|TOTAL 21.0% 0.7%, FOOBAR, ~|TOTAL 13.7% 10.2%, ~|TOTAL last5 6
Re^2: Extracting string from a file
by CountZero (Bishop) on Nov 13, 2013 at 10:40 UTC
    I have a problem with your regex /^.\|TOTAL.*$/i, more specifically with the .*$ part of it.

    That part actually says "anything (even nothing) until the end of the string" and is therefore superfluous.

    Worse is that /^.\|TOTAL.*$/i will allow to pass a line without any digits in it and therefore will push nothing on the @dump array but neither is the $false_count variable incremented. Of course it is very well possible that the file will always have digits on its "TOTAL" lines, but IMHO that is a dangerous assumption to make.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      You are absolutely right. And yes I really started from the premise that any string beginning with ~ | TOTAL further contains a set of numbers. It would be better to add a check for the presence of numbers on the right side of expression. Thank you.