in reply to Re: problem with multi-line
in thread problem with multi-line
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.
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 :-)#!/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
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:
Which leaves your results in @arr#!/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
Nuance
|
---|