sorry for the resend, i made a mistake in the subject line and didn't
want anyone folling the thread to miss the resolution.

---------- Forwarded message ----------
From: Ryan Moszynski <[EMAIL PROTECTED]>
Date: Jul 6, 2006 1:57 PM
Subject: Re: beginners Digest 6 Jul 2006 08:41:28 -0000 Issue 2890
To: [EMAIL PROTECTED], beginners@perl.org


rob,

thanks, awesome help.  I've been stuck on that part for too long.
i'm replying to the questions in your reply, but your solution worked,
so you can ignore this if youre busy.


---------- Forwarded message ----------
From: Rob Dixon <[EMAIL PROTECTED]>
To: beginners@perl.org
Date: Thu, 06 Jul 2006 00:03:27 +0100
Subject: Re: iterate over newlines
Ryan Moszynski wrote:

Hi Ryan

 > this is a snippet of a file i'm trying to process:
 > ###########
 >
 >  iotests (
 >     WriteEmUp [create:openwr:write:close]
 >     ReadEmUp [openrd:read:close]
 >  )
 > ####################
 >
 > i need my perl regex to recognize "iotests", then make anything that
 > matches ".[.]" into a string.  I can't count on the whitespace or the
 > new lines being there or not. i read the file into perl with:

I'm not sure what you mean here. Are you talking in terms of the regex
'.' character? My best guess is that you want to find everything that
looks like

   word [stuff]

but please let me know.

yes, thats what i meant.  I should have been more clear.


 > #################
 > open (YIP, "<
 > /home/ryan/code/perl/enzo_fio_tests/output/verify_4MB/
 >    fio_enzo_verify_4mbb.inputscratch")
 >
 >      || die("No SOUP FOR YOU!!");

You should put $! in your die string so that you can see the reason the
open failed.

right again


 >
 > LINE: while (<YIP>){
 > ############################
 >
 > and i've been struggling with some form of this, which doesn't work,
 > though I'm not quite sure why:
 > ####################
 >     if ( $_ =~ /iotests/ || $_ =~ /\[/ || $_ =~ /\]/ ){

A regex will test $_ by default, so this could be

   if ( /iotests/ || /\[/ || /\]/ ){


 >     next LINE if /./gs;

I don't understand what this is supposed to do. The regex will succeed
unless $_ contains the empty string, which can never be true within this
while loop.

i forgot the "*" i wanted this code:
############
    if ( $_ =~ /iotests/ || $_ =~ /\[/ || $_ =~ /\]/ ){

    next LINE if /./gs;

    unless  ( $` =~ /\)/ ) {
############

to find 'iotests' or '[' or ']', and then if ANYTHING, including the
empty string, go to the next line unless it found a ')' in the line
above, to make sure that the text inside the parentheses is
proccessed, even if it was in the same line as the closing paren.
thats why i commented  that other line.

i didn't right in the code to process the lines between iotests and
the right parenthese yet.


 >     unless  ( $` =~ /\)/ ) {           #get # of pes

Again, I can't see what you are trying to do. $` holds the contents of
the object string preceding the last successful pattern match. Do you
mean $_ again?

 >
 >     #unless ( $_ =~ /\)/ ){

Like this, except you've commented it out.

 >      $blah4 = $blah4 . $_ ;
 >         print $blah4."\n";
 >
 >     }
 >
 > ###################
 >
 > right now, as you can see, i'm just trying to turn the chunk i need
 > into a multiline string so i can work on it, but i haven't been
 > successfull.  I know I must be approaching this the wrong way, any
 > ideas?

This looks like an ideal case for the range operator '..'. Read about it
in perldoc perlop.

that was my big problem, i didn't know about '..'
i've been searching through all sorts of documentation and hadn't run
across it until you suggested it.


In the following code, the test /iotests/ .. /\)/ will be false until a
line is found which contains 'iotests'. It will then stay true until a
line arrives containing ')'. While this is true the current line is
appended to $chunk. If the test is false then $chunk holds the full
block to be processed (because of the chomp() it is a single line of
text). The code I have written prints out the chunk, and also finds all
the data I think you want from this string and prints that out as well.
The chunk is then undefined so it doesn't get processed again. The
output from your very small data set is shown. If it's not quite what
you want then let us know.

my $chunk;

while (<YIP>) {

   chomp;

   if ( /iotests/ .. /\)/ ) { # Inside a chunk?
     $chunk .= $_;
   }
   elsif (defined $chunk) {

     print $chunk, "\n\n";

     while ($chunk =~ /(\S+\s*\[.*?\])/g) {
       print $1, "\n";
     }

     print "\n\n\n";

     undef $chunk;
   }
}

** OUTPUT

  iotests (    WriteEmUp [create:openwr:write:close]    ReadEmUp
  [openrd:read:close] )

WriteEmUp [create:openwr:write:close]
ReadEmUp [openrd:read:close]



I hope this helps,

Rob




also, you code worked just about exactly as is in my program, though i
changed a few details.  Once again, awesome, thanks.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to