A
Alf McLaughlin
Hello all-
Please look at the code at the code listed below. It produces the
result also listed below. My question is, I have absolutely no idea
why the values in my original array are changing when I perform
operations on the second array. I am totally stumped... I do not
understand why the @match array seems to share the same memory address
as @array. Any help is much appreciated!
Thanks, Alf
THE RESULT:
In match array:
===============
70 c
75 d
77 e
88 f
In original array:
==================
4 a
6 b
700000 c
750000 d
770000 e
880000 f
99 g
100 h
105 i
200 j
300 k
THE CODE:
#!/usr/bin/perl
MAIN: {
my $ra_array = get_array();
my $low_range = 68;
my $high_range = 90;
my @match = grep {my $i = $_; $i->[0] >= $low_range and $i->[0] <=
$high_range} @$ra_array;
print "In match array:\n";
print "===============\n";
foreach my $i (@match) {
print "$i->[0]\t$i->[1]\n";
$i->[0] *= 10000;
}
print "In original array:\n";
print "==================\n";
foreach my $i (@$ra_array) {
print "$i->[0]\t$i->[1]\n";
}
}
sub get_array {
my @array; #= (4, 6, 70, 75, 77, 88, 99, 100, 105, 200, 300);
$array[0] = [(4, 'a')];
$array[1] = [(6, 'b')];
$array[2] = [(70, 'c')];
$array[3] = [(75, 'd')];
$array[4] = [(77, 'e')];
$array[5] = [(88, 'f')];
$array[6] = [(99, 'g')];
$array[7] = [(100, 'h')];
$array[8] = [(105, 'i')];
$array[9] = [(200, 'j')];
$array[10] =[(300, 'k')];
return \@array;
}
Please look at the code at the code listed below. It produces the
result also listed below. My question is, I have absolutely no idea
why the values in my original array are changing when I perform
operations on the second array. I am totally stumped... I do not
understand why the @match array seems to share the same memory address
as @array. Any help is much appreciated!
Thanks, Alf
THE RESULT:
In match array:
===============
70 c
75 d
77 e
88 f
In original array:
==================
4 a
6 b
700000 c
750000 d
770000 e
880000 f
99 g
100 h
105 i
200 j
300 k
THE CODE:
#!/usr/bin/perl
MAIN: {
my $ra_array = get_array();
my $low_range = 68;
my $high_range = 90;
my @match = grep {my $i = $_; $i->[0] >= $low_range and $i->[0] <=
$high_range} @$ra_array;
print "In match array:\n";
print "===============\n";
foreach my $i (@match) {
print "$i->[0]\t$i->[1]\n";
$i->[0] *= 10000;
}
print "In original array:\n";
print "==================\n";
foreach my $i (@$ra_array) {
print "$i->[0]\t$i->[1]\n";
}
}
sub get_array {
my @array; #= (4, 6, 70, 75, 77, 88, 99, 100, 105, 200, 300);
$array[0] = [(4, 'a')];
$array[1] = [(6, 'b')];
$array[2] = [(70, 'c')];
$array[3] = [(75, 'd')];
$array[4] = [(77, 'e')];
$array[5] = [(88, 'f')];
$array[6] = [(99, 'g')];
$array[7] = [(100, 'h')];
$array[8] = [(105, 'i')];
$array[9] = [(200, 'j')];
$array[10] =[(300, 'k')];
return \@array;
}