Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Parsing for [] in a file

by toolic (Chancellor)
on Nov 17, 2011 at 17:53 UTC ( #938655=note: print w/ replies, xml ) Need Help??


in reply to Parsing for [] in a file

This gives you your desired output:

use warnings; use strict; my %results = (a => 1); foreach my $var (keys %results) { while (my $attr_line = <DATA>) { if ($attr_line =~ /^\Q$var/ ) { print $attr_line; } } } __DATA__ a|12 a[11]|1 a[11][11]|0 b|2 c|0
Update: in response to to OP's update, read http://sscce.org


Comment on Re: Parsing for [] in a file
Download Code
Re^2: Parsing for [] in a file
by MidLifeXis (Prior) on Nov 17, 2011 at 17:57 UTC

    Except it will only work for the first key in %results, as the position in DATA will be at eof.

    If you flip around the foreach and the while, it should do as intended.

    Update: If you load the variable names (%results), make sure that you use chomp.

    --MidLifeXis

      ah!that greatly worked. Thank you very much. swapping the foreach and while and chomping after foreach.
Re^2: Parsing for [] in a file
by HJ (Novice) on Nov 17, 2011 at 18:58 UTC
    Hi toolic, Yes it worked just for the first line. Thanks for you response!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-08-22 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (153 votes), past polls