Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Comment on Re: Parsing for [] in a file
Download Code
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 drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2016-02-11 04:32 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (359 votes), past polls