in reply to how to store an array to a pseudohash?

As others have mentioned, psuedohashes were an experiment that didn't pan out, and are been deprecated in perl 5.8 and gone in perl 5.10.

I thought I'd add to what others have shown you here by suggesting that you try out Data::Dumper. It is very helpful when trying to understand datastructures. Try this code:

use Data::Dumper; my $slice = { kind => 'alph', prop => 'single', item => 5..11, } print Dumper $slice;

While you are looking at perldoc.perl.org, check out perldsc--the data structures cookbook. There's good stuff in there on setting up nested structures.

I also noticed that you use map to iterate over your @item_2 array, but don't use the return value--in other words, you are using map in a void context. Some people think this is a bad idea. Others don't have a problem with it. I don't really care, one way or the other. Anyhow, in case you decide you don't like void map, I'll show a couple of ways to do the same thing without raising that stylistic bugaboo:

# print takes a list of arguments # here map generates a list of args for one call to print. print map { "num is $num the $_\n" } @item_2; # simply replaced map with foreach used as a statement modifier. print "num is $num the $_\n" foreach @item_2;


TGI says moo

Replies are listed 'Best First'.
Re^2: how to store an array to a pseudohash?
by Hanken (Acolyte) on Jun 29, 2008 at 11:46 UTC
    Thanks for all the replies which are very instructive! (1) Thanks for leting me know that using following statement is referred to another hash which is not what I expected.
    my $slice = { kind => $kind, prop => $prop, item => 5..11 };

    (2) Thanks for pointing out the dereference method like:
    my @items = @{$slice -> {item}};
    I don't really understand why I need use @{} here to dereference, can somebody explain it for more details? I am just a Perl newbie :)
    (3) Thanks for present a good program for data structure viewer -- Data::Dumper. I guessed I heard of it sometime ago, now I know how to use it. It's a good toolkit for debugging too.
    (4) Thanks for criticizing my bad coding style (using map in a void context). I am digging into the link to find out why I should avoid to use this style... :)
      (4) Thanks for criticizing my bad coding style (using map in a void context). I am digging into the link to find out why I should avoid to use this style... :)

      Don't be too hard on yourself for this one. Void map/grep have been argued over for years in the perl community. Some don't see a problem with it, others do. Definitely look into the issue and decide for yourself what is right.

      I'm personally neutral on void map. I use foreach in most cases where a void map could be used, because I think it more clearly expresses the intent of the code. However, I would have no problem using a void map if I thought it made things more readable.

      You might like to check out this nice article on void map


      TGI says moo

      I don't really understand why I need use @{} here to dereference, can somebody explain it for more details?
      By using the square brackets, you have created a reference to an array. Since you want to access the elements of the array reference, you first need to de-reference the array. One way to do so is to use the @{} syntax. Here are some good resources on the topic: