match and read
Anonymous Monk
created: 2006-01-26 03:26:34
Hi, I have a problem with perl matches. I am trying to read a xml file where I have to start reading when I see a patternThe pattern keeps changing every time in a for loop) and stop reading when I see another pattern( the sto p pattern remains the same) Now the code I am trying is this. while () { my $reading = 0; $reading++ if $reading; if(/.* ss:Name=\"$file*/oi){ # When we see start storing the data print "VALUE IS $value \n"; $reading = 1; } $reading = 0 if (//); next unless $reading; print "$_"; } here is the part of the file I am reading: APPLES GRAPES TOWELS NUMBER1 NUMBER2 NUMBER3 NUMBER4 9 0 0 0 4 126 5 144 01/24/2006-03:00
Re: match and read
created: 2006-01-26 03:46:50

There are a number of problems with you code fragment. In particular $value never gets a value and $reading can never become non-zero. Perhaps you could sort those problems first, then you could tell us what the problem is that you see and what result you would rather see.

Note that using a __DATA__ section as in the sample code below means that we can all share the pleasure of testing your code for you. Note too that use warnings; use strict; often saves red faces.

use strict;
use warnings;

my $file = "Appxxx";

while () {
  my $reading = 0;
  
  $reading++ if $reading;
  if(/.* ss:Name=\"$file*/oi){
    # When we see  start storing the data
    print "VALUE IS $value \n";
    $reading = 1;
  }
  
  $reading = 0 if (//);
  next unless $reading;
  print "$_";
}

__DATA__

APPLES GRAPES TOWELS NUMBER1 NUMBER2 NUMBER3 NUMBER4 9 0 0 0 4 126 5 144 01/24/2006-03:00

DWIM is Perl's answer to Gödel
Re: match and read
created: 2006-01-26 03:47:44
i know that for sake of brevity, you're likely omitting large portions of code. but i'm having trouble seeing where $file and $value come from, what you do with your matches, etc. further, the poor formatting/indentation (that it appears an attempt was made by the janitorial staff to clean up a bit) doesn't help matters, either .


$/  =  q#(\w)#  ;  sub  sig { print scalar reverse  join  ' ',  @_  }  sig
map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu" );
Re: match and read
created: 2006-01-26 06:38:06
Setting $reading to zero inside the loop doesn't seem right at all. Try...
#!perl

use warnings;
use strict;

my $r = 0
while () {
    $r++ if $r;
    if (/start/) {
        $r = 1;
        print "Start storing...\n";
    }
    $r = 0 if (/end/);
    next unless $r;
    print;
}
__DATA__
predata
more predata
start
real data
more real data
end
post data
post data
Re: match and read
created: 2006-01-26 11:10:47

I'm trying to figure out why you aren't treating the file as XML, and thus using an XML module, such as XML::Twig to parse and deal with it. For example, in this case, you could load it up in XML::Twig, and then cut out twigs that you don't care about. Or the reverse - use xpath to find twigs that you do care about. Or a combination.

It also allows you to reformat the output using a pretty-print mode, or to compress out unneeded whitespace or ... well, there's lots of room for doing wonderful things once you treat the data in the format it is rather than as just another text file.

Re^2: match and read
created: 2006-01-26 22:54:00
Hi Again, I fixed the problem. There was a small syntax issue in the begining parts of the code. Thank u kindly

perlmonks.org content © perlmonks.org and Anonymous Monk, chargrill, GrandFather, Tanktalus, tweetiepooh

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03