IT

Perl using hash variables 150 150 Roderick Derks

Perl using hash variables

In Perl, a hash lets you create a one-to-one association between a variable, called the key, and a value. We’ll show you the basics of Perl hashes and some useful tricks.

One of the most useful constructs in Perl is the humble array, which lets you store more than one value in a single variable. This is very useful if you have a set of related values—for example, a shopping list—and you need to group them into a collection, yet be able to access them individually.

A variant of the regular array, which is indexed by number, is the hash or associative array, which is indexed by key. A hash is a Perl construct that allows a programmer to create one-to-one associations between a variable, or “key,” and its value. Elements of a hash can be accessed only via their keys, and keys within a hash must be unique.

Get ready, because we’re about to give you a crash course in how Perl hashes work. And we’ll show you some examples of where you can use them.

Hash basics
Creating a Perl hash is very similar to defining an array: You create a hash variable and (optionally) assign some keys and values to it:
# define hash
%account = (‘username’ => ‘jimmyboy’, ‘password’ => ‘scar3me’);

Notice the % symbol preceding the variable name, and contrast it to the @ sign preceding an array. Notice also the => symbol, which is used to connect a key to its associated value.

Hash elements consist of a key-value pair. Each key in a hash is unique and serves as a unique identifier for the corresponding value. The value can be retrieved only if you know the corresponding key. Therefore, in the example above, you would retrieve the value jimmyboy with the following notation:
# retrieve value of hash element
print $account{‘username’};

To add new elements to a hash, define a new key-value pair, and Perl will automatically add it to the hash variable:
# add an element to a hash
$account{‘active’} = 1;
 
# the hash now contains
%account = (‘username’ => ‘jimmyboy’, ‘password’ => ‘scar3me’, ‘active’ => 1);

Note that unlike arrays, Perl hashes are not arranged in any specific numerical order or in the order in which the keys are added. Rather, Perl uses its own sorting algorithms to arrange the elements in an optimal manner. For this reason, you must always access hash values by key rather than position.

To remove an element from a hash, use the delete() function as shown below. Don’t even think about using the infamous—and flawed—technique of assigning an empty value to the key. That just leaves the key in the array with a null value. It doesn’t actually remove the key, which is why you need to use delete() instead.
# delete the named key from the hash
delete($account{‘password’});

Hashes and regular arrays mix well with each other, so it’s even possible to have a hash key pointing to an array (or vice versa), as in this example:
# hash keys pointing to an array
%combo = (‘colors’ => [‘red’, ‘green’, ‘blue’], ‘vowels’ => [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]);

Processing hash elements
Perl offers the keys() and values() functions to obtain lists of all the keys and values present in a hash. These functions come in handy when you need to process all the elements in a hash using a foreach() loop. The following example iterates over a hash and writes the elements to a file as comma-separated key-value pairs.
# set up hash
%capitals = (‘china’ => ‘beijing’, ‘england’ => ‘london’, ‘france’ => ‘paris’, ‘norway’ => ‘oslo’, ‘italy’ => ‘rome’);
 
# open file
open (FILE, “>data.txt”);
 
# process hash elements
# write to file in csv format
foreach $k (keys (%capitals))
{
        print FILE $k, “,”, $capitals{$k}, “\n”;
}
 
# close file
close(FILE);

Note that the keys() function returns an array. By iterating over this array in a foreach() loop, it becomes possible to process each and every element in the hash.

Another option is to use the each() function, which iteratively moves through a hash creating a two-item array at each stage for the current key and value. The each() function is thus well suited to a while() loop, as illustrated in this rewrite of the previous example:
# set up hash
%capitals = (‘china’ => ‘beijing’, ‘england’ => ‘london’, ‘france’ => ‘paris’, ‘norway’ => ‘oslo’, ‘italy’ => ‘rome’);
 
# open file
open (FILE, “>data.txt”);
 
# process hash elements
# write to file in csv format
while ( ($country, $capital) = each (%capitals))
{
       print FILE $country, “,”, $capital, “\n”;
}
 
# close file
close(FILE);

Quick tip: If all you really want to do is check if a particular value exists in a hash, don’t waste time by using a loop and an if() test at each iteration. Instead, use the built-in exists() function, which returns Boolean true if a key match occurs.

 

One-Dimension Array Operations

Create an empty array:
@myarray = ( );

Create a filled array:
@myarray = ($var0, $var1, $var2, …);

Get the length of an array:
$arrlen = @myarray;
or
$arrlen = $#myarray + 1;

Fetch a specific single member in an array:
$myscalar = $myarray[$index];

