Tutorial Addendum on Perl - Allotment A - perldata - Perl Data Types
| |
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.
|
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, |
Also see ...
This affiliate describes:How to cover antecedent codes from additional files.How to ascertain and use name spaces.How to ascertain appropriate subroutines for accumul
The calling program, IncRequireTest.pl, IncRequireTest.pl Absorb (c) 1995 by Dr. Yang print(" Testing require()... "); require("MyRequireLib.inc"); require("MyRequireL
Special Subroutines for Accumulation and Beheading ProcessesIn a Perl antecedent cipher file, you can ascertain 4 appropriate subroutines, which will be accomplished automatically by the ac
Defining and Using Perl ModulesPerl Bore A appropriate antecedent cipher book that:Contains variables and subroutines accompanying to a individual subject./l
This affiliate describes:How to adjure subroutines as chic methods.How to adjure subroutines as item methods.How to make objects.How to make it
Output: Test 1: Param 1 = Jan Param 2 = FebTest 2: Param 1 = Apple Param 2 = BananaTest 3: Param 1 = One Param 2 = Twob
I acclimated both syntaxes in the followiwng sample program, ObjectMethodTest.pl: ObjectMethodTest.pl Absorb (c) 1999 by Dr. Yangpackage Foo;sub echoParam { $i = 0;
ObjectVariableTest.pl Absorb (c) 1999 by Dr. Yangpackage Account; $euroRate = 0.85;sub book { my $this = shift; my $currency = shift; my $balance = $$this{"Balance"};p
AccountClassTest.pl Absorb (c) 1999 by Dr. Yangpackage Account; $euroRate = 0.85;sub new { my $class = shift; $this = {}; $$this{"Name"} = shift; $$this{"Type"} =
This affiliate describes:What are allegorical (soft) references and how to use them.How to make harder references.How to use harder references./ul