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

Re: Matching over multiple lines in a scalar

by BrowserUk (Pope)
on Oct 27, 2002 at 22:31 UTC ( #208389=note: print w/replies, xml ) Need Help??


in reply to Matching over multiple lines in a scalar

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $info; $info .= $_ while(<DATA>); my %lines; while($info =~ m/(?:^|\n)(\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

Gives

c:\test>208384 $VAR1 = { '29' => ' Tag <test> found', '21' => ' Tag <test> found', '7' => ' Tag <test> found', '14' => ' Tag <test> found', '80' => ' Tag <test> found', '32' => ' Tag <test> found', '16' => ' Tag <test> found', '49' => ' Tag <test> found', '25' => ' Tag <test> found', '3' => ' Tag <test> found Tag <test> found', '98' => ' Tag <test> found Tag <test> found', '34' => ' Tag <test> found', '18' => ' Tag <test> found', '27' => ' Tag <test> found', '5' => ' Tag <test> found' }; c:\test>

Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re: Re: Matching over multiple lines in a scalar
by Rich36 (Chaplain) on Oct 27, 2002 at 22:39 UTC

    Thanks for the help on this. By the way - just out of curiosity - why pass a hash reference to Data::Dumper instead of just the hash?


    «Rich36»

      If you pass the hash, it gets flattened and passed as a list of 30 seperate scalars, the association between key=>value pairs is lost. By passing the reference, Data::Dumper knows it a hash, and outputs it as such, with the Key=>value pairs clearly associated and shown as being a part of a compound entity.

      In fact, you can then write this output to a file and then read it back, eval the string and it will recontruct the hash in memory. Often used as a cheap man DB.

      Run the program both ways to see the difference.


      Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

Log In?
Username:
Password:

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

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





    Results (96 votes). Check out past polls.

    Notices?