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

Hello,
I am in pampers.

I am reading from a file and want to name different hashes according to the data in the file. For example, my file could simply be

0.01 0.02 0.03 0.04 1.01 1.02 1.03 1.04

from this, I want two hashes %001 and %002 to which I will assign the rest of the info in prelabeled keys.

I tried asigning from a match parenthesis memory, $1 but it gave me the following error message:

"Can't use string ("0.01") as a SCALAR ref while "strict refs" in use at ./perlexamples/partial_hasher line 6, <> line 1."

I would appreciate any help!
Thanks.
Monjo

Replies are listed 'Best First'.
Re: hash name from read file
by holli (Abbot) on Mar 07, 2006 at 09:44 UTC
    I am reading from a file and want to name different hashes according to the data in the file.
    A BIG No! What you want is a hash of hashes. Simply define a %data and use your input data as top level keys. see perlref for more about complex data structures.


    holli, /regexed monk/
Re: hash name from read file
by GrandFather (Saint) on Mar 07, 2006 at 09:49 UTC

    holli has given you the answer you require for your immediate problem. However, in general it helps us help you if you show us what you have tried.

    From your data and description it's not even clear that a hash is the best thing for you to use - most likely what you really need is an array or arrays (AoA).


    DWIM is Perl's answer to Gödel
Re: hash name from read file
by linux454 (Pilgrim) on Mar 07, 2006 at 15:29 UTC
    holli is correct, you don't really want to implement it that way, a hash of hases or Array of arrays, etc, would be much more appropriate.

    That being said there is a way to do what you want that being inserting your own entry into the symbol table, thusly:

    use strict; use warnings; { no strict 'refs'; # Create the 001 hash *{ "main::001" } = { akey => 'avalue', bkey => 'bvalue' }; # alias the 001 hash to some common name *{ "main::thehash" } = *{ "main::001" }; } # show contents of 001 hash while ( my($k,$v) = each(%001) ) { print "$k => $v\n"; } # should be same as above, some monk smarter than I may # be able to tell why the 001 name got imported but not, # thehash into the main:: package. while ( my($k,$v) = each(%main::thehash) ) { print "$k => $v\n"; }

    Again let me say this is a sub-optimal solution, an actuall datastructure would serve you better here, and be much more maintainable and readable. This post is knowledge for knowledge's sake.

A reply falls below the community's threshold of quality. You may see it by logging in.