Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2022-05-24 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?