Fetch a contiguous set of members in an array (‘ .. ‘ is literal):
@newarray = @myarray[$idx_beg .. $idx_end];

Fetch specific members in an array:
@newarray = @myarray[$idx1, $idx2, $idx3, …];

Set a specific single member in an array:
$myarray[$index] = $myscalar;

Set a contiguous set of members in an array (‘ .. ‘ is literal):
@myarray[$idx_beg .. $idx_end] = @replac;

Set specific members in an array:
@myarray[$idx1, $idx2, $idx3, …] = @replac;

Remove but save the scalar from the beginning of an array:
$value = shift (@myarray);

Insert a scalar at the beginning of an array (returns new size):
$newlen = unshift (@myarray, $myvar);

Insert one array at the beginning of another existing one (returns new size):
$newlen = unshift (@another, @one_array);

Remove but save the scalar at the end of an array:
$value = pop (@myarray);

Add a value at the end of an array (returns new size):
$newlen = push (@myarray, $myvar);

Append one array to another existing one (returns new size):
$newlen = push (@another, @one_array);

Delete everyting from an array and lose it all:
%myarray = ();

Delete everyting from an array but save it for later:
@rem_data = splice (@myarray);

Delete everything from a specified position and later:
@rem_data = splice (@myarray,$first_del);
pop (@myarray);    is same as    splice (@myarray, -1);

