EverEffects Backup Forum
Welcome To We Can't Program Forums, Please Log In To Access All Forums.

Enjoy Your Stay

- Staff

EverEffects Backup Forum

Designers World
HomePortalMemberlistRegisterLog in

 Array variables Resize

Share | 

 Array variables

View previous topic View next topic Go down 

Posts : 33
Join date : 2010-05-27
Age : 26
Location : Oklahoma

Array variables _
PostSubject: Array variables   Array variables EmptyThu Jun 03, 2010 5:14 pm

Today i will show about array variables. Now array variables are like scalars variables because they both have the same format except they are prefixed by an "@" symbol. Now before you proceed, first look for the topic called "Scalar variables - Perl" section and "Control structures - Perl" section, then you can proceed to this tutorial. Also i have made a topic on this section called "Perl Download file", so look at that and lets begin.

The Basic Variables of Arrays

Here is a basic statement of mines:

@food  = ("apples", "pears", "eels");
@music = ("whistle", "flute");

The statement assigns a three element list to the array variable @food and a two element list to the array variable @music.

The array is accessed by using in dices starting from 0, and square brackets are used to specify the index. The expression returns eels. Notice that the @ has changed to a $ because eels is a scalar. Here is a example:


Array Assignments

As in all of Perl, the same expression in a different context can produce a different result. The first assignment below explodes the @music variable so that it is equivalent to the second assignment. Here is the following example:

@moremusic = ("organ", @music, "harp");
@moremusic = ("organ", "whistle", "flute", "harp");

This should suggest a way of adding elements to an array. A neater way of adding elements is to use the statement which pushes eggs onto the end of the array @food. Here is a following example:

push(@food, "eggs");

Now to push two or more items onto the array use one of the following forms:

push(@food, "eggs", "lard");
push(@food, ("eggs", "lard"));
push(@food, @morefood);

The push function returns the length of the new list.

Ok now from our original list the pop function returns eels and @food now has two elements. To remove the last item from a list and return it use the pop function:

$grub = pop(@food);   # Now $grub = "eels"

It is also possible to assign an array to a scalar variable. As usual context is important.

$f = @food;

The line assigns the length of @food

$f = "@food";

But turns the list into a string with a space between each element. This space can be replaced by any other string by changing the value of the special $" variable. This variable is just one of Perl's many special variables, most of which have odd names.

Also arrays can be used to make multiple assignments to scalar variables:

($a, $b) = ($c, $d);      # Same as $a=$c; $b=$d;
($a, $b) = @food;      # $a and $b are the first two
            # items of @food.
($a, @somefood) = @food;   # $a is the first item of @food
            # @somefood is a list of the
            # others.
(@somefood, $a) = @food;   # @somefood is @food and
            # $a is undefined.

The last assignment occurs because arrays are greedy, and @somefood will swallow up as much of @food as it can. Therefore that form is best avoided.

Finally, you may want to find the index of the last element of a list. To do this for the @food array use the expression:


Displaying Arrays

Since context is important, it shouldn't be too surprising that the following all produce different results:

print @food;   # By itself
print "@food";   # Embedded in double quotes
print @food."";   # In a scalar context

Here is a exercise for you, try out each of the above three print statements to see what they do.
Back to top Go down

Array variables

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
EverEffects Backup Forum :: Perl-
Jump to:  

   [Valid RSS]

Create a forum on Forumotion | © phpBB | Free forum support | Contact | Report an abuse | Forumotion.com