Simple array iteration not working?

B

brian.haines

This code:

my @sref = @{$softwareListRef};
print Dumper(@sref);

for (my $i = 0; $i<@sref ; $i++) {
my $sn = $sref[$i];
print Dumper($sn);

Generates this output:

$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];

Note that the array @sref is properly de-referenced and printed. Then,
inexplicably, the array item is selected by index and when printed, is
equal to the original array. Foreach does the same thing. What's going
on?
 
J

John W. Krahn

This code:

my @sref = @{$softwareListRef};
print Dumper(@sref);

for (my $i = 0; $i<@sref ; $i++) {
my $sn = $sref[$i];
print Dumper($sn);

Generates this output:

$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];

Note that the array @sref is properly de-referenced

@sref is not dereferenced in your example. And you can't dereferenced
an array, only a scalar. In your example $softwareListRef is dereferenced.

and printed. Then,
inexplicably, the array item is selected by index and when printed, is
equal to the original array. Foreach does the same thing. What's going
on?



John
 
B

brian.haines

Thanks for pointing out the incorrect problem description. You're
right $softwareListRef is de-referenced, not @sref.

Any ideas why this would be happening? A similar block of code in the
next function in the same file works.
 
K

kj

In said:
This code:
my @sref = @{$softwareListRef};
print Dumper(@sref);
for (my $i = 0; $i<@sref ; $i++) {
my $sn = $sref[$i];
print Dumper($sn);
Generates this output:
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
Note that the array @sref is properly de-referenced and printed. Then,
inexplicably, the array item is selected by index and when printed, is
equal to the original array. Foreach does the same thing. What's going
on?


I'm not sure what you expect. In your code, @sref is an array
containing *exactly one element* that happens to be an arrayref,
pointing to an array of two elements. As far as Dumper is concerned,
Dumper(@sref) is no different from Dumper($sref[0]).

Put this right before the code you showed:

$softwareListRef = $softwareListRef->[0];

and see if the resulting code behaves more along the lines of what
you want. If so, this means that the way you are constructing
$softwareListRef is incorrect (you have one extra level of
referencing).

kj



--
 
S

sln

This code:

my @sref = @{$softwareListRef};
print Dumper(@sref);

for (my $i = 0; $i<@sref ; $i++) {
my $sn = $sref[$i];
print Dumper($sn);

Generates this output:

$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];

Note that the array @sref is properly de-referenced and printed. Then,
inexplicably, the array item is selected by index and when printed, is
equal to the original array. Foreach does the same thing. What's going
on?

This is right, @sref contains a single ref to
[
'autoTestSoftware001',
'autoTestSoftware050'
]
then dumped, then dumped again in the for loop, thats why VAR1,VAR1..
Below is using a ref to a double element array of ref's:

use strict;
use warnings;
use Data::Dumper;

my @ar1 = ('autoTestSoftware001','autoTestSoftware050');
my @ar2 = ('autoTestSoftware051','autoTestSoftware100');
my $softwareListRef = [\(@ar1, @ar2)];

# same as:
# my $softwareListRef = [
# ['autoTestSoftware001','autoTestSoftware050'],
# ['autoTestSoftware051','autoTestSoftware100'],
# ];

my @sref = @{$softwareListRef};
print Dumper(@sref);

for my $sn (@sref) {
print Dumper($sn);
}

__END__

$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR2 = [
'autoTestSoftware051',
'autoTestSoftware100'
];
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR1 = [
'autoTestSoftware051',
'autoTestSoftware100'
];

-sln
 
S

sln

This code:

my @sref = @{$softwareListRef};
print Dumper(@sref);

for (my $i = 0; $i<@sref ; $i++) {
my $sn = $sref[$i];
print Dumper($sn);

Generates this output:

$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];

Note that the array @sref is properly de-referenced and printed. Then,
inexplicably, the array item is selected by index and when printed, is
equal to the original array. Foreach does the same thing. What's going
on?

This is right, @sref contains a single ref to
[
'autoTestSoftware001',
'autoTestSoftware050'
]

The only way to get this output is if

$softwareListRef = [
[
'autoTestSoftware001',
'autoTestSoftware050'
]
];

Where Dumper @sref prints the single 'element' in the array,
which is the ref ['autoTestSoftware001','autoTestSoftware050']

Then Dumper $sref[0] which is also the same 'element' which
is the same ref.

You have done basically the same operation.

If you had coerrced the element as an array @{$sref[0]},
its contents would have been printed without the [] and as

$VAR1 = 'autoTestSoftware001';
$VAR2 = 'autoTestSoftware050';


-sln
 
S

sln

This code:

my @sref = @{$softwareListRef};
print Dumper(@sref);

for (my $i = 0; $i<@sref ; $i++) {
my $sn = $sref[$i];
print Dumper($sn);

Generates this output:

$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];
$VAR1 = [
'autoTestSoftware001',
'autoTestSoftware050'
];

Note that the array @sref is properly de-referenced and printed. Then,
inexplicably, the array item is selected by index and when printed, is
equal to the original array. Foreach does the same thing. What's going
on?

This is right, @sref contains a single ref to
[
'autoTestSoftware001',
'autoTestSoftware050'
]

The only way to get this output is if

$softwareListRef = [
[
'autoTestSoftware001',
'autoTestSoftware050'
]
];

Where Dumper @sref prints the single 'element' in the array,
which is the ref ['autoTestSoftware001','autoTestSoftware050']

Then Dumper $sref[0] which is also the same 'element' which
is the same ref.

You have done basically the same operation.

If you had coerrced the element as an array @{$sref[0]},
its contents would have been printed without the [] and as

$VAR1 = 'autoTestSoftware001';
$VAR2 = 'autoTestSoftware050';

Sorry about that, one more thing. If you pass an @array
to Dumper it will print its contents. If you pass a $scalar
to Dumper it will print its contents. Otherwise,
there is no way to tell what it is.

So if an array reference, it will be shown with brackets with its contents expanded.
Identical if its an array with a single element array reference, which is what you
found in your case.
All content are recursively expanded.

-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top