Delete several elements from a specified position and later:
@rem_data = splice (@myarray, $first_del, $num_dels);
pop (@myarray);    is same as    splice (@myarray, $#myarray, 1);
shift (@myarray);    is same as    splice (@myarray, 0, 1);

Delete several elements from a specified position and later, then replace with data from a list:
@rem_data = shift (@myarray, $first_del, $num_dels, $replac1, $replac2, …);
or
@rem_data = shift (@myarray, $first_del, $num_dels, @replac);

Multi-Dimension Arrays

In a 2-dimension array, the first dimension is a set of references to arrays, and each reference yields a 1-D array of scalar values for the second dimension. In a 3-D array, the first and second dimensions are sets of references to arrays, and the last dimension is a set of scalar values.

Create a filled 2-D array (2r x 3c):
@veca = {[$r0c0,$r0c1], [$r1c0,$r1c1], [$r2c0,$r2c1]};

Fill in a single row from another 1-D array:
$veca[$rownum] = [ @row ];

Fetch a single row:
@row = @{$veca[$rownum]};
or
@row = @$veca[$rownum];

Fetch a single member:
$val = ${$veca[$rownum]}->[$colnum];
or
$val = $veca[$rownum]->[$colnum];
or
$val = $veca[$rownum][$colnum];

Number of rows (length of the first dimension):
@n_rows = $#veca + 1;

Number of columns in a row:
@n_cols = $#{$veca[$rownum]};

Hash Operations

Create an empty hash:
%myhash = { };

Create a filled hash:
%myhash = ($key1, $val1, $key2, $val2, …);
or
%myhash = ($key1 => $val1, $key2 => $val2, …);

Fetch a hash value from a known key:
$the_val = $myhash{$the_key};

Fetch a several hash values at once
@val_list = @myhash{$key1, $key2, $key3, …};

Create a reference to an anonymous filled hash:
$hashref = {$key1 => $val1, $key2 => $val2, …};
for example
$record[$rec_num] = {name => $val1, rank => $val2, sernum => $val3};
then
$this_rank = $record[$this_rec_num]->{‘rank’};

Test if a particular key exists in a hash:
$is_there = exists ($myhash{$the_key});

Test if a particular key has its corresponding value defined (not undef):
$has_value = defined ($myhash{$the_key});

Get a list of all keys:
@keylist = keys (%myhash);

Get a list of all values (same order as keys):
@valuelist = values (%myhash);

Remove a key/value pair but save it elsewhere for later:
%removed = delete ($myhash{$delkey});

 

 

Manipulating Perl Arrays

How to perform tasks with an array in Perl

Now that you have seen the basics of arrays, you’ll want to see how to change and manipulate an array or the elements of an array. The first thing we will look at is changing and adding elements.

Changing & Adding Elements

To change an array element, you can just access it with its list number and assign it a new value:

@browser = (“NS”, “IE”, “Opera”);
$browser[2]=”Mosaic”;

This changes the value of the element with the list number two (remember, arrays start counting at zero- so the element with the list number two is actually the third element). So, we changed “Opera” to “Mosaic”, thus the array now contains “NS”, “IE”, and “Mosaic”.

Now, suppose we want to add a new element to the array. We can add a new element in the last position by just assigning the next position a value. If it doesn’t exist, it is added on to the end:

@browser = (“NS”, “IE”, “Opera”);
$browser[3]=”Mosaic”;

Now, the array has four elements: “NS”, “IE”, “Opera”, and “Mosaic”.

Splice Function

Using the splice function, you can delete or replace elements within the array. For instance, if you simply want to delete an element, you could write:

@browser = (“NS”, “IE”, “Opera”);
splice(@browser, 1, 1);

You’ll see three arguments inside the () of the splice function above. The first one is just the name of the array you want to splice. The second is the list number of the element where you wish to start the splice (starts counting at zero). The third is the number of elements you wish to splice. In this case, we just want to splice one element. The code above deletes the element at list number 1, which is “IE” (NS is zero, IE is 1, Opera is 2). So now the array has only “NS” and “Opera”.

If you want to delete more than one element, change that third number to the number of elements you wish to delete. Let’s say we want to get rid of both “NS” and “IE”. We could write:

@browser = (“NS”, “IE”, “Opera”);
splice(@browser, 0, 2);

Now, it starts splicing at list number zero, and continues until it splices two elements in a row. Now all that will be left in the array is “Opera”.

You can also use splice to replace elements. You just need to list your replacement elements after your other three arguments within the splice function. So, if we wanted to replace “IE” and “Opera” with “NeoPlanet” and “Mosaic”, we would write it this way:

@browser = (“NS”, “IE”, “Opera”);
splice(@browser, 1, 2, “NeoPlanet”, “Mosaic”);

Now the array contains the elements “NS”, “NeoPlanet”, and “Mosaic”. As you can see, the splice function can come in handy if you need to make large deletions or replacements.

Unshift/Shift

If you want to simply add or delete an element from the left side of an array (element zero), you can use the unshift and shift functions. To add an element to the left side, you would use the unshift function and write something like this:

@browser = (“NS”, “IE”, “Opera”);
unshift(@browser, “Mosaic”);

As you can see, the first argument tells you which array to operate on, and the second lets you specify an element to be added to the array. So, “Mosaic” takes over position zero and the array now has the four elements “Mosaic”, “NS”, “IE”, and “Opera”.

To delete an element from the left side, you would use the shift function. All you have to do here is give the array name as an argument, and the element on the left side is deleted:

@browser = (“NS”, “IE”, “Opera”);
shift(@browser);

Now, the array has only two elements: “IE” and “Opera”.

You can keep the value you deleted from the array by assigning the shift function to a variable:

@browser = (“NS”, “IE”, “Opera”);
$old_first_element= shift(@browser);

Now the array has only “IE” and “Opera”, but you have the variable $old_first_element with the value of “NS” so you can make use of it after taking it from the array.

Push/Pop

These two functions are just like unshift and shift, except they add or delete from the right side of an array (the last position). So, if you want to add an element to the end of an array, you would use the push function:

@browser = (“NS”, “IE”, “Opera”);
push(@browser, “Mosaic”);

Now, you have an array with “NS”, “IE”, “Opera”, and Mosaic”.

To delete from the right side, you would use the pop function:

@browser = (“NS”, “IE”, “Opera”);
pop(@browser);

Now you have an array with just “NS” and “IE”.

You can keep the value you deleted from the array by assigning the pop function to a variable:

@browser = (“NS”, “IE”, “Opera”);
$last_element= pop(@browser);

Now the array has only “NS” and “IE”, but you have the variable $last_element with the value of “Opera” so you can make use of it after taking it from the array.

Chop & Chomp

If you want to take the last character of each element in an array and “chop it off”, or delete it, you can use the chop function. It would look something like this:

@browser = (“NS4”, “IE5”, “Opera3”);
chop(@browser);

This code would take the numbers off the end of each element, so we would be left with “NS”, “IE” and “Opera”.

If you want to remove newline characters from the end of each array element, you can use the chomp function. This comes in handy later when we are reading from a file, where we would need to chomp the new line (\n) character from each line in a file. Here is a more simplified example for now:

@browser = (“NS4\n”, “IE5\n”, “Opera3\n”);
chomp(@browser);

This code takes off the \n characters from the end of each element, leaving us with NS4, IE5, and Opera3.

Sort

If you want to sort the elements of an array, this can come in handy. You can sort in ascending or descending order with numbers or strings. Numbers will go by the size of the number, strings will go in alphabetical order.

@browser = (“NS”, “IE”, “Opera”);
sort (ascend @browser);

sub ascend
{
 $a <=> $b;
}

The sub from above is a subroutine, much like what is called a “function” in other languages. We will explain that in more detail later. As you can see, the code above would sort in ascending order, so it would sort our array in alphabetical order. So, we have: “IE”, “NS”, and “Opera” as the new order. If you want it in reverse alphabetical order, you would use $b <=> $a in place of the $a <=> $b above. You could change the name of the subroutine to whatever you wish, just be sure you call the subroutine with the same name. This will make a bit more sense after we get to the section on subroutines and reading from files.

Reverse

You can reverse the order of the array elements with the reverse function. You would just write:

@browser = (“NS”, “IE”, “Opera”);
reverse(@browser);

Now, the order changes to “Opera”, “IE”, and “NS”.

Join

You can create a flat file database from your array with the join function. Again, this is more useful if you are reading and writing form files. For now, what you will want to know is that it allows you to use a delimiter (a character of your choice) to separate array elements. The function creates a variable for each element, joined by your delimiter. So, if you want to place a colon (:) between each element, you would write:

@browser = (“NS”, “IE”, “Opera”);
join(“:”, @browser);

This would create something like this:

NS:IE:Opera

Split

The split function is very handy when dealing with strings. It allows you to create an array of elements by splitting a string every time a certain delimiter (a character of your choice) shows up within the string. Suppose we had the string:

NS:IE:Opera

Using the split function, we could create an array with the elements “NS”, “IE”, and “Opera” by splitting the string on the colon delimiter (:). We could write:

$browser_list=”NS:IE:Opera”;
@browser= split(/:/, $browser_list);

Notice in the split function that you place your delimiter between two forward slashes. You then place the string you want to split as the second argument, in this case the string was the value of the $browser_list variable. Setting it equal to @browser creates the @browser array from the split.

Well, that should be enough to keep us all busy for a while. You can do a whole bunch of things with arrays, and we’ll be using them extensively in later sections. So, have fun with what we have so far, we’ll be moving on soon!

Well, that’s it for now. Let’s go on to: Associative Arrays.

 

Perl advanced output overview 150 150 Roderick Derks

Perl advanced output overview

In many situations, especially for web programming, you will find that you want to put certain things, such as backslashes or quotes, in your text that aren’t allowed in a traditional print statements. A statement such as

print "I said "I like mangos and bananas". ";

will not work because the interpreter would think that the quotes mark the end of the string. As with all things in Perl, there are many solutions to this problem.

Use other quotes

The quickest solution to this problem would be to use single quotes to surround the string, allowing the use of double quotes in the middle.

# I said "I like mangos and bananas".
print 'I said "I like mangos and bananas".';

This is obviously not the best solution, as it is conceivable that you are trying to print a string containing both kinds of quote:

# I said "They're the most delicious fruits".
print 'I said "They're the most delicious fruits".';

Escape characters

For situations like the above where only a short amount of text is being quoted, a common solution is to escape any quotes in the string. By preceding any quotes with a backslash they are treated as literal characters.

print "I said \"They\'re the most delicious fruits\".";

This of course implies that you also need to escape any backslashes you want to use in your string. To print the line I have written above literally in perl, you would need to write.

print " print \"I said \\\"They\\\'re the most delicious fruits\\\".\";"

Luckily perl provides us with another way of quoting strings that avoids this problem.

Custom Quotes

Perl provides the operators q and qq that allows you to decide which characters are used to quote strings. Most punctuation characters can be used. Here are a few examples:

print qq{ I said "They're the most delicious fruits!". };
print q! I said "They're the most delicious fruits\!". !;

The only symbols I have found that cannot be used for these quotes are $ ` /

Block Output

As can be seen, while the custom quotes option works for short strings, it can run into problems if a lot of text containing a lot of punctuation is output. For this situation, a technique called Block quoting can be used.

print <<OUTPUT
I said "They're the most delicious fruits!".
OUTPUT;

Any string of characters can be used instead of OUTPUT in the example above. Using this technique anything can be output no matter what characters it contains. The one caveat of this method is that the closing OUTPUT must be the first character on the line, there cannot be any space before it.

print <<EverythingBetween
...
...
EverythingBetween;

Variable Output

It is possible to output variables within strings when you use some of these methods:

my $one = 'mangoes';

print "I like $one."; # I like mangoes.
print 'I like $one.'; # I like $one.
print qq@ I love $one.@; # I love mangoes.
print q#I love $one.#; # I love $one.

print <<OUT
I love $one
OUT; # I love mangoes

print <<'OUT'
I love $one
OUT; # I love $one

Collect data and print it in a nice way on the screen

# this is to create a report that can be printed to the screen or to a file
formline q{@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<< @<<<<<<<<<<   @>\@<<
}, $mac, $ip, $dns, ${StackTypeNumber}, ${stack_copy}, ${switch}, ${port};
# this is to print the collected statistics to a variable
$rapport = $^A;

This is an internal function used by formats, though you may call it, too. It formats (see the perlform manpage) a list of values according to the contents of PICTURE, placing the output into the format output accumulator, $^A (or $ACCUMULATOR in English). Eventually, when a write() is done, the contents of $^A are written to some filehandle, but you could also read $^A yourself and then set $^A back to “”. Note that a format typically does one formline() per line of form, but the formline() function itself doesn’t care how many newlines are embedded in the PICTURE. This means that the ~ and ~~ tokens will treat the entire PICTURE as a single line. You may therefore need to use multiple formlines to implement a single record format, just like the format compiler.

Be careful if you put double quotes around the picture, because an “@” character may be taken to mean the beginning of an array name. formline() always returns TRUE. See the perlform manpage for other examples.

 

Caveats

The single quote ‘ q{ and double quote ” qq <<A operators, behave differently. Whereas when using double quotes, you can include variables and escape any characters, when you use single quotes you can only escape single quotes and you cannot include variables.

Perl string matching 150 150 Roderick Derks

Perl string matching

Regular expressions

A regular expression is contained in slashes, and matching occurs with the =~ operator. The following expression is true if the string the appears in variable $sentence.

$sentence =~ /the/

The RE is case sensitive, so if

$sentence = "The quick brown fox";

then the above match will be false. The operator !~ is used for spotting a non-match. In the above example

$sentence !~ /the/

is true because the string the does not appear in $sentence.

 


The $_ special variable

We could use a conditional as

if ($sentence =~ /under/)
{
print "We're talking about rugby\n";
}

which would print out a message if we had either of the following

$sentence = "Up and under";
$sentence = "Best winkles in Sunderland";

But it’s often much easier if we assign the sentence to the special variable $_ which is of course a scalar. If we do this then we can avoid using the match and non-match operators and the above can be written simply as

if (/under/)
{
print "We're talking about rugby\n";
}

The $_ variable is the default for many Perl operations and tends to be used very heavily.

 


More on REs

In an RE there are plenty of special characters, and it is these that both give them their power and make them appear very complicated. It’s best to build up your use of REs slowly; their creation can be something of an art form.

Here are some special RE characters and their meaning

.	# Any single character except a newline
^ # The beginning of the line or string
$ # The end of the line or string
* # Zero or more of the last character
+ # One or more of the last character
? # Zero or one of the last character

and here are some example matches. Remember that should be enclosed in // slashes to be used.

t.e	# t followed by anthing followed by e
# This will match the
# tre
# tle
# but not te
# tale
^f # f at the beginning of a line
^ftp # ftp at the beginning of a line
e$ # e at the end of a line
tle$ # tle at the end of a line
und* # un followed by zero or more d characters
# This will match un
# und
# undd
# unddd (etc)
.* # Any string without a newline. This is because
# the . matches anything except a newline and
# the * means zero or more of these.
^$ # A line with nothing in it.

There are even more options. Square brackets are used to match any one of the characters inside them. Inside square brackets a indicates “between” and a ^ at the beginning means “not”:

[qjk]		# Either q or j or k
[^qjk] # Neither q nor j nor k
[a-z] # Anything from a to z inclusive
[^a-z] # No lower case letters
[a-zA-Z] # Any letter
[a-z]+ # Any non-zero sequence of lower case letters

At this point you can probably skip to the end and do at least most of the exercise. The rest is mostly just for reference.

A vertical bar | represents an “or” and parentheses () can be used to group things together:

jelly|cream	# Either jelly or cream
(eg|le)gs # Either eggs or legs
(da)+ # Either da or dada or dadada or...

Here are some more special characters:

\n		# A newline
\t # A tab
\w # Any alphanumeric (word) character.
# The same as [a-zA-Z0-9_]
\W # Any non-word character.
# The same as [^a-zA-Z0-9_]
\d # Any digit. The same as [0-9]
\D # Any non-digit. The same as [^0-9]
\s # Any whitespace character: space,
# tab, newline, etc
\S # Any non-whitespace character
\b # A word boundary, outside [] only
\B # No word boundary

Clearly characters like $, |, [, ), \, / and so on are peculiar cases in regular expressions. If you want to match for one of those then you have to preceed it by a backslash. So:

\|		# Vertical bar
\[ # An open square bracket
\) # A closing parenthesis
\* # An asterisk
\^ # A carat symbol
\/ # A slash
\\ # A backslash

and so on.

 


Some example REs

As was mentioned earlier, it’s probably best to build up your use of regular expressions slowly. Here are a few examples. Remember that to use them for matching they should be put in // slashes

[01]		# Either "0" or "1"
\/0 # A division by zero: "/0"
\/ 0 # A division by zero with a space: "/ 0"
\/\s0 # A division by zero with a whitespace:
# "/ 0" where the space may be a tab etc.
\/ *0 # A division by zero with possibly some
# spaces: "/0" or "/ 0" or "/ 0" etc.
\/\s*0 # A division by zero with possibly some
# whitespace.
\/\s*0\.0* # As the previous one, but with decimal
# point and maybe some 0s after it. Accepts
# "/0." and "/0.0" and "/0.00" etc and
# "/ 0." and "/ 0.0" and "/ 0.00" etc.

 


Get the output from a command and split the result

      open(OUTPUT, “snmpwalk -v1 -c ${communitystring} ${stack} IF-MIB::ifName.${port_id}|”);
      @OUTPUT = <OUTPUT>;
      close(OUTPUT);

      $switch_port=”@OUTPUT”;
      chomp $switch_port;
      $switch = ((split /Slot: /, ${switch_port})[1]);
      $switch = ((split / Port:/, ${switch})[0]);

 

Perl Loops 150 150 Roderick Derks

Perl Loops

Perl supports four main loop types:

  1. while

  2. for

  3. until

  4. foreach

In each case, the execution of the loop continues until the evaluation of the supplied expression changes.

  • In the case of a while loop execution continues while the expression evaluates to true.
  • The until loop executes while the loop expression is false and only stops when the expression evaluates to a true value.
  • The list forms of the for and foreach loop are special cases. They continue until the end of the supplied list is reached.

while Loops

The while loop has three forms:

while EXPRLABEL
while (EXPR) BLOCKLABEL
while (EXPR) BLOCK continue BLOCK

In first form, the expression is evaluated first, and then the statement to which it applies is evaluated. For example, the following line increases the value of $linecount as long as we continue to read lines from a given file:

For example, the following line increases the value of $linecount as long as we continue to read lines from a given file:

$linecount++ while ();

To create a loop that executes statements first, and then tests an expression, you need to combine while with a preceding do {} statement. For example:

do
{
$calc += ($fact*$ivalue);
} while $calc <100;

In this case, the code block is executed first, and the conditional expression is only evaluated at the end of each loop iteration.

The second two forms of the while loop repeatedly execute the code block as long as the result from the conditional expression is true. For example, you could rewrite the preceding example as:

while($calc < 100)
{
$calc += ($fact*$ivalue);
}

The continue block is executed immediately after the main block and is primarily used as a method for executing a given statement (or statements) for each iteration, irrespective of how the current iteration terminated. It is somehow equivalent to for loop

{
my $i = 0;
while ($i <100)
{ ... }
continue
{
$i++;
}
}

This is equivalent to
for (my $i = 0; $i < 100; $i++)
{ ... }

for Loops

A for loop is basically a while loop with an additional expression used to reevaluate the original conditional expression. The basic format is:

LABEL for (EXPR; EXPR; EXPR) BLOCK

The first EXPR is the initialization – the value of the variables before the loop starts iterating. The second is the expression to be executed for each iteration of the loop as a test. The third expression is executed for each iteration and should be a modifier for the loop variables.

Thus, you can write a loop to iterate 100 times like this:

for ($i=0;$i<100;$i++)
{
...
}

You can place multiple variables into the expressions using the standard list operator (the comma):

for ($i=0, $j=0;$i<100;$i++,$j++)

You can create an infinite loop like this:

for(;;)
{
...
}

until Loops

The inverse of the while loop is the until loop, which evaluates the conditional expression and reiterates over the loop only when the expression returns false. Once the expression returns true, the loop ends.

In the case of a do.until loop, the conditional expression is only evaluated at the end of the code block. In an until (EXPR) BLOCK loop, the expression is evaluated before the block executes. Using an until loop, you could rewrite the previous example as:

do
{
$calc += ($fact*$ivalue);
} until $calc >= 100;

This is equivalent to

do
{
$calc += ($fact*$ivalue);
} while $calc <100;

foreach Loops

The last loop type is the foreach loop, which has a format like this:

LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK

Using a for loop, you can iterate through the list using:

for ($index=0;$index<=@months;$index++)
{
print "$months[$index]\n";
}

This is messy, because you.re manually selecting the individual elements from the array and using an additional variable, $index, to extract the information. Using a foreach loop, you can simplify the process:

foreach (@months)
{
print "$_\n";
}

The foreach loop can even be used to iterate through a hash, providing you return the list of values or keys from the hash as the list:

foreach $key (keys %monthstonum)
{
print "Month $monthstonum{$key} is $key\n";
}

Labled Loops

Labels can be applied to any block, but they make the most sense on loops. By giving your loop a name, you allow the loop control keywords to specify which loop their operation should be applied to. The format for a labeled loop is:

LABEL: loop (EXPR) BLOCK ...

For example, to label a for loop:

ITERATE: for (my $i=1; $i<100; $i++)
{
print "Count: $i\n";
}

Loop Control – next, last and redo

There are three loop control keywords: next, last, and redo.

The next keyword skips the remainder of the code block, forcing the loop to proceed to the next value in the loop. For example:

while (<DATA>)
{
next if /^#/;
}

Above code would skip lines from the file if they started with a hash symbol. If there is a continue block, it is executed before execution proceeds to the next iteration of the loop.

The last keyword ends the loop entirely, skipping the remaining statements in the code block, as well as dropping out of the loop. The last keyword is therefore identical to the break keyword in C and Shellscript. For example:

while ()
{
last if ($found);
}

Would exit the loop if the value of $found was true, whether the end of the file had actually been reached or not. The continue block is not executed.

The redo keyword reexecutes the code block without reevaluating the conditional statement for the loop. This skips the remainder of the code block and also the continue block before the main code block is reexecuted. For example, the following code would read the next line from a file if the current line terminates with a backslash:

while(<DATA>)
{
if (s#\\$#)
{
$_ .= <DATA>;
redo;
}
}

Here is an example showing how labels are used in inner and outer loops

OUTER:
while(<DATA>)
{
chomp;
@linearray=split;
foreach $word (@linearray)
{
next OUTER if ($word =~ /next/i)
}
}

goto Statement

There are three basic forms: goto LABEL, goto EXPR, and goto &NAME. In each case, execution is moved from the current location to the destination.

In the case of goto LABEL, execution stops at the current point and resumes at the point of the label specified.

The goto &NAME statement is more complex. It allows you to replace the currently executing subroutine with a call to the specified subroutine instead.

Centreon and Nagios 491 250 Roderick Derks

Centreon and Nagios

Since a year I've been working with Nagios, an open source system and network monitoring tool. In the company I work for I check around 300 hosts and almost a thousand services, mostly by using SNMP plugins. Some of them are the standard plugins that come with Nagios and sometimes needed a little tweak to get the right output, others I created myself. With Nagios it's fairly easy to configure sending alerts via email and sms. Some hosts send them during office hours, and others 24 hours a day. This all was a big step forward in actively monitoring our network.

Then I started to use Nagiosgrapher to make some nice graphs of the output of the plugins. For example: diskusage, cpu usage, memory usage, temperatures, nr of processes, load averages, etc. Really cool for detecting trends and this enhanced our ability of pro-actively monitoring servers and network components.

Nagios was accepted and adopted by my collegues (and they are not easy to convince). This was good news, but at the same time I realised I also created a problem. I was about the only one at the office who understood the configuration Nagios: all changes had to go through me and during my long vacations no changes were being made. You have to understand the structure of the files and the directories, and also the dependencies of host, services, serviceextensions, commands, etc to be able to add a server or a service. A little guide for Nagios Dummies did not help. And then offcourse people have to know how to work with Linux and an editor like vi (even if it's weird that there are still professional ict guys who never worked with Linux).

So I was looking for a way to be able to configure Nagios through a webinterface. A few weeks ago I heard about the Centreon project. Centreon is a tool that configures and controls Nagios via the webinterface and stores the output of the Nagios plugins into a MySQL database. This data is used to show the status of the hosts and services like nagios does. And Centreon creates graphs from this data. This all sounded very promissing so I decided to give it a try.

It took me about two weeks to get Centreon running on my Nagios server and I have to say that it was worth the effort. A bit of a problem is the lack of documentation and some of it is in French. So you have to work with it for a while to get the hang of it and to understand what is happening. But don't be afraid, the forum is lively and it turned out to be a great help.

The result is worthwile. After I imported my current Nagios configuration (not well documented on the centreon wiki) I found out that Centreon makes it easier to configure Nagios. You still have to understand a bit how Nagios works, this does not change offcourse, but it's pretty easy do changes or add and delete a server or a service. Centreon uses a database to store the collected data from the plugins and this gives us a lot of options what to do with it.

The website gives us a much more professional look and feel compared to the Nagios site. The graphs are beautiful and the way they are presented in the webinterface is really cool. Centreon is able to produce different graphs of the same or even of different hosts on the same webpage. I have to add the fact that I had to change the output of some of the plugins that I used to create the graphs so this took a while to get things running. But I'm happy with it because it improved the quality of the plugins. By the way, Centreon comes with it's own set of plugins and some of them are very good, like the plugin to get CPU usage of Windows and Linux hosts.

The reporting tool shows the availability of hosts and services and looks good, especially when you take a look at the timeline of the status of a host or a service. Because of the use of AJAX you can scroll back and forth on the timeline. And to my surprise all of the Nagios history was imported into Centreon by itself. So I can scroll back a year even if I just installed Centreon. Really great!

So, I'm using Nagios 3.0.3 and Centreon RC2 with NDO-utis (cvs version). I ran into some Centreon bugs like that some buttons are not exactly doing what they are supposed to do, but this is not a problem. Guess these bugs will be fixed before the stable version is released. You got to use cvs version of NDO tools to get the data from Nagios to the NDO database, the stable version did not work.

Alright, that's it. Hope this article helps you out a bit.

Samba authentication against Active Directory 150 150 Roderick Derks

Samba authentication against Active Directory

A guide to authenticate Samba on a HP UX 11.23 system to Microsoft Active Directory running on W2003 R2.

read more

Which domain controller is the pdc? 150 150 Roderick Derks

Which domain controller is the pdc?

To find out which of you Windows Domain Controller actually is the PDC, execute te following command and check the output:

C:\>nbtstat -a <name of your domaincontroller>

Local Area Connection:
Node IpAddress: [10.20.113.228] Scope Id: []

           NetBIOS Remote Machine Name Table

       Name               Type         Status
    ———————————————
    <name of your domaincontroller>        <00>  UNIQUE      Registered
    <name of your domain>      <00>  GROUP       Registered
    <name of your domain>      <1C>  GROUP       Registered
    <name of your domaincontroller>        <20>  UNIQUE      Registered
    <name of your domain>      <1B>  UNIQUE      Registered
    <name of your domain>      <1E>  GROUP       Registered
    <name of your domain>      <1D>  UNIQUE      Registered
    ..__MSBROWSE__.<01>  GROUP       Registered

    MAC Address = 00-0E-7F-30-08-30
C:\

The Domain Controller that is the PDC contains the line:
    <name of your domain>      <1B>  UNIQUE      Registered

VMware ESX upgrade 3.0.1 to 3.5 150 150 Roderick Derks

VMware ESX upgrade 3.0.1 to 3.5

The command list and some extra info.

read more

VMworld Europe in Cannes 150 150 Roderick Derks

VMworld Europe in Cannes

Très cool, le VMworld à Cannes. Oui, c’est vrai, last month I visited VMworld in Palais des Festivals et des Congrès de Cannes. Where once a year film stars are being celebrated, computergeeks now had the power! Virtual power, so to speak. For three days. And just in the Congrès building, not during the night. And we had to wear badges.

read more

Add a sitemap in Google with Xmap 150 150 Roderick Derks

Add a sitemap in Google with Xmap

Joomla! Extension: Xmap

Xmap is a Joomla! extension based on the popular Joomap component. Xmap allows you to create a map of your site using the structure of your menus.

Detail Information

You can add your sitemap to your Google account in Google Sitemap to provide it with additional information about your site. The objective is clearly, to make your website Google-friendly so it will improve search engine optimization of your website.
How To Add Sitemap in Google Webmaster Tools with Xmap

   1. Install Joomla! Extension, Xmap
   2. In Administrator page, go to menu Components > Xmap. This will open the Xmap Configuration window.
   3. You can add additional menu with click add menu.
   4. Click the blue arrow, choose Preferences menu.
   5. In XML Sitemap, copy and paste the url code to notepad. Just use the url started with index.php. This code will use in Google sitemap.
   6. Go to Google Webmaster Tools. This will open the Dashboard.
   7. Add your site url in Sites field. Click Add Site button. Verify your website first.
   8. Go to Sitemaps menu.
   9. Click Add a Sitemap.
  10. Choose Add General Web Sitemap in drop-down menu.
  11. Paste the XML Sitemap code (remember just add the url started with index.php, don’t add your site url).
  12. Click Add General Sitemap button.

Extend the Xmap capability with Xmap plugin

You can add Xmap plugin (download it from the official website) that can add sitemap from various extension, such as Fireboard, DOCMan, or zOOm Media Gallery. After you download it extract, there is only one file, extension_name.plugin.php. Upload it to administrator > components > com_xmap > plugins folder.

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

      Your Name (required)

      Your Email (required)

      Subject

      Your Message