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

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 http://sscce.org

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.

    --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 browsing the Monastery: (7)
As of 2016-06-30 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (405 votes). Check out past polls.