http://www.perlmonks.org?node_id=38743

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

How do I dynamically create a hash table?

Originally posted as a Categorized Question.

  • Comment on How do I dynamically create a hash table?

Replies are listed 'Best First'.
Re: How do I dynamically create a hash table?
by tedv (Pilgrim) on Oct 27, 2000 at 20:54 UTC
    use strict; my %hash = (); foreach $value (@some_list) { my $key = make_some_key($value); $hash{$key} = $value; }

    In other words, just assign a new key/value pair using $hash{$key} = $value. It's the logical compliment of how you'd access a hash value: $value = $hash{$key}

    -Ted
Re: How do I dynamically create a hash table?
by Anonymous Monk on Oct 19, 2004 at 18:42 UTC
    In only 1 line!
    %new_hash = (split(/;/,$variable_with_keys_and_values));
    For example:
    %new_hash = (split(/;/,'apple;good;banana;good;citrus;good;no doughnut +;bad cop')); print $new_hash{'no doughnut'};
    Outputs the value associated with the key 'no doughnut'
      This would probably be more clear if you said print $new_hash{'doughnut'}; instead of print $new_hash{'no doughnut'}; There is no item with key 'no doughnut'.
        Thanks; Anonymous Monk had originally had everything say "doughnut". one of the QandAEditors changed it to "no doughnut" and apparently flubbed it. Now fixed.
Re: How do I dynamically create a hash table?
by kharris (Initiate) on Sep 25, 2002 at 18:51 UTC
    I just got done with this sort of thing. It looks like this.

    my ($url, @rest, $url_rec, @url_keys); ($url, @rest)=split /\|/,$_; if($#rest == 3) # We're re-building. { @url_keys= qw{url_last_checked_return_code url_cat_id url_descriptio +n url_last_checked}; } if($#rest == 1) # We're adding. { @url_keys = qw{url_cat_id url_description}; } foreach my $key (@url_keys) { $url_rec->{$key} = pop @rest; }

    I know that someone above adept-level could have done this more elegantly efficiently, but it works...so far. ;-)

    kevin

Re: How do I dynamically create a hash table?
by Anonymous Monk on Oct 08, 2001 at 19:14 UTC
    We have tried out this way:

    @list_of_elements = split /;/,$variable_with_keys_and_values);
    %new_hash = (@list_of_elements);
    

    We think this is more simple (just two lines!)

    - Franco e Giuseppe.
Re: How do I dynamically create a hash table? (A straightforward Q, I know!)
by merlyn (Sage) on Oct 27, 2000 at 03:25 UTC
    This question needs more context. Do you mean:
    %hash_table = @dynamically_read_data;
    Or if not, then what?

    Originally posted as a Categorized Answer.

Re: How do I dynamically create a hash table? (A straightforward Q, I know!)
by davorg (Chancellor) on Oct 27, 2000 at 12:06 UTC

    As merlyn says, it's not very clear what you're asking. You might find that the examples in the perldsc manual page help you tho'.

    Originally posted as a Categorized Answer.

Re: How do I dynamically create a hash table? (A straightforward Q, I know!)
by kharris (Initiate) on Sep 25, 2002 at 18:51 UTC
    I just got done with this sort of thing. It looks like this.

    my ($url, @rest, $url_rec, @url_keys); ($url, @rest)=split /\|/,$_; if($#rest == 3) # We're re-building. { @url_keys= qw{url_last_checked_return_code url_cat_id url_descriptio +n url_last_checked}; } if($#rest == 1) # We're adding. { @url_keys = qw{url_cat_id url_description}; } foreach my $key (@url_keys) { $url_rec->{$key} = pop @rest; }

    I know that someone above adept-level could have done this more elegantly efficiently, but it works...so far. ;-)

    kevin

    Originally posted as a Categorized Answer.