السبت، فبراير ٢١، ٢٠٠٩

Perl, You don't want to miss! -3-

Lists and arrays in Perl.


That will be my last post about Perl, however Perl doesn't stop here :) ... you may notice I didn't mention the strength of Perl string processing and file handling. It's worth searching for ;)

What about list?
I may have mentioned before that lists could be generated easily this way in Perl:


(1..5)



This is a range list, it's also valid for character types



What about:

(c..10)



This one is valid too! since "c" is interpolated to Zero (0), then it becomes: (0..10)



Lists in general could be written this way:

('Item1', 'Item2', 'Item3')

or

qw(Item1 Item2 Item3) #you may use any delimeters for this method rather than the parens



Indexing the list is much like any other language you know,using square brackets. The two things that might be of interest in Perl:

- Using a floating number to index, will be floored

- Using a negative number will index starting from the end of the list (backward)

- You may user a list of indexes! this will return another list for the indexed positions (list slice)

- You may combine slicing and ranging to construct the list of indexes. [1,2,3 .. 7]



Assignable lists:

($firstItem , $secondItem) = (1,2);

print $firstItem; #This should print '1'



Array type:

@arrayName = (1,2,3,4);

print @array,"\n"; #Output: 1234

print "@array\n"; #Output: 1 2 3 4 #Notice the spaces! Amazing



Indexing an array variabe:

use "$" to tell Perl what you "want" to get out of the variable.

$firstItem = $arrayName[0]; #Notice the usage of the '$' when indexing the array

#That's because it's returning a single/scalar value;



The "scalar" function may take care of this too.



What's the array's last index? $#

So for example, for array "@arr", that last element's index is: $#arr



List methods:

- shift: Add element to the front

- unshift: Get & remove element from the front

- push: Add element to the back

- pop: Get & remove element from the back

- sort: returns a sorted array, be default it sorts using the string comparison methods.



sort { <:method expression>} @arrayName



So it would be for example:

sort { -($a <=> $b) } @array;

This will return a sorted array, in descending order.

Perl: You don't want to miss! -2-

This post will be about loops and conditions! Hopefully this will be too short.

Loops and if conditions are not much differnt from other langauges:
while, for, do-while, if, foreach

What seems new to me (Cx & Java developer)
- if -elseif
- until (negative of "while": while not)
- do ... until
- unless (negative of "if": if not)

What about loop controls?
- last (aka "break")
- next (aka "continue")
- redo (the amazing keyword, just redo the loop!)

while(<STDIN>) #Keep reading the standard input
{
print; #No argument? yes, it defaults to printing the magic variable: $_
if($_ > 0) {
$_--; #Decerement
redo; #Back to the top of the loop without executing the condition body!
}
}

Expression Modifier
What about rephrasing the "redo"?

redo if $_-- > 0; #Remember, the post-decrement has the lower precedence
See, the statement (action) comes before the condition in this expression modifier.

This is valid for the "if" and "unless" too. Give it a try!

Perl: You don't want to miss! -1-

Assalamo Alikom,

This post is about Perl, a well known programming language, and you don't want to ask me about it! I'm still a beginner doing the book execrices, didn't finish my assignments yet.
Better you go google "Perl": A {Scripting Dynamic} Langauge

This post is intended mainly to mention the first facts I've learned about Perl, it'll be all about the "print" function and some Strings and Numbers!

Print:
Here's the first picture of "print":
print "Hello... you know!";
This will print: Hello... you know!

Trivial, hah? even this:
print 'Hello... World!'
This will print: Hello...World!

What if we embeded this (scalar) string $myName inside the first version of print:
$myName = "Muhammad";
print "Hello $myName"

The second version?
$myName = "Muhammad";
print "Hello $myName"

This didn't automatically convert my string, good!

What about this:
print `Hello World!`;

These are different quote styles: `
This should output something like: Command not found ... Yeah, this print will execute what's inside!
So you may try something like:
print `cp file1 file2`;

("cp" is the copy command for bash)

What about Strings/Numbers?

Strings in Perl are First-Class Citizens! users shouldn't treat them as a character array!

$stringOne = "1";
print $stringOne + 1;

This prints "2" ;)

What about this:
$stringAddress = "8A";
print $stringAddress + 8;

This will print a warning! and then? ... it will print "16" which is 8+8
Perl starts parsing the string until it finds a non-numeric value it terminates but it keeps the valid piece. Notice that if the address string was something like: "A8" it would be converted to "0"!

Incrementing strings:

print ++"A0";

This will output: A1
You should give it a try yourself ;)

Hopefully I'll have more to say later on :)

Assalamo Alikom