Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Extracting string from a file

by ww (Archbishop)
on Nov 11, 2013 at 21:28 UTC ( [id://1062056]=note: print w/replies, xml ) Need Help??


in reply to Re: Extracting string from a file
in thread Extracting string from a file

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-20 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found