Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

RE: Re: problem with multi-line

by nuance (Hermit)
on Aug 25, 2000 at 18:12 UTC ( [id://29625]=note: print w/replies, xml ) Need Help??


in reply to Re: problem with multi-line
in thread problem with multi-line

I don't think that's the problem, he doesn't appear to be opening anything properly or reading anything from the filehandle.

the ($titi, $toto) = m/$matchstuff/; puts the match operator in a list context, it then return a list of backreferences ($1, $2, ...). this means $titi is assigned the value of $1 and $toto is assigned the value of $2. Which is what our anonymous friend appears to be attempting.

The "spec" is quite confused, I'm not sure what is needed in order to match the second line. Is there a manditory tab character at the start, the regex appears to say there is but that may just be using the wrong modifier. I'm going to assume that there is at least one tab, here's my take on the problem.

#!/usr/local/bin/perl -w use strict; my ($titi, $tito); { $_ = <DATA>; if (m/^\.(\w+)/ .. m/^\t+\.\w+\W+(\w+)/) { $tito = $1 and next if defined $titi; $titi = $1 unless defined $titi; }; redo; } print "$titi, $tito\n"; __DATA__ .this is a line .and second is another line .again, a line. .Yet another line
Don't forget that you need to properly open the file you will be reading from, I'm just using the DATA file handle here (as pinched from previous answer by ctweten :-)

I suppose I should comment on what I've differently, I'm using two regexs and the range operator. The range operator in scalar context is a flip flop operator. it returns false until the first expression matches, then it continues to return true until the second regex matches. So we check each line until we get a match for the first one, the $titi is undefined so the first statement is skipped and $titi is assigned a value. The "bare block loop construct" is then "redone" until the second regex matches at which point the value is assigned to $tito and the whole loop exits.

If I've misunderstood the format of your data, you may have to tweak the regexs.

Update:
I've just realised that my code only pulls one pair of values if you want all pairs that may exist in the file, then you could try:

#!/usr/local/bin/perl -w use strict; my @arr; while (<DATA>) { push @arr, $1 if m/^\.(\w+)/ .. m/^\t+\.\w+\W+(\w+)/; }; print join " ", @arr; __DATA__ .this is a line .and second is another line .again, a line. .Yet another line
Which leaves your results in @arr

Nuance

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://29625]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found