G
G Klinedinst
Hi all. I have a question I have been pulling my hair out all day
over. Can someone tell me what is happening in the following code. The
first print statements create the results I expect. The subroutines
statments print arrays with 1 element but not data in [0]. What gives?
I am passing by reference and then dereferencing in the sub. Can you
point me towards what I am missing? I have looked at the perlfaqs and
read the perldocs on references but it I can't find what I am looking
for. It could also be my sleep deprived mind is seeing it but not
grokking it. TIA.
-Greg
***********CODE****************
#!/usr/local/bin/perl
use strict;
use warnings;
my @arr1;
my @arr2;
$arr1[0] = 1;
print( $arr1[0] . ":" . scalar( @arr1 ) . "\n" );
print( $arr2[0] . ":" . scalar( @arr2 ) . "\n" );
print( "\n\n" );
test( \@arr1, \@arr2 );
sub test
{
my @subArr1 = @$_[0];
my @subArr2 = @$_[1];
print( $subArr1[0] . ":" . scalar( @subArr1 ) . "\n" );
print( $subArr2[0] . ":" . scalar( @subArr2 ) . "\n" );
}
**************/CODE***********
**************OUTPUT*********
1:1
Use of uninitialized value in concatenation (.) or string at
../arrays.pl
line 12.
:0
Use of uninitialized value in concatenation (.) or string at
../arrays.pl
line 22.
:1
Use of uninitialized value in concatenation (.) or string at
../arrays.pl
line 23.
:1
*************/OUTPUT**********
over. Can someone tell me what is happening in the following code. The
first print statements create the results I expect. The subroutines
statments print arrays with 1 element but not data in [0]. What gives?
I am passing by reference and then dereferencing in the sub. Can you
point me towards what I am missing? I have looked at the perlfaqs and
read the perldocs on references but it I can't find what I am looking
for. It could also be my sleep deprived mind is seeing it but not
grokking it. TIA.
-Greg
***********CODE****************
#!/usr/local/bin/perl
use strict;
use warnings;
my @arr1;
my @arr2;
$arr1[0] = 1;
print( $arr1[0] . ":" . scalar( @arr1 ) . "\n" );
print( $arr2[0] . ":" . scalar( @arr2 ) . "\n" );
print( "\n\n" );
test( \@arr1, \@arr2 );
sub test
{
my @subArr1 = @$_[0];
my @subArr2 = @$_[1];
print( $subArr1[0] . ":" . scalar( @subArr1 ) . "\n" );
print( $subArr2[0] . ":" . scalar( @subArr2 ) . "\n" );
}
**************/CODE***********
**************OUTPUT*********
1:1
Use of uninitialized value in concatenation (.) or string at
../arrays.pl
line 12.
:0
Use of uninitialized value in concatenation (.) or string at
../arrays.pl
line 22.
:1
Use of uninitialized value in concatenation (.) or string at
../arrays.pl
line 23.
:1
*************/OUTPUT**********