harryC has asked for the wisdom of the Perl Monks concerning the following question:

I want to return two hashes from a subroutine
does not seem to work and gives this output
Use of uninitialized value in concatenation (.) or string at C:\scripts\perl\source\testhash.pl line 20.
test1 0 type-test2-0 value1-test2-0value1-test2-0
test1 1 type-test2-1 value1-test2-1value1-test2-1
test1 2 type-test2-2 value1-test2-2value1-test2-2
test1 3 type-test2-3 value1-test2-3value1-test2-3
test1 4 type-test2-4 value1-test2-4value1-test2-4
test1 5 type-test2-5 value1-test2-5value1-test2-5
test1 6 type-test2-6 value1-test2-6value1-test2-6
test1 7 type-test2-7 value1-test2-7value1-test2-7
Use of uninitialized value in concatenation (.) or string
at C:\scripts\perl\source\testhash.pl line 20.
Use of uninitialized value in concatenation (.) or string
at C:\scripts\perl\source\testhash.pl line 20. Use of uninitialized value in concatenation (.) or string
at C:\scripts\perl\source\testhash.pl line 20.
Use of uninitialized value in concatenation (.) or string
at C:\scripts\perl\source\testhash.pl line 20.
what is wrong in my code
#!/usr/bin/perl -w use strict; my %test1; my %test2; my $k; (%test1,%test2) = &testsub; foreach $k (sort keys %test1) { print " test1 $k $test1{$k}{'type'} $test1{$k}{'value1'}$test1{$k}{'v +alue1'} \n"; } foreach $k (sort keys %test2) { print " test1 $k $test2{$k}{'type'} $test2{$k}{'value1'}$test2{$k}{'v +alue1'} \n"; } sub testsub { my $hasrows=0; my %test1; my %test2; while ($hasrows < 5) { $test1{$hasrows} = { 'type' => "type-test-$1hasrows", 'value1' => "value1-test1-$hasrows", 'value2' => "value2-test1-$hasrows" }; $hasrows +=1; } $hasrows=0; while ($hasrows < 8) { $test2{$hasrows} = { 'type' => "type-test2-$hasrows", 'value1' => "value1-test2-$hasrows", 'value2' => "value2-test2-$hasrows" }; $hasrows +=1; } return (%test1,%test2); }
thks for your help
HarryC >/br>

20071029 Janitored by Corion: Changed </br> tags to <br/> tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: can i return two hashes from a soubroutine
by ikegami (Patriarch) on Oct 25, 2007 at 21:27 UTC

    Subroutines can only return a scalar in scalar context.
    Subroutines can only return a list of scalars in list context.
    Subroutines can never return hashes or arrays.
    That leaves you with two options:
    1) Return information that will allow you to rebuild the hashes and arrays you wish to return, and/or
    2) Return references to the hashes and arrays you wish to return.

    This is very similar to arguments.
    Only a list of scalars can be passed to a subroutine as arguments.
    Hashes and arrays can never be passed to a subroutine as arguments.
    You have the same two options in that situation:
    1) Pass information that will allow you to rebuild the hashes and arrays you wish to provide, and/or
    2) Pass references to the hashes and arrays you wish to provide.

Re: can i return two hashes from a soubroutine
by jdporter (Paladin) on Oct 25, 2007 at 20:38 UTC

    Of course, though you'll want to return them by reference. (Actually, you'll have to return at least the first one by reference.)

    return( \%test1, \%test2 );
    and use the return values appropriately in the caller.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: can i return two hashes from a soubroutine
by thezip (Vicar) on Oct 25, 2007 at 20:44 UTC

    You'll need to return references to your hashes from the sub, as in:

    return(\%test1, \%test2);

    In the caller, if you must, you can dereference the hashrefs by:

    my ($test1,$test2) = &testsub; my %test1 = %$test1; my %test2 = %$test2;

    Where do you want *them* to go today?
Re: can i return two hashes from a soubroutine
by toolic (Bishop) on Oct 25, 2007 at 20:45 UTC
    Use of uninitialized value in concatenation (.) or string at C:\scripts\perl\source\testhash.pl line 20.
    I believe you are getting this warning because you have a typo.

    Change:

    'type' => "type-test-$1hasrows",

    to:

    'type' => "type-test-$hasrows",