in reply to Hash slice to get DB values - include undefined values?

Hi George_Sherston

This already happens as the code below shows:

my %hash=(a=>1,b=>2,c=>3); my @list=@hash{qw(a b c d e f)}; print "\t$_:",defined $list[$_] ? $list[$_] : "undef" foreach 0..$#lis +t; __END__ 0:1 1:2 2:3 3:undef 4:undef 5:undef
Undef is returned as with any hash lookup where the key is undefined. BTW I wrote a module called Tie::Hash::Trie (which I have yet to upload anywhere, life got in the way :-) which amongst other things alows this to be changed to any arbitrary value. Let me know if you want to see.

Yves / DeMerphq
--
This space for rent.

Replies are listed 'Best First'.
Re: Re: Hash slice to get DB values - include undefined values?
by George_Sherston (Vicar) on Dec 11, 2001 at 19:55 UTC
    Duh - how right you are... I thought that my problem was happening because my @vals array was too short... but, armed with the certainty that it couldn't be that, I revisit the code and find that it's another problem.

    The moral of this story is, if you want to know what's in an array in your CGI, don't do
    print @array;
    do
    print $_,br for @array;
    Thanks!

    § George Sherston
      Or perhaps
      print join("<BR>",@array)."<BR>";
      BTW, im sure you know this already but you could convert all of the undefs to a given constant quite easily
      my @values=map { exists $hash{$_} ? $hash{$_} : $constant } @keys_need +ed;
      You might want to change the exists() to a defined() if you dont want _any_ undefs getting through.

      :-)

      Yves / DeMerphq
      --
      This space for rent.