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

dear monks,
i have a problem while retrieving the size of an array of hash passed as an argument of a sub.
Please consider the following code.
#!/usr/bin/perl -w use strict; sub rien2rien (@) { my @other = shift; for (my $i = 0; $i < @other; $i++) { my $a = $other[$i]{'first'}; my $b = $other[$i]{'second'}; print "in other sub:\t$a\t$b\n"; } } sub rien () { my $i = 0; my $file ="somefile.html"; my $regexp = "(.*)something(.*)"; open(FH, "<$file"); my @rrayOfHash; (my @data) = split(/<br>/, <FH>); foreach my $tuff (@data) { if ($tuff =~ /$regexp/) { $rrayOfHash[$i]{'first'} = $1; $rrayOfHash[$i]{'second'} =$2; $i++; } } for ($i = 0 ;$i <@data; $i++) { my $a = $rrayOfHash[$i]{'first'}; my $b = $rrayOfHash[$i]{'second'}; print "in sub: \t$a\t$b\n"; } return @rrayOfHash; } my @ut = &rien(); for (my $i = 0; $i < @ut; $i++) { my $a = $ut[$i]{'first'}; my $b = $ut[$i]{'second'}; print "in main:\t$a\t$b\n"; } &rien2rien(@ut);
the code produce this output :
in sub: bla bleu in sub: bli blo in main: bla bleu in main: bli blo in other sub: bla bleu
i imagine it may be a context question but i can't manage how to retrieve the whole array in the "other sub".

Thanks in advance for your help

Replies are listed 'Best First'.
Re: size of an array of hash
by ikegami (Patriarch) on Oct 04, 2005 at 01:16 UTC
    GrandFather showed you how to pass the array. However, it would be more efficient if a reference to the array was passed:
    &rien2rien(\@ut); . . . sub rien2rien { my $other = shift; for (my $i = 0; $i < @$other; $i++) { my $a = $other->[$i]{'first'}; my $b = $other->[$i]{'second'}; print "in other sub:\t$a\t$b\n"; } }

    Other problems:

    1) $a and $b have special meanings for sort. Using them as normal variables can cause unexpected problems. Use other names instead.

    2) You're using prototypes (e.g. sub rien2rien(@)), which is a bad idea.

    3) You use the ampersand notation for calling functions (e.g. &rien2rien(@ut) ) which overrides the prototype, also a bad idea.

    4) You're using a C-style for loop, which is much less readable and no more efficient than a Perl-style for loop, or even a foreach loop:

    sub rien2rien { my $other = shift; foreach (@$other) { my $var1 = $_->{'first'}; my $var2 = $_->{'second'}; print "in other sub:\t$var1\t$var2\n"; } }
Re: size of an array of hash
by GrandFather (Saint) on Oct 04, 2005 at 00:08 UTC

    If you change my @other = shift; (which simply copies the first parameter to the sub into @other) to my @other = (@_); which generates an array containing all the parameters passed in and assigns that to @other.

    Using the line "firstsomethingsecond
    nextsomethinganother
    " as the data the test code generates:

    in sub: first second in sub: next another in main: first second in main: next another in main: in other sub: first second in other sub: next another in other sub:

    Perl is Huffman encoded by design.
Re: size of an array of hash
by GrandFather (Saint) on Oct 03, 2005 at 23:37 UTC

    I suggest you change the <FH> to a <DATA>, comment out the open line, and provide some sample data for test purposes.


    Perl is Huffman encoded by design.