http://www.perlmonks.org?node_id=938655


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!