INCREMENT and DECREMENT a Variable in Perl
You create a variable and name it $i because you hate typing long variable names.
$i=0;
We are glossing over where $huhu and @gy come from because this is about INCREMENTING.
$huhu="London-Paris-Tokyo-";
@gy=split(/-/, $huhu);
foreach $gy(@gy) {
$i++;
print "$i $gy<br>";
}
Which prints:
1 London
2 Paris
3 France
In plain English:
$i equals zero to start.
Each time through the foreach loop $i gets one added to it.
If we'd have said $i-- we'd have gotten negative numbers printed:
-1 London
-2 Paris
-3 Tokyo
Start Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
$i=0;
$huhu="London-Paris-Tokyo-";
@gy=split(/-/, $huhu);
foreach $gy(@gy) {
$i++;
print "$i $gy<br>";
}
exit;
Perl Increment Code - See in Action (new window)
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
Dojo Matthew A. Russell... | | |
| | | |
| | | |