Perl Tutorial Part 3:

So, lets manipulate this new script with “for” and then try to find an easier way to write it over:

Code

#!/usr/local/bin/perl
use strict;
use warnings;

my @Linuxflavors=qw( ubuntu slackware fedora knoppix openSUSE );
my $index; 
for($index=0; $index<@Linuxflavors; $index++) {

print "My favorite Linux flavor is $Linuxflavors[$index] and...";

    }

    print "and many others..n";





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:

Code

#!/usr/local/bin/perl
use strict;
uuse warnings;

my @Linuxflavors=qw( ubuntu slackware fedora knoppix openSUSE );

foreach my $distro (@Linuxflavors){

print "I'd love to try the distro, $distron";
    }




And we have our output of both proggies:

h4x@slax:~/Desktop$ perl arraymanip.pl
output:
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

or even:

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:



Code

    #!/usr/local/bin/perl

   use strict;
   use warnings;

   my @list=( 'whole milk', 'Coco puffs cereal', 'Nat Sherman smokes', 'gas and oil' );

    foreach my $list (@list) {
         
       print "I really need some $list before it gets too late and..n";
    }

    print "Ok, we now finally have:n";

    foreach my $list (@list) {

       print "$listn";
    }

    print "Whew, I feel so much better now that we have what we needn";




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.

Output:

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:
whole milk
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:


Code

#!/usr/local/bin/perl
use strict;
use warnings;

my @data=( 'bike', 'bat', 'golf clubs' );

push(@data, 'gun');

    print "@datan";






Here with “push” the script added ‘gun’ to our array.

output:

bike bat golf clubs gun


b)pop – The “pop” function allows us to remove scalars from our array. Example in this script:

Code

#!/usr/local/bin/perl
use strict;
use warnings;

my @data=( 'bike', 'bat', 'golf clubs', 'gun' );
  pop(@data);
  print "@datan";




The ‘pop’ function here just removed the gun we just added.

Output:

bike bat golf clubs


c) scalar – The scalar function is used to count the number of elements in an array or hash.

Code

#!/usr/local/bin/perl
use strict;
use warnings;

my @data=( 'bike', 'bat', 'golf clubs', 'gun' ); #our array

print "This array has " ,scalar(@data) , " actual elements to itn";




Output:

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:

Code

#!/usr/local/bin/perl
use strict;
use warnings;

my @data = ("The quarter pounder", "the happy meal", "a Coca Cola");

print join(" and ", @data) . " will be what I'm ordering, please.n";




Output:

The quarter pounder and the happy meal and a Coca Cola will be what I’m ordering, please.


What Do You Think on This ? Say Here

This site uses Akismet to reduce spam. Learn how your comment data is processed.