Perl Tutorial Part 3:
So, lets manipulate this new script
with “for” and then try to find an easier way to write it over:
Let’s take a look at the script. The first actual line uses the qw operator to clearly introduce the array, and the second line does most of the work in the script, basically giving values and details so the script will run.
For instance, $index=0 means the $index scalar is starting at 0 and adding 1 until @Linuxflavors is made true and the end of @Linuxflavors is reached. The $index<@Linuxflavors means just that. $index has the value of 0. @Linuxflavors will assume the value of how many actual Linux distros are assigned to @Linuxflavors, which will be 5(ubuntu slackware fedora knoppix openSUSE), and finally the index++ just tells the program to add the increment of 1
^^In Perl, a language notorious for a lazy user base, that is too much typing for such a small result..There is a much easier way to write this script by introducing the “foreach” statement. Now foreach and for are in close relation to each other and interrelate a lot, so keep that in mind. A much easier way to have gone about this would be the script below:
And we have our output of both proggies:
h4x@slax:~/Desktop$ perl arraymanip.pl
My favorite Linux flavor is ubuntu and…My favorite Linux flavor is slackware and…My favorite Linux flavor is fedora and…My favorite Linux flavor is knoppix and…My favorite Linux flavor is openSUSE and…many others..
Up untill now, we have used the ‘qw operator’ for our arrays, and the qw operator works great with one worded scalars, but what if we were to have variables with 2 or more words involved? We would have to revert to the more traditional type list or array.
Examples of qw:
a) my $os=qw( Milk cigs gas oil ); # syntax works
b) my $os=qw( whole milk Coco puffs cereal Nat Sherman smokes gas and oil ); # syn
tax will fail.
Why? The syntax fails because the qw operator uses each word as a different scalar variable so it would recognize whole milk as two different scalars; whole and milk, and so on down the list. So our array would be seperated into 11 scalars, while only 5 were intended. How do we get around this? Easy: we ditch the qw operator and use a different array syntax like so:
my @list=( “whole milk”, “Coco puffs cereal”, “Nat Sherman smokes”, “gas and oil” ); # correct syntax
my @list=( ‘whole milk’, ‘Coco puffs cereal’, ‘Nat Sherman smokes’, ‘gas and oil’ ); # also correct syntax depending on what exactly is in our array and how “literal” we want to take it.
Let’s write an array script usign the above variables and syntax:
This is a good looping array script using our new “foreach” statement, and we ditched the qw operator, so we can use a more particular array syntax. Also we were able to add a new variable $list to (@list) which makes lists easier to manipulate.
h4x@slax:~/Desktop$ perl arraymanip5.pl
I really need some whole milk before it gets too late and..
I really need some Coco puffs cereal before it gets too late and..
I really need some Nat Sherman smokes before it gets too late and..
I really need some gas and oil before it gets too late and..
Ok, we now finally have:
Coco puffs cereal
Nat Sherman smokes
gas and oil
Whew, I feel so much better now that we have what we need
So, little by little we have learned to build, add, extract,and manipulate our arrays.. We have learned what the qw operator is used for and when it can and can’t be used in our arrays. Next we will go through some functions.
16) Functions to manipulate arrays – I think as stated before there are way too many functions to explain so we will go through a few important ones, for the sake of this tutorial.
a) push –
The push function allows us to add scalars to an array. Example in this script:
Here with “push” the script added ‘gun’ to our array.
bike bat golf clubs gun
b)pop – The “pop” function allows us to remove scalars from our array. Example in this script:
The ‘pop’ function here just removed the gun we just added.
bike bat golf clubs
c) scalar – The scalar function is used to count the number of elements in an array or hash.
This array has 4 actual elements to it
d) join – Like those above, join is another popular function. This function can join different arrays and elements. Here’s an example: