Hi All,
Here is the next chapter. Once again all feedback greatly appreciated.

Arrays

Defined using the @ symbol in front of the array name, arrays store a 
list variables with numbered locations starting at 0. e.g

my @array = (‘one’, ‘two’);

stores ‘one’ at location 0 and ‘two’ at location 1. This is an important 
thing to note with a lot of computer arithmetic, numbering generally 
starts at 0.

To add a variable to the end of any array you can use the push command.

push( @array, ’three’ );

or simply use:

@array = ( @array, ’three’ );

To join an array

my @join = ( @array1, @array2 );

To take an item off the end of an array, use pop.

my $item = pop( @array );

So push and pop are a pair of functions that deal with adding/removing 
from the end of an array. Their counterparts are unshift and shift, that 
deal with adding/removing from the start of an array.
shift takes the first item off of an array.

my $item = shift( @array );

and unshift puts it back on

unshift( @array, 'zero' );

To access a value in an array you need to reference its location e.g

my @array = (‘one’, ‘two’,’three’);
my $item = $array[1];

$item will contain 'two', if no value is returned you get undef. Note 
that when obtaining an arrays value you use $ rather than @ and square 
brackets []. This is because you are accessing a single Scalar value 
form the array, not the whole array.

To sort an array

@array = sort @array;

The default sort is an ASCII one, so A-Z goes before a-z. We'll cover 
more on sorting later.


Getting input from the user

There are different ways of getting input from a user. From command line 
arguments to sockets. Here are a couple of common methods.

Command line arguments

Perl has a special variable named @ARGV. It's an array containing all of 
the parameters passed into the script when it's been called from the 
command line. For instance the script:-

#!c:/perl/bin/perl.exe
print $ARGV[0];

Would print the first parameter passed in. So if called with “perl 
script.pl Hello” it would display “Hello”. If called with “perl 
script.pl Hello Lyle” it would still only display “Hello”. This is 
because it's only printing the first parameter passed. You could use 
parenthesis to make the first parameter contain both Hello and Lyle, 
such as 'perl script.pl “Hello Lyle”' would print “Hello Lyle”.
STDIN file handle
STDIN stands for 'Standard Input”. Generally speaking, all Unix derived 
languages will have a STDIN, STDOUT 'Standard Output' and STDERR 
'Standard Error'. They are treated as file handles that you can either 
read from, or write to. You've already used STDOUT, by default print 
goes to STDOUT, so the following lines are the same:-

print “Hello”;
print STDOUT “Hello”; # Same

You can read from STDIN using the readline command, although Perl 
provides shorthand in the form of <FILEHANDLE>, such that:-

my $name = readline(*STDIN);
my $name = <STDIN>; # Same

It's uncommon to use readline, so stick with <STDIN>. You'll notice that 
in the readline example, STDIN has a * sigil, this is because to pass 
file handles into functions you must pass the whole GLOB. I'm not 
covering GLOBs here, for now think of them as another kind of variable.
The example code above will pause the program and wait for user input, 
once the user hits enter the program will continue and $name will 
contain whatever they typed.


Conditionals

Things would be pretty boring if programs just went in a straight line 
all the time. Conditionals allow the software to make decisions based on 
variables, often ones that have been input. Perl offers if, elsif, else 
and for convenience unless.

if

The if statement allows you to run a block of code if a condition is 
true. They are in the format:-

if ( CONDITION ) {
CODE
}#if

You'll notice the use of curly brackets again. This time they surround 
the block of code that is to be run if the condition is true. The small 
comment on the end #if is a personal touch and not required. In larger 
programs you can have many code blocks within code blocks, a simple 
comment on the end bracket can help you keep track of where they started.

In singular context, Perl sees anything that isn't undef, 0, 0.0 or '' 
(empty string), '0' (string containing 0) or an empty list as true. 
Examples:-

if ( 0 ) { } # False
if ( 1 ) { } # True
if ( 0.0 ) { } # False
if ( '' ) { } # False
if ( 'text' ) { } # True
if ( undef ) { } # False

You can also reverse this with not or !, such as:-

if ( ! 0 ) { } # True
if ( not 1 ) { } # False

You can also make comparisons or check if an operation or function 
returns a true value:-

if ( 0 == 0 ) { } # True
if ( 1 == 0 ) { } # False

Note that for comparisons you need to use == rather than just =. This is 
because = is used for an assignment. Example:-

# Correct way
my $value = 1;
if ( $value == 1 ) { } # True

# Wrong way
if ( $value = 0 ) { } # True

Both examples return true. This is because the second one assigns 0 to 
the scalar $value which is a successful operation returning true. !, >, 
< can also be used:-

if ( 0 != 0 ) { } # False
if ( 1 > 0 ) { } # True
if ( 0 <= 0 ) { } # True

When comparing strings you use the eq and ne syntax instead (equals, not 
equals respectively):-

if ( 'a' eq 'b' ) { } # False
if ( 'a' ne 'b' ) { } # True

You also have OR, ||, AND, &&. OR and || do basically the same thing, as 
do AND and &&. There are subtle differences in preference, but I won't 
be covering them as the vast majority of the time they are interchangeable.

if ( 'a' eq 'b' || 1 > 0 ) { } # True
if ( 'a' ne 'b' && 1 < 0 ) { } # False


else

You may also use the else statement with if, allowing to to do one thing 
or the other. Such as:-

if ( $name eq 'Lyle' ) {
print 'Thanks for teaching us Lyle';
}#if
else {
print “Hello $name”;
}#else


elsif

Often 'else if' or 'elseif' in other languages, shortened to elsif in 
Perl. It allows you to run another condition only if the original if is 
false. You can have many elsif statements as you want, often you end 
with a else. Such as:-

if ( $name eq 'Lyle' ) {
print 'Thanks for teaching us Lyle';
}#if
elsif ( $name eq '' ) {
print “Please input your name”;
}#elsif
else {
print “Hello $name”;
}#else


unless

For general convenience and to make things read easier, unless is 
provided as an alternative to if not.

if ( not( 9 > 10 ) ) { } # True
unless ( 9 > 10 ) { } # Same



Lyle

_______________________________________________
BristolBathPM mailing list
[email protected]
http://mailman.bristolbath.org/mailman/listinfo/bristolbathpm

Reply via email to