packetstormer has asked for the wisdom of the Perl Monks concerning the following question:

I am working my through Perl and grasping some of it! The code below has me puzzled though, can anyone explain it:
for($i = 1; $i <= $ItemCount; $i+=1) { print "Item $i is $ItemList[$i][3]\n", " Name: $ItemList[$i][0]\n", " Amount: $ItemList[$i][1]\n", " Process: $ItemList[$i][2]\n\n"; }
I understand the "for loop" fine. It is the next part that puzzles me. I know the format $itemlist[x] refers to position x in the array @itemlist but what does the $ItemList[$i][0] refer to? Basically, what is that $i doing in there! Thanks

Replies are listed 'Best First'.
Re: Newbie: 'For loops'
by chrestomanci (Priest) on Nov 30, 2010 at 15:02 UTC

    Short answer: $ItemList[$i][0] refers to an entry in a two dimensional array.

    For loops as you have written are not frequently used by perl programmers. (It makes you look as if you have just come from C). Instead we tend to use foreach loops because they are clearer, and avoid some types of bugs (Such as getting the index wrong and jumping off the end of the array). Re-writing your snippet with a foreach loop we get:

    my $i=0; foreach my $Item ( @itemList ) { print "Item $i is $Item[3]\n", " Name: $Item[0]\n", " Amount: $Item[1]\n", " Process: $Item[2]\n\n"; $i++; }
      $Item is not an array, its a reference, so you have to dereference, like  $$Item[0] or
      printf "Item %s is %s\n Name: %s\n Amount: %s\n Process: %s\n\n", $i, @{$Item}[3,0,1,2];
      see References quick reference
      Excellent! That makes much more sense. Thanks......I'll be back!
Re: Newbie: 'For loops'
by locked_user sundialsvc4 (Abbot) on Nov 30, 2010 at 16:32 UTC

    “References” are a key concept in Perl.   They are “a single, scalar thing” that refers to something else ... such as an array or a hash.   Furthermore, it is quite common that the array or hash thus-referenced has no other existence except that some variable(s) refer to it.   (It is an “anonymous” value.)   They are vaguely like pointers in C, but much more refined:   when you use one, unlike a C pointer, you’re not pointing a loaded revolver in the general direction of your foot.

    Spending a little time with the Perl tutorials, such as perldoc perl and other docs referenced thereby such as perldoc perlintro, will help enormously in “getting a feel for the Perl way of doing things,” which is always-elegant but not-always-quite obvious at times.   Since you already know other programming languages, the time of disorientation and confusion will very quickly pass.   Just don’t try to apply too directly, too literally, what you know from those other languages.   One of the mantras of Perl is TMTOWTDI:   “There’s More Than One Way To Do It.™”   It takes a little time to realize just how important, and how pervasive, that notion really is around these parts.   At first, you might find it curious – even a little-bit quaint.   But there is a deep reason for it, as you will see.

Re: Newbie: 'For loops'
by jffry (Hermit) on Nov 30, 2010 at 15:37 UTC

    Even though we don't use C-style for loops unless necessary, the use of multidimensional arrays is normal. The array indexing syntax you have used is acceptable in Perl and it imitates C syntax. This leads me to believe that you don't understand the concept of multidimensional arrays and perhaps even the possibility of using variables to specify the indices of an array.

    Based on my conclusion, I suggest you read about multidimensional arrays in general.

    Also, in Perl, multidimensional arrays are really arrays of references to arrays, so you'll find information on Perl's multidimensional arrays not in the data type documentation, but in the Perl references documentation.

Re: Newbie: 'For loops'
by Anonymous Monk on Nov 30, 2010 at 15:00 UTC
    The same thing the 0/1/2 is doing, its a multi dimensional array, an array of arrays, see Tutorials