rohit_raghu has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I wrote a small piece of code to extract a table of values from a file into a hash but it's giving me a weird output. What am I doing wrong?
#!usr/bin/perl use strict; open (FILE,'<abc.txt'); my %config; while(<FILE>) { if($_=~'^(\w+_?\w+)=([01])') { $config{$1}=$2; print "$1:$config{$1}\n"; } } close FILE; for my $i (sort keys(%config)) { print "$i:$config{i}\n"; }
This is the output I get:
$ perl test002.pl AAA:1 BBBB:0 CCC:0 DD:1 AAA: BBBB: CCC: DD:
And this is the sample input file:
AAA=1; BBBB=0; CCC=0; DD=1;
Rohit Raghunathan

Replies are listed 'Best First'.
Re: hash assignment
by Ratazong (Monsignor) on Jul 25, 2012 at 07:43 UTC

    print "$i:$config{i}\n";
    You are missing a $ before the second i

    Rata
      Oops. I apologize for the waste of your time.
      Rohit Raghunathan
        Not a "waste" but an opportunity to learn:
        1. use warnings; as well as strict;
        2. Check compilation before throwing up your hands in dismay. That is, at your command prompt, execute perl -c filename

        The two, together, will reveal problems such as this.