Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Help Extract these lines

by Anonymous Monk
on Jun 17, 2013 at 18:31 UTC ( #1039441=note: print w/replies, xml ) Need Help??


in reply to Re: Help Extract these lines
in thread Help Extract these lines

I thought of using a range operator to grab the two lines. But I'm confused as to why it is not grabbing the two lines.

Replies are listed 'Best First'.
Re^3: Help Extract these lines
by Preceptor (Deacon) on Jun 17, 2013 at 18:38 UTC

    Because the 'while' loop goes off first, populating $_ with one line of DATA. Your range operator then applies to that, which is why it doesn't work.

    #!/usr/bin/perl use strict; use warnings; my %rec; while ( my $line = <DATA>) { $line .= <DATA>; $line =~ s/\"//g; $line =~s/,//g; my ( $id, $name ) = ( $line =~ m/id: (\S+)\nname: (\S+)/mg ); print "$id = $name\n"; $rec{$id} = $name; } __DATA__ "id": "xx-ada-qwebasd", "name": "telphone", "id": "fasda-asd-123123-fkja123a", "name": "car", "id": "97f921-a312-fas2", "name": "ball",

    That I think does the trick. (Basically, grabs two lines in a go, but isn't ideal if your data structure is more complicated). I suspect there's something more clever you could do to parse a file and grab out pattern matching, but I think most of those would involve reading the file in a scalar context and reading the whole lot (which may be fine, but can go wrong with large files).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039441]
help
Chatterbox?
[marioroy]: LA only $ and \ needed backslash, not ' and ".
[marioroy]: my @music_times = qx(find \$HOME/Music/ Albums -type f -iname '*.mp3' -exec mp3info -p "%S\\n" {} \\;);
[marioroy]: I wanted to come back and report on that.
[marioroy]: cheers

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-04-23 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (433 votes). Check out past polls.