Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

arrays and hashes blended !

by prad_intel (Monk)
on Sep 29, 2005 at 12:54 UTC ( #496083=perlquestion: print w/ replies, xml ) Need Help??
prad_intel has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monastery wanderer ,

I just fumbled a way in blending arrays and hashes , I am naive to estimate whether this is a right way of doing... whatever is seen below( is what i meant ).

There cannot be a better judge than you , so please integrate your comments.
for($i=0;$i<=5;++$i){ print"\nEnter the $i th value which you need to append in hash: "; chomp($table_value[$i]=<STDIN>); } %new_hash_experiment=( "aa",$table_value[0], "ab",$table_value[1], "ac",$table_value[2], "ad",$table_value[3], "ae",$table_value[4], "af",$table_value[5]); foreach (sort keys %new_hash_experiment) { print "$_ contains $new_hash_experiment{$_}\n";
Its fun to code perl even when its going wrong !



Comment on arrays and hashes blended !
Download Code
Replies are listed 'Best First'.
Re: arrays and hashes blended !
by jeffa (Bishop) on Sep 29, 2005 at 13:32 UTC

    My only disappointment is that this is not a Perl-ish recipe for a mixed drink. :(

    My only suggestion is to avoid using 'hard coded' hash keys and array indices. I prefer to use a while loop to gather STDIN instead of a for loop, but there are many, many different ways to do this. Here's my take on it:

    use strict; use warnings; my %hash; my @array; my $key = 'aa'; my $prompt = "\nEnter value <cntl-d exits>: "; print $prompt; while (<STDIN>) { chomp; push @array, $_; print $prompt; } $hash{$key++} = $_ for @array; print "\n"; print "$_ contains $hash{$_}\n" for sort keys %hash;
    Feel free to ask any questions you may have about this code. :)


    (the triplet paradiddle with high-hat)
Re: arrays and hashes blended !
by osunderdog (Deacon) on Sep 29, 2005 at 13:35 UTC

    No need to store values in an intermediate array.

    use strict; my %new_hash_experiment = map {($_, undef)} qw(aa ab ac ad ae af); foreach my $key (sort(keys(%new_hash_experiment))) { print "\nEnter a value for key: $key: "; chomp($new_hash_experiment{$key} = <STDIN>); } foreach (sort keys %new_hash_experiment) { print "$_ contains $new_hash_experiment{$_}\n"; }

    All your base are belong to us!

    Hazah! I'm Employed!

      Thanks to all those

      Esp to osunderdog , the code he gave was the closest of what i exactly wanted.

      Well others , thanks i now have learnt a new lesson : Do not sumbit a code if there are some warnings and error in the code after using use strict or use warnings.. since that indicates a flaw in the logic.


Re: arrays and hashes blended !
by Roy Johnson (Monsignor) on Sep 29, 2005 at 14:12 UTC
    There was at one time an experimental feature that blended hashes and arrays. It was called a pseudohash. There is a module, Class::PseudoHash, that gives you the ability to work with it.

    There is also Tie::IxHash (and the XS version Tie::Hash::Indexed). It's not clear to me what you want to do, but these might fit the bill.

    Caution: Contents may have been coded under pressure.
Re: arrays and hashes blended !
by Anonymous Monk on Sep 29, 2005 at 13:42 UTC
    am naive to estimate whether this is a right way of doing...

    The right way of doing what? I'm not sure what you have archieved here. Typically, the reason to store something in a hash is to be able to quickly find it again - based on another criterium than 'order'. And you store something in an array to be able to find something based on order. Since you are left with none of the benefits of storing your data in a hash, and have lost some of the benefits of using an array, I don't think this is the right way - without even knowing where the way is supposed to lead to.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://496083]
Approved by rev_1318
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2016-05-02 20:31 GMT
Find Nodes?
    Voting Booth?