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


In reply to RE: Re: problem with multi-line by nuance
in thread problem with multi-line by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.