Tutorial Addendum on Perl - Allotment A - perldata - Perl Data Types

 31 December 18:00   

    



    



    

List Amount Constructors

    



    

List amount constructors are acclimated to assemble account objects. A account item is about

    

identical to an arrangement object. It represents an ordered account of scalars indexed by

    

numbers starting with 0.

    



    

List altar are complete by advertisement alone scalars afar by commas. You

    

should aswell use parentheses to assure the architect as a individual assemblage in operations

    

to abstain confusions. If you do use parentheses, they you don t accept to put string

    

literals in quotes. Some acceptable and bad examples are:

    

in the afterward examples:

    

 

    

accost ; # ok, a individual scalar is aswell a list

    

3,5,7,11; # ok

    

Mon , Tue , Wed ; # ok

    

( Mon , Tue , Wed ); # ok

    

(Mon,Tue,Wed); # ok

    

Mon ,2, Angel ; # ok

    

Jan ,31, Feb ,28, Mar ,31; # ok

    

Jan Feb Mar ; # bad, charge commas to separate the elements

    



    



    

An alone scalar in a account item can be accessed by the cable notation,

    

[index]. In this case, you charge parentheses to assure the account object. Aswell note

    

that abrogating basis can be acclimated to calculation down from the endure entry.

    

If the basis is out of the range, you are accessing a amorphous scalar.

    

Here is a

    

sample program with account subscriptions:

    

 

    

#- ListObject.pl

    

#- Absorb (c) 1995 by Dr. Yang

    

#

    

print((3,5,7,11)[0], "
"); # 3

    

print(( accost )[0], "
"); # hello

    

print(( accost )[1], "
"); #

    

print(( accost )[-1], "
"); # hello

    

print(( Mon , Tue , Wed )[1], "
"); # Tue

    

print((Mon,Tue,Wed)[-1], "
"); # Wed

    



    



    

Once account altar are constructed, they can be acclimated in arrangement capricious appointment

    

operations as in the afterward examples:

    



    

 

    

@messages = accost ;

    

@primes = 3,5,7,11;

    

@vacations = Mon , Tue , Wed ;

    

@vacations = ( Mon , Tue , Wed );

    

@vacations = (Mon,Tue,Wed);

    

@menu = Mon ,2, Angel ;

    

@dates = Jan ,31, Feb ,28, Mar ,31;

    



    



    

List altar can aswell be acclimated in a assortment capricious appointment operation. In this

    

case, every two alone scalars will be commutual calm to anatomy a key string

    

and the associated scalar. If the endure scalar has no partner, it will become

    

a key with a amorphous scalar. Actuality are some examples:

    

 

    

%messages = accost ;

    

%primes = 3,5,7,11;

    

%vacations = Mon , Tue , Wed ;

    

%vacations = ( Mon , Tue , Wed );

    

%vacations = (Mon,Tue,Wed);

    

%menu = Mon ,2, Angel ;

    

%dates = Jan ,31, Feb ,28, Mar ,31;

    

%prices = ( Milk ,1.99, Coke ,0.99, Aliment ,1.49);

    



    



    

If a scalar item is acclimated area a account item is expected, the scalar item will

    

be adapted into a account item with individual scalar. If a account item is acclimated area a scalar object

    

is expected, the account item will be adapted into a scalar item with the

    

last scalar in the list.

    

 

    

#- ListToScalar.pl

    

#- Absorb (c) 1995 by Dr. Yang

    

#

    

print(1+(3,5,7,11), "
"); # 12

    

print(1+(Mon,Tue,Wed), "
"); # 1

    

print((sort 11), "
"); # 11

    



    



    

Of course, account altar can be acclimated in some additional operations. We will altercate them

    

in additional chapters.

    



    

 


Tags: types, individual, examples, single, index, object, print

 scalar, object, print, objects, examples, @vacations, %vacations, index, single, scalars, individual, parentheses, , mon tue, tue wed, list object, list objects, scalar object, %vacations mon, print hello, @vacations mon, undefined scalar here, perl data types, perldata perl data,

Share Tutorial Addendum on Perl - Allotment A - perldata - Perl Data Types:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  perl