6

I am starting to learn Perl and this is just a basic little for loop for which I get a strange output and was hoping for some clarity on this.

@numbers = {1,4,5,6,7,8,9};
for(my$i = 0; $i<=$#numbers; $i++)
{
    print ("$numbers[$i}\n");
}

the output is HASH(0x23a09c).

What does this actually mean and why do I get this result.

regards Arian

smci
  • 32,567
  • 20
  • 113
  • 146
Arianule
  • 8,811
  • 45
  • 116
  • 174
  • 1
    Your example has a syntax error. Make sure you post what you are actually using. – Mat Nov 06 '11 at 10:19

3 Answers3

19

You want this:

@numbers = (1,4,5,6,7,8,9);
foreach my $number (@numbers)
{
    print ("$number\n");
}

With {1,4,5,6,7,8,9} you're actually creating a reference to an anonymous hash containing the key value pairs (1 => 4, 5 => 6, 7 => 8, 9 => undef). When you write @numbers = {1,4,5,6,7,8,9}; that reference becomes the sole scalar stored in the @numbers array.

Furthermore, if you just want to iterate over the elements, no need to use the "classic" style with a counter.

You can do:

for my $number (1 .. 9) {
    print "$number\n";
}

Make sure you have use strict; and use warnings; at the beginning of every Perl script you write. Those directives enable perl to catch errors and warn about certain possibly erroneous code. As a beginner, you might want to couple those with warnings with diagnostics to get more detailed information.

These are very handy, especially when starting out with Perl as they help you prevent shooting yourself in the foot.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
pcalcao
  • 15,789
  • 1
  • 44
  • 64
  • Thanks allot. The braces was interpreted as a hash. so used to using braces when declaring arrays. – Arianule Nov 06 '11 at 10:28
  • No prob :) I'm still struggling with Perl a bit myself. Please accept if it solves your problem. Thanks – pcalcao Nov 06 '11 at 10:32
6

What you're doing there is creating an array with a hash ref at index 0, i.e.:

$numbers[0] = {
    1 => 4,
    5 => 6,
    7 => 8,
    9 => undef,
};

If you had used strict you would have seen:

Global symbol "@numbers" requires explicit package name

And warnings would have told you:

Odd number of elements in anonymous hash

Start all your perl scripts with something like:

#!/usr/bin/perl -w
use strict;

(alternatively use warnings;) and you'll always be able to catch these kinds of errors right away. Even seasoned Perl programmers make them from time to time. There's really never any good reason to leave them out (unless you're golfing that is).

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
flesk
  • 7,439
  • 4
  • 24
  • 33
1

try:

@numbers = (1,4,5,6,7,8,9);
foreach(@numbers) {
    print $_;
}
  • Have you tried that with the original poster's code? Why not? The problem is not the loop statement, but the type of the variable. – Jens Nov 06 '11 at 10:55
  • there were two problems. one in the loop itself, one with the declaration. my answer focused on the loop and what shorter alternatives there are. thanks for the comment though, I've edited my post! – interphase27 Nov 06 '11 at 11:07