Exemple de atribuire de scalari din array-uri

103

Secvenţă cod (tst25.pl)

 

Rezultat Ón consola text

@array = (5, 7, 11);

($var1, $var2) = @array;

print ("var1 = $var1 var2= $var2\n");

 

var1 = 5 var2= 7

($var1, $var2, $var3) = ("one", "two", "three");

print ("var1 = $var1 var2= $var2 var3= $var3\n");

 

var1 = one var2= two var3= three

Aflarea lungimii unui array

Secvenţă cod (tst25.pl)

 

Rezultat Ón consola text

@array = (5, 7, 11);

$length=@array;

print("$length\n");

 

3

 

Un program care tipăreşte toate elementele unui array

 

Secvenţă cod (tst26.pl)

 

Rezultat Ón consola text

@array = (14, "cheeseburger", 1.23, -7, "toad");

$count = 1;

while ($count <= @array) {

     print("element $count: $array[$count-1]\n");

     $count++;

}

 

element 1: 14

element 2: cheeseburger

element 3: 1.23

element 4: -7

element 5: toad

 

Accesarea unor porţiuni de array-uri

               Secvenţe cod care ilustrează cum se extrage o bucată de array sunt următoarele.

Secvenţă cod (tst27.pl)

 

Rezultat Ón consola text

@array = (1, 2, 3, 4);

@subarray = @array[1,2];

print ("The first element of subarray is $subarray[0]\n");

print ("The second element of subarray is $subarray[1]\n");

 

The first element of subarray is 2

The second element of subarray is 3

@array = (1, 2, 3, 4, 5, 6);

@subarray = @array[1..4];

print(@subarray,"\n");

 

2345

@array = (1, 2, 3, 4, 5, 6);

$endrange = 3;

@subarray = @array[1..$endrange];

print(@subarray,"\n");

 

234

@array = ("one", "two", "three", "four", "five", "six", "seven", "eight");

@range = (1, 3..5, 7);

@subarray = @array[@range];

print ("The array slice is: @subarray\n");

 

The array slice is: two four five six eight

@array = ("old1", "old2", "old3", "old4");

@array[1,2] = ("new2", "new3");

print ("@array\n");

 

old1 new2 new3 old4

@array = ("one", "two", "three", "four", "five");

@array[1..3] = @array[2..4];

print ("@array\n");

 

one three four five five

© Cornel Mironel Niculae, 2003-2004

13-Nov-2009