Reputation: 37
I am attempting write code that will allow me to print a list of arrays in Perl.
I have a list of arrays of GPS coordinates called @points
that is formatted with arrays of lat/lon like so:
@points = [34.2,17.5],[-64.1, 110.35],[54.8,77.1], and so on and so forth
I am able to print the list itself using print "$_\n" for @points;
, however this only gives me the memory reference for the array. I know that you can't just print arrays like you can strings or integers however I am stuck on how to actually iterate through the list and print the integers. I tried using a foreach loop and @_ to print at each increment however that didn't work for me either. If anyone is able to give me any direction on this it would be greatly appreciated.
Upvotes: 0
Views: 465
Reputation: 1093
One More solution for this, although I changed @points
to reference
.
use strict;
use warnings;
my $points = [[34.2,17.5],[-64.1, 110.35],[54.8,77.1]];
for(my $i = 0; $i<=6; $i++)
{
print $points->[$i]->[0];
print "\n";
print $points->[$i]->[1];
print "\n";
}
Upvotes: 0
Reputation: 13792
You may use the Data::Dumper CPAN module, it's simple:
use strict;
use warnings;
use Data::Dumper;
my @points = ( [34.2,17.5], [-64.1, 110.35], [54.8,77.1] );
$Data::Dumper::Terse = 1;
print Dumper(\@points), "\n";
#---> prints:
[
[
'34.2',
'17.5'
],
[
'-64.1',
'110.35'
],
[
'54.8',
'77.1'
]
]
Upvotes: 1
Reputation: 53478
You're doing two dimensional iteration so you need a two dimensional loop. The way perl does two dimensional data structures is via references.
In your example - @points
isn't a 2d structure, it's a list of references - which is why if you print it you get:
print "@points";
Gives:
ARRAY(0x4a6dec) ARRAY(0x4a6fb4) ARRAY(0x12fef64)
So you need to dereference each of those elements. There's several ways to do this, but essentially @{$reference}
is how it works.
There are a few ways of accomplishing the task, but essentially:
foreach my $group ( @points ) {
print join ("\n", @$group ),"\n";
}
For example - join is one iterate, foreach is another. (Note - @$group
and @{$group}
mean the same thing, so you don't need the braces. If you were trying to do @$group[1] you would need it, to disambiguate whether you were after @{$group[1]}
or @{$group}[1]
)
Or for another:
use feature qw ( say );
say for map { @$_ } @points;
This is making use of the fact that map
is applying a transform to each element of @points
, and that "transform" is simply "dereference and give me the elements".
Of course, then you have 'smashed flat' @points
and not retained their mutual association.
Upvotes: 0
Reputation: 61512
You need to use the @{ ... }
sigil to dereference your array references. This will give you a plain array that you can print directly or manipulate with the join
builtin to get better formatted output:
use strict;
use warnings;
my @points = ([34.2,17.5],[-64.1, 110.35],[54.8,77.1]);
print join(',', @$_), "\n" foreach my @points;
output:
34.2,17.5
-64.1,110.35
54.8,77.1
You should note that I placed parentheses around your definition of the @points
array:
my @points = ([34.2,17.5],[-64.1, 110.35],[54.8,77.1]);
these parentheses are required due to how lists are constructed in Perl, without them present @points
would only be assigned the final array reference of [54.8,77.1]
Upvotes: 1