iterate over newlines
6 answers - 1074 bytes -

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:
open (YIP, "< /")
|| die("No SUP FR YU!!");
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/ || $_ =~ /\[/ || $_ =~ /\]/ ){
next LINE if /./gs;
unless ( $` =~ /\)/ ) { #get # of pes
#unless ( $_ =~ /\)/ ){
$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?
thanks
ryan
No.1 | | 444 bytes |
| 
7/5/06, Ryan Moszynski <ryan.m.lists (AT) gmail (DOT) comwrote:
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.
This sounds like a job for Parse::RecDescent, maybe?
http://search.cpan.org/~dconway/Parse-RecDescent-1.94/
Hope this helps!
Phoenix
Stonehenge Perl Training
No.2 | | 710 bytes |
| 
i really hope thats overkill for my problem. I have my program
finshed, this is the last condition i have to deal with. Also, my
supervisor would prefer me not to use any nonstandard modules.
7/5/06, Tom Phoenix <tom (AT) stonehenge (DOT) comwrote:
7/5/06, Ryan Moszynski <ryan.m.lists (AT) gmail (DOT) comwrote:
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.
This sounds like a job for Parse::RecDescent, maybe?
http://search.cpan.org/~dconway/Parse-RecDescent-1.94/
Hope this helps!
Phoenix
Stonehenge Perl Training
No.3 | | 935 bytes |
| 
Ryan Moszynski wrote:
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:
open (YIP, "<
/")
|| die("No SUP FR YU!!");
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/ || $_ =~ /\[/ || $_ =~ /\]/ ){
This is painful to even read.
my $flag = 0;
while( <){
chomp;
if( /iotests/ ){
$flag = 1;
}elsif( $flag && /\s*(.*?)\s*\[\s*(.*?)\s*\] ){
print "$1[$2]\n";
}elsif( $flag && /^\s*\)\s*$/ ){
$flag = 0;
}
}
No.4 | | 3628 bytes |
| 
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, "<
/
")
>
|| die("No SUP FR YU!!");
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;
}
}
** UTPUT
iotests ( WriteEmUp [create:openwr:write:close] ReadEmUp
[openrd:read:close] )
WriteEmUp [create:openwr:write:close]
ReadEmUp [openrd:read:close]
I hope this helps,
Rob
No.5 | | 662 bytes |
| 
Ryan Moszynski wrote:
i really hope thats overkill for my problem. I have my program
finshed, this is the last condition i have to deal with. Also, my
supervisor would prefer me not to use any nonstandard modules.
Why not, if it on CPAN its "standard". If its not bundled with the perl
you have what so hard about a one time
perl -MCPAN -e 'install Whatever;'
and look at the author, dconway, if your boss has any sense he'll be
more than happy to use one of damian's modules.
If not then he's already a hopeless clueless idiot who wiches to remain
such. In that case what can you do :)
No.6 | | 277 bytes |
| 
Thu, Jul 06, 2006 at 06:04:27PM -0500, JupiterHost.Net wrote:
and look at the author, dconway, if your boss has any sense he'll be
more than happy to use one of damian's modules.
course, that depends on which module and what you want to use it for.