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

I'm an idiot. Now that that's out of the way, check it out...

A csv number list is on the command line. Why does:

$files[$i]{hashkey}=split(/,/,$ARGV[0]);

... assign the number of tokens in the scalar context of the return value of split on $ARGV[0] to my hash in my array of hashes instead of returning the expected array of strings?

Replies are listed 'Best First'.
Re: Assigning an array of strings to a hash value in an array of hashes
by batkins (Chaplain) on Dec 20, 2002 at 01:36 UTC
    Try making an anonymous array like so:
    $files[$i]{hashkey}= [ split(/,/,$ARGV[0]) ];
    those extra square brackets will create an array containing the results of split and return a reference to it. now you'll access an element like so:
    print $files[$i]{hashkey}->[0];
    where -> is the dereference operator and 0 is the index of the element in question.
      (original poster)

      Now I have a username to voice my gratitude... I had no idea so many people were watching this to help out people like me... dear lord!

      By the time I had registered a username, logged in, and actually found my Anonymous Monk post, there were already four right answers!!

      I am awed and humbled...
Re: Assigning an array of strings to a hash value in an array of hashes
by submersible_toaster (Chaplain) on Dec 20, 2002 at 01:41 UTC

    You have to force split into a list, before assigning it to the hash using square brackets . This discussed in this node about array and list context

    >./test.pl some,kinda,sick,joke #!/usr/bin/perl -w use strict; use Data::Dumper; my @files=[ {foo=>[qw/this that other/], bar=>[qw/them those they/]}, {foo=>[qw/up down left right/], bar=>[qw/spin turn veer/]} ]; ################# Square brackets round split ###### $files[2]{foo} = [ split(/,/,$ARGV[0]) ]; print Dumper @files; __DATA__ $VAR1 = [ { 'foo' => [ 'this', 'that', 'ottther' ], 'bar' => [ 'them', 'those', 'they' ] }, { 'foo' => [ 'up', 'down', 'left', 'right' ], 'bar' => [ 'spin', 'turn', 'veer' ] } ]; $VAR2 = undef; $VAR3 = { 'foo' => [ 'this', 'that', 'other' ] };
Re: Assigning an array of strings to a hash value in an array of hashes
by LTjake (Prior) on Dec 20, 2002 at 01:40 UTC
    This is because it is in scalar context. See this example:
    use Data::Dumper; $string = 'A,B,C'; @test = (); $test[0]{test} = split(/,/, $string); print Dumper(\@test); @test = (); @{$test[0]{test}} = split(/,/, $string); print Dumper(\@test); __END__ $VAR1 = [ { 'test' => 3 } ]; $VAR1 = [ { 'test' => [ 'A', 'B', 'C' ] } ];


    --
    "I don't intend for this to take on a political tone. I'm just here for the drugs." --Nancy Reagan
Re: Assigning an array of strings to a hash value in an array of hashes
by ibanix (Hermit) on Dec 20, 2002 at 01:38 UTC
    I think it is because $files[$i]{hashkey} has scalar context.

    Try @{ $files[$i]{hashkey} }=split(/,/,$ARGV[0]) ?

    Cheers,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;