http://qs1969.pair.com?node_id=612167

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

Hi Monks,

i am facing a problem in assigning an array into hash.

i have a ini file like

article=>art chapter=>chap section=>sec

My code to access the values in the ini file

open (INI, "<ReNLM.ini") || "error ini file"; while (<INI>) { chomp($_); $con[$i] = $_; $i++ } $" = ", "; %conval = qw(@con); print $conval{article};

i didn't get outpupt

thanks

Aakikce

Replies are listed 'Best First'.
Re: assign an array into hash
by GrandFather (Saint) on Apr 26, 2007 at 09:31 UTC

    qw(@con) is an error. Perl would have told you about it if you used strictures (use strict; use warnings;). Always use strictures. qw quotes 'words' where in this case a 'word' is any sequence of non-white space characters. @con is taken to be a word by qw so you are trying to assign a single element list to a hash - that doesn't do what you want.

    Consider instead:

    use strict; use warnings; my %con; while (<DATA>) { chomp($_); next unless /^(.*?)=>(.*)/; $con{$1} = $2; } print "Key: $_, Value: $con{$_}\n" for sort keys %con; __DATA__ article=>art chapter=>chap section=>sec

    Prints:

    Key: article, Value: art Key: chapter, Value: chap Key: section, Value: sec

    DWIM is Perl's answer to Gödel
Re: assign an array into hash
by rinceWind (Monsignor) on Apr 26, 2007 at 09:33 UTC

    aakikce, I think the prolem is not in the assignment of the array to a hash, but in the parsing of your input file. You are wanting @con to receive key and value pairs, the key going into even and value going into odd indices of your array. Here's a version of your while loop that should work:

    while (<INI>) { my ($key, $value) = /^(\w+)=\>(\w+)/; push @con, $key, $value; }

    There are a couple of style points to note. I'm using "my" to declare variables that I'm using. This goes a long way towards strictness - allowing you to "use strict" at the top of your program. Learn how to use push: it'll save you needing to count array elements and have a $i variable.

    --
    wetware hacker
    (Qualified NLP Practitioner and Hypnotherapist)

Re: assign an array into hash
by johngg (Canon) on Apr 26, 2007 at 09:37 UTC
    There are a few problems with your code. Firstly, it is always recommended to place use strict; and use warnings; at the top of your scripts to help avoid typos etc.

    When you read your file you construct an array with one element per line where you actually need two. Interpolating an array into a string is only going to result in a single string being assigned to your hash. In any case, qw( ... ) doesn't interpolate so you were assigning the string "@con" to your hash. You can assign an array to a hash but you will get a warning if there aren't an even number of elements..

    use strict; use warnings; use Data::Dumper; my $inFile = q{spw612167.dat}; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; my @conArr = (); while ( <$inFH> ) { chomp; push @conArr, split m{=>}; } close $inFH or die qq{close: $inFile: $!\n}; my %conHash = @conArr; print Data::Dumper->Dump([\@conArr, \%conHash], [qw{*conArr *conHash}] +);

    Here's the output

    @conArr = ( 'article', 'art', 'chapter', 'chap', 'section', 'sec' ); %conHash = ( 'chapter' => 'chap', 'article' => 'art', 'section' => 'sec' );

    I hope this helps you.

    Cheers,

    JohnGG

Re: assign an array into hash
by akho (Hermit) on Apr 26, 2007 at 10:15 UTC
    Assigning arrays to hashes is usually a bad idea. The excellent suggestions above (especially the one by GrandFather) will help you get this to work; but you really should look into using one of the relevant CPAN modules, such as Config::INI::Simple.