Tuesday, 11 March 2014

Selecting Elements from Lists



Selecting Elements from Lists

The list notation is identical to that for arrays - you can extract an element from an array by appending square brackets to the list and giving one or more indices:
#!/usr/bin/perl
 
$var = (5,4,3,2,1)[4];
 
print "value of var = $var\n"
This will produce following result:
value of var = 1
Similarly, we can extract slices, although without the requirement for a leading @ character:
#!/usr/bin/perl
 
@list = (5,4,3,2,1)[1..3];
 
print "Value of list = @list\n";
This will produce following result:
Value of list = 4 3 2

No comments: