Friday, February 4, 2011

PERL : Array Functionas

pico arrayfunctions1.pl
#!/usr/bin/perl -w
user strict;
my $firstname = "Mark";
my $midlename = "Middle";
my $lastname = "LastName";
my @array1=("1","2","3");#for integers we don't need ()
print @array1\n";
#pop function removes the last elem from an array
my $ppop = pop @array1;
print "@array1\n";#Results is 1 2
#push - populates at the end a value to an array
print $ppop;#results is 3
push @array1, $ppop;
print "@array1\n";#Results is 1 2 3
#unshift - insert at the first element
unshift (@array1, "$firstname");#add contents of $firstname to the first element of array1
print @array1; #Results -> Mark 1 2 3
unshift (@array1, "$firstname", "$midlename", "$lastname");#Results -> Mark MiddleName lastname 1 2 3
# shift - removes the first element from the array
shift @array1;
print @array1;#Results -> MiddleName lastname 1 2 3
# sort is good to use with arrays
my @array1 = ("New York","New Jersey","Conneticut");
my @array1 = sort @array1; # Result ->Conneticut New Jersey New York
#end

No comments:

Post a Comment