in reply to Re: Fetch data between markers
in thread Fetch data between markers

For those wondering why this works.....

From the Perl Cookbook:

The range operators, .. and ..., are probably the least understood of Perl's myriad operators. They were designed to allow easy extraction of ranges of lines without forcing the programmer to retain explicit state information. Used in scalar context, such as in the test of if and while statements, these operators return a true or false value that's partially dependent on what they last returned. The expression left_operand .. right_operand returns false until left_operand is true, but once that test has been met, it stops evaluating left_operand and keeps returning true until right_operand becomes true, after which it restarts the cycle. Put another way, the first operand turns on the construct as soon as it returns a true value, whereas the second one turns it off as soon as it returns true.

TheStudent

Replies are listed 'Best First'.
Re^3: Fetch data between markers
by penantes (Acolyte) on Jun 07, 2005 at 14:42 UTC
    Hi there
    I read carefully everybodys opinions and found the .. operator to be the simplest way to do it. Altough, I think I didn't express myself very well since your code (in general) is very complex on the regex side altough its valid it is not necessary for the simple thing I needed to to wich I paste bellow.

    I abandoned the @array solutions because Im linking 1,2 - 1,3 giga files :)


    Nevertheless I deeply thank you all for your fast replies and for sharing with me that bit of knowledge.

    ps: I actually found fun in those while in while solutions :) geeee what a geek!

    How it will stay
    #!/usr/bin/perl use warnings; use strict; #It needs ARGS and readable file my $logfile=$ARGV[0]; my $contract=$ARGV[1]; unless (@ARGV == 2) { print "USAGE: $0 \"logfile\" \"numero\"\n"; exit(1); } my $output="/tmp/session_".$contract.".txt"; unless (-e $logfile) { print "O ficheiro $logfile usado para input n\343o existe. Verifique +o nome do mesmo sff.\n"; exit(1); } if (-e $output) { unlink($output); } open(OUTPUT,">>",$output) or die("Could not open conf file."); open(LOG, $logfile) or die("Could not open conf file."); while (<LOG>) { if (/:NOTICE:user=$contract,session=\d+:/ .. /\)\[/) { print OUTPUT $_ } } close(LOG); close(OUTPUT);
    Actually I allready had acomplish that result using a marker with a var and is actually 7 seconds faster than the .. operator, but, I have deprecate it because is better coded with the above script (IMHO).
    open(OUTPUT,">>",$output) or die("Could not open conf file."); open(LOG, $logfile) or die("Could not open conf file."); while (defined($line = <LOG>) ) { if ($line =~/:NOTICE:user=$contract,session=(\d+):/) { print OUTPUT $line; $echo=1; } elsif ($echo == 1) { print OUTPUT $line; if ($line =~/\)\[/) { $echo=0; } } }