Friday, December 2, 2011

Perl - [ $perl -e ' Command Line ' ] - [ Shift / Unshift / Pop / Push ]


Note: Open shell and type the code "command line" below!
Syntax : $ perl -e ' command line '

1) Profile example 

$ perl -e '@array = (1..5); print "Result : @array \n";'
Result : 1 2 3 4 5 


2) Removes the first element of the array
$ perl -e '@array = (1..5); shift (@array);print "Result : @array \n";'
Result : 2 3 4 5 


3) Remove the last element of the array
$ perl -e '@array = (1..5); pop (@array);print "Result : @array \n";'
Result : 1 2 3 4 

4) Insert the last element of the array
$ perl -e '@array = (1..5); push (@array,"command line");print "Result : @array \n";'
Result : 1 2 3 4 5 command line 

5) Insert at the beginning of the array
$ perl -e '@array = (1..5); unshift (@array,"command line");print "Result : @array \n";'
Result : command line 1 2 3 4 5


Note: Always use push and pop instead of shit and unshift when possible, for push and pop are more efficient with large array

0 comentários:

Post a Comment