Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Matching over multiple lines in a scalar

by Enlil (Parson)
on Oct 27, 2002 at 22:18 UTC ( #208387=note: print w/replies, xml ) Need Help??


in reply to Matching over multiple lines in a scalar

I am assuming you want to slurp the whole file into the variable $info. It might be easier to do this as follows:
{ local $/ = undef; $info = <DATA>; }
For more info on $\ look at perlvar

Apart from this, I have changed the regex a little bit to do more of what I think you want it to do. Here is the code.

use strict; use warnings; use Data::Dumper; my $info; { local $/ = undef; $info = <DATA>; } my %lines; while ($info =~ m/(\d+)\: (.+?)\n(?=\d)/gs) { $lines{$1} = $2; } print Dumper (%lines); __DATA__ 3: Tag <test> found Tag <test> found 5: Tag <test> found 7: Tag <test> found 14: Tag <test> found 16: Tag <test> found 18: Tag <test> found 21: Tag <test> found 25: Tag <test> found 27: Tag <test> found 29: Tag <test> found 32: Tag <test> found 34: Tag <test> found 49: Tag <test> found 80: Tag <test> found 98: Tag <test> found Tag <test> found
and here is the output:
$VAR1 = '29'; $VAR2 = 'Tag <test> found'; $VAR3 = '21'; $VAR4 = 'Tag <test> found'; $VAR5 = '7'; $VAR6 = 'Tag <test> found'; $VAR7 = '14'; $VAR8 = 'Tag <test> found'; $VAR9 = '80'; $VAR10 = 'Tag <test> found'; $VAR11 = '32'; $VAR12 = 'Tag <test> found'; $VAR13 = '16'; $VAR14 = 'Tag <test> found'; $VAR15 = '49'; $VAR16 = 'Tag <test> found'; $VAR17 = '25'; $VAR18 = 'Tag <test> found'; $VAR19 = '3'; $VAR20 = 'Tag <test> found Tag <test> found'; $VAR21 = '34'; $VAR22 = 'Tag <test> found'; $VAR23 = '18'; $VAR24 = 'Tag <test> found'; $VAR25 = '27'; $VAR26 = 'Tag <test> found'; $VAR27 = '5'; $VAR28 = 'Tag <test> found';
. The regex: m/(\d+)\: (.+?)\n(?=\d)/gs
looks for a number then lazily matches up to where the next thing is a new line, but only if the first thing after that new line is a digit.

Well, I think this is what you want.

UPDATE: I meant to have placed the Dumper(\%lines) as BrowserUK has done below, instead of just Dumper(%lines). Don't know what I was thinking.

-enlil

Replies are listed 'Best First'.
(jeffa) 2Re: Matching over multiple lines in a scalar
by jeffa (Bishop) on Oct 27, 2002 at 23:54 UTC
    my $info; { local $/ = undef; $info = <DATA>; }
    can be golfed reduced down to:
    my $info = do {local $/;<DATA>};
    UPDATE:
    Well shucks ... chromatic has already said that in this thread (and thanks for the doobie doobie do, Aristotle).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Re: Matching over multiple lines in a scalar
by Rich36 (Chaplain) on Oct 27, 2002 at 22:33 UTC

    Thanks very much - that's exactly what I was looking for. I need to study up on ?=...

    The only problem in using that regex is that is doesn't capture the last line of input because the data doesn't end with a digit. The easy way around that was to add $info .= '00:';.


    «Rich36»
Re: Re: Matching over multiple lines in a scalar
by Louis_Wu (Chaplain) on Oct 27, 2002 at 23:32 UTC
    { local $/ = undef; $info = <DATA>; }
    For more info on $\ look at perlvar

    I think that you mean "info on $/ look ...", at least that variable exists elsewhere. :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2021-05-08 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (96 votes). Check out past polls.

    Notices?