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.

> #################
> 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.

>
> 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.

>     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.

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

--
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