[Please respond to the mailing-list so that others can contribute and learn from the discussion. Thanks.]

On 12/25/2003 10:31 AM, Duong Nguyen wrote:
Hello,

Thanks for your reply. I think I am a bit confused between using array slices and a particular element of an array. The "print" function prints out the same value for both cases. Do I use the latter when I want to deal with arithmetic operations (scalar)?

Thanks,
Duong

The simple explanation is that, when you want to refer to a single element of an array, the left-most symbol should be a '$'; the '$' indicates to perl that you want to access a single (scalar) value. If you want to access multiple elements of an array (or hash), you would use an array slice (left-most symbol would be '@'); the '@' indicates to perl that you want to access a _list_ of 1 or more elements as a group.


Randy.


----- Original Message ----- From: Randy W. Sims To: Duong Nguyen Cc: [EMAIL PROTECTED] Sent: Thursday, December 25, 2003 3:02 AM
Subject: Re: Perl Help: Array Manipulation



On 12/25/2003 2:51 AM, Duong Nguyen wrote:


> From: Randy W. Sims >
> On 12/25/2003 12:59 AM, Duong Nguyen wrote:
> > Hello everyone,
> > > > Sorry for the spam, I am new to Perl and am having a hard time manipulating arrays. Below is the sample code that I am working on:
> > > > @array1[0..5] = 1;
> > @total[0] = 0;
> > > > for($i=0; $i<4; $i++)
> > {
> > if($i == 0)
> > {
> > @total[$i] = @array1[$i];
> > print @total[$i];
> > }
> > else
> > {
> > $j = $i - 1;
> > @total[$i] = @total[$j] + @array1[$i];
> > print @total[$i];
> > }
> > }
> > > > This code would work in C/C++, but with Perl, instead of adding up the previous values, the array "Total" seems to treat the integer value as a string and join them together. So instead of ending up with 4 as my resulting total, I instead get 1111. I know this is a rather dumb question, but any help would be GREATLY appreciated. Thanks in advance.
> > hint #1: Add the following to the beginning of your program
> > use strict;
> use warnings;
> > hint #2: Do you know the difference between '@total[$i]' and '$total[$i]' ?
> >
> Every time I use "strict" my program goes all crazy on me with the global and local variables. So instead, I just comment that out.


That craziness suggests opportunities to learn. It really is easier to debug when you get in the habit of using those pragmas. It prevents a lot of headache.

> The difference between '@total[$i]' and "@total[$i]" is that with the single quote, what u have there is what gets printed out. With double quote, the element of the array gets printed out.

No, look at the first character '@total[0]' vs '$total[0]' (@ vs $).
The first refers to an array (slice) with one element. The second refers to one element of an array.


BTW, wrt your other reply to the group, the initialization syntax you want is:

@array[0..5] = (1) x 6;

  Regards,
  Randy.





--
A little learning is a dang'rous thing;
Drink deep, or taste not the Pierian spring;
There shallow draughts intoxicate the brain;
And drinking largely sobers us again.
                - Alexander Pope


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to