in reply to Hash problem

sort of a side note, but you can't assign a whole array to one hash element - I.E.
if (!$Tex{$a}){$Tex{$a} = @bex};
Will not work. You probably want to use an anonymous array instead:
if (!$Tex{$a}){$Tex{$a} = [@bex] };

Replies are listed 'Best First'.
Re^2: Hash problem
by BrowserUk (Patriarch) on Feb 04, 2006 at 02:08 UTC

    You're right about needing to use a reference to an array for assigning to a hash element, but that way of doing it can be rather wasteful. What you are doing with $Tex{$a} = [@bex] is copying the contents of @bex in to another array and then assign a reference to that new array to $Tex{$a}.

    You can avoid the unneccesary copying by just taking a reference to the original array

    if (!$Tex{$a}){ $Tex{$a} = \@bex; }

    Of course, this only works correctly in a loop if the array you are taking a reference to is scoped to the loop. If not, your method is correct.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      \@bex won't work unless you do my @bex in the foreach loop. You mentioned this (in less explicit terms), but I just wanted to be clear. Without the my, all values will be references to the same array. In fact, the OP says he's using [@bex], but the symptoms he's describing are those of \@bex.
        \@bex won't work unless you do my @bex in the foreach loop. You mentioned this (in less explicit terms), but I just wanted to be clear.

        The scoping doesn't have to be my, local works just fine too:

        #! perl -slw use strict; use Data::Dumper::SLC; my %hash; our @array; for my $i ( 0 .. 9 ) { local @array = map{ "$i : $_" } 1 .. 5; $hash{ $i } = \@array; } Dump \%hash, 60; __END__ c:\Perl\test>junk3 { 0 => [ '0 : 1', '0 : 2', '0 : 3', '0 : 4', '0 : 5', ], 1 => [ '1 : 1', '1 : 2', '1 : 3', '1 : 4', '1 : 5', ], 2 => [ '2 : 1', '2 : 2', '2 : 3', '2 : 4', '2 : 5', ], 3 => [ '3 : 1', '3 : 2', '3 : 3', '3 : 4', '3 : 5', ], 4 => [ '4 : 1', '4 : 2', '4 : 3', '4 : 4', '4 : 5', ], 5 => [ '5 : 1', '5 : 2', '5 : 3', '5 : 4', '5 : 5', ], 6 => [ '6 : 1', '6 : 2', '6 : 3', '6 : 4', '6 : 5', ], 7 => [ '7 : 1', '7 : 2', '7 : 3', '7 : 4', '7 : 5', ], 8 => [ '8 : 1', '8 : 2', '8 : 3', '8 : 4', '8 : 5', ], 9 => [ '9 : 1', '9 : 2', '9 : 3', '9 : 4', '9 : 5', ], }

        The funny thing is that I changed the wording specifically to avoid a correction being required :)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Hash problem
by Gnat53 (Novice) on Feb 04, 2006 at 01:19 UTC
    Thanks -- but this hangs my system up!!!!