Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Parsing for [] in a file

by toolic (Bishop)
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

Replies are listed 'Best First'.
Re^2: Parsing for [] in a file
by MidLifeXis (Monsignor) 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.


      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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2016-10-25 00:30 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (313 votes). Check out past polls.