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

Hello monks, I need assistance with a small problem. How do I use a variable in a hash title when assigning key pairs?
I want to create 100+ hashes named like %hashname21, I have an incrementing value $countit for the numeric value needed in the hash title.
%hashname."$countit" = (FILE,$file);
gives me the error:
Can't modify concatenation (.) in scalar assignment at yourscript.pl line 282, near ");"
Pardon my ignorance of perl, I am still learning
HOW?
thanks

Replies are listed 'Best First'.
Re: How to use variable in Hash title
by thelenm (Vicar) on Aug 15, 2002 at 13:37 UTC
    I think you might be trying to do this:
    ${"hashname".$countit}{FILE} = $file;
    But don't do that. The code works, meaning that you will get a hash called %hashname21 if the value of $countit is 21. But it's much, much, much cleaner to use a hash of hashes or an array of hashes, like this:
    $hashes{$countit}->{FILE} = $file; # hash of hashes version $hashes[$countit]->{FILE} = $file; # array of hashes version
    These versions will also work when using strict, whereas the version using symbolic references will not. I generally avoid using symbolic references unless I can clearly articulate why I need them instead of hard references (and offhand, I can't think of an instance where I could). For more information about this topic, see perlref.

    -- Mike

    --
    just,my${.02}

Re: How to use variable in Hash title
by tommyw (Hermit) on Aug 15, 2002 at 13:30 UTC

    Ooh, you don't want to do it like that, noo, noo! ;-)

    You're trying to create a symbolic reference, and this is generally frowned upon, since it can cause very nasty effects if you get your variables mixed up

    If you use references instead, then everything becomes much easier. Try: $hash{$countit}=(FILE => $file); instead. The actual value can then be accessed as $hash{$countit}->{FILE}

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

      Yeah, but hashes can cause very nasty effects if you get your parenthesis mixed up. ;-)

      Try this instead:

      $hash {$countit} = {FILE => $file};
      Abigail
Re: How to use variable in Hash title
by itsmrchris (Initiate) on Aug 15, 2002 at 14:44 UTC
    thanks for the quick responses!
    making an array of incrementing hashes sounds practical
    sounds easy, but I am unsure how to properly do this - do I inclue the "%" - the book I have doesnt cover any of these examples, and it's late (oops early!) I am sleep deprived, and learning

    let me describe further: I have an array named @filennames that contains hundreds of files. I want to create a hash for each filename to store other linked data like $date, $owner, $permissions, etc. so I can call the keys individually later..
    can someone please describe both the method of making the proper array of hashes, hash key entries, and how to call the hash keys individually later in the file? pretend you are teaching a 12 yr old student :)
    Thanks again!!

      Have a look at perllol, which is Lists of Lists, ie 2-D arrays, and not precisely what you want, but is fairly readable, and more specifically at perldsc, the data structures cookbook, which covers nested data structures in general.

      --
      Tommy
      Too stupid to live.
      Too stubborn to die.

Re: How to use variable in Hash title
by Bird (Pilgrim) on Aug 15, 2002 at 13:49 UTC
    Actually, it doesn't sound like you should be naming your hashes using the $countit variable anyway. You're probably better off putting all of your hashes in an array and using $countit as the array subscript.
    $array_of_hashes[$countit]->{FILE} = $file;
    or...
    %{$array_of_hashes[$countit]} = (FILE => $file);
    -Bird