Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Extracting string from a file

by ww (Bishop)
on Nov 11, 2013 at 13:50 UTC ( #1061990=note: print w/ replies, xml ) Need Help??


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

Not to quarrel because your explanation of the regex problems is exemplary, but OP is clearly dealing with a multi-line logfile, in which some lines begin with ~|TOTAL. Hence, an array better matches the SOPW spec than the string is your "Test Case."

Aside to Bindo: Your spec comes up a little short of perfection because (very strictly speaking and very nitpicky) there's no requirement -- merely a single illustration -- that what's captured be numeric followed by a percent sign. What if the notation were hex, binary or some sort of non-Arabic numbers? In any case, I've treated you spec as "any line that begins with tilde, pipe, 'TOTAL' followed by anything" which is the only reason my regex differs from RMGir's:

#!/usr/bin/perl use 5.016; use warnings; #1061986 my @logfile = ("~|TOTAL 24.1% 0.4%", "~|not a total 11%", "~|TOTAL 21.0% 0.7%", "FOOBAR", "~|TOTAL 13.7% 10.2%", "~|TOTAL last5 6", ); my @FIGURE; for my $logentry(@logfile) { if ($logentry =~ /~\|(TOTAL.*)/ ) { push @FIGURE, $1; } else { say "\t \$logentry, $logentry, does not match pattern"; } } for (@FIGURE) { say $_; } =head execution: C:\>1061986.pl $logentry, ~|not a total 11%, does not match pattern $logentry, FOOBAR, does not match pattern TOTAL 24.1% 0.4% TOTAL 21.0% 0.7% TOTAL 13.7% 10.2% TOTAL last5 6 =cut


Comment on Re^2: Extracting string from a file
Select or Download Code
Re^3: Extracting string from a file
by Bindo (Acolyte) on Nov 18, 2013 at 10:26 UTC

    Thank you very much for all the good advices gentlemen. Guess I owe you all a big apology since I have failed to reply. I was in the hospital due to a small accident and only last night I have been discharged. Now back at feet :)

    I tried the following code but the program wont give any output nor any errors. Please can one of you correct this code for me? Please gentlemen Im a beginner who is trying to understand the whole concept of regexes more specifically with files, So be gentle.

    my $SYS_HOME = $ENV{'SYSTEM_HOME'}; my $GD_FILE = $SYS_HOME."/GD.log"; my $FH; my @DUMP; open ($FH, '<', $GD_FILE) || die "Cant open : $!"; while (my $line = $FH) { if ($line =~ /~\|(TOTAL.*)/){ #my $tmp = $1; push @DUMP, $1; foreach (@DUMP) { print "$_\n"; } } }

    Many thanks in advance! /Bindo

Re^3: Extracting string from a file
by Bindo (Acolyte) on Nov 20, 2013 at 05:15 UTC

    Sir can you please go through my latest reply at the end of the thread and advice? For some reason no one is replying. thanks.

      The "reason(s)" you're getting no replies may include:

      1. The tread has grown elderly (and deeper than the max depth to which some Monks read).
      2. The latest code you've posted shows little evidence that you've worked to understand and implement prior replies.
      3. The form of your question amounts to a 'gimme', AKA, a 'do it for me' which is not an approach approved here.

      Still, in the spirit of 'help, but don't do the (whole) job':

      • Your use of while is not doing what you want. See perldoc -f while (which refers you to a specific section of perlsyn, a document available on your own computer) and compare to the docs re for.
        open (my $FH, '<', "bindo1061990A.txt") || die "Cannot open bindo10619 +90A,txt: $!"; my @lines = <$FH>; for my $line (@lines) { say $line; }
      • Your foreach (@DUMP) { at Ln 10 should NOT be inside the loops beginning at Lns 6 and 7 as that will produce repetitive output for each (new) match.

      I hope this helps... and also clarfies that the saying "Heaven helps those who help themselves" can be paraphrased to apply here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-08-02 01:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls