Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Perl 101 - Part V.
  • rx-
    Posts: 169
    Perl 101 - Part V.

    Control structures and conditionals

    Here are the basic loop operators:

    $r == $x - is $r numerically equal to $x ?
    $r != $x - is $r numerically not equal to $x ?
    $r eq $x - is $r string equal to $x ?
    $r ne $x - is $r string not equal to $x ?


    #!/usr/bin/perl
    use warnings;
    use strict;
    if(1 == 1) { print \"1=1\n\"; }
    if(1 != 2) { print \"1!=2\n\"; }
    if(\"bob\" eq \"bob\") { print \"bob is bob\n\"; }
    if(\"bob\" ne \"tim\") { print \"bob is not tim\n\"; }

    if(\"rx-\" ne \"THEMOSTAWESOMESHITUWILLEVARSEE\") {
    } elsif(\"rx-\" eq \"-rx\") {
    } else {
    print \"i did nuthin!\n\";
    }

    my @array = (\"item1\",\"item2\",\"item3\");

    my $i = 1;
    foreach my $item (@array) { # foreach loop
    print \"$i - $item\n\";
    $i++;
    }

    for ($i=0;$i<5;$++) { # for loop
    print $i.\"\n\"; # C style for loop
    }

    foreach $item (@array) {
    while($item ne \"item2\") { # While loop
    print \"This isnt item2!\";
    }
    }



    Above is nice example of all conditionals and control structs you can possibly need. Please mind that while() can be substituted by until(), which i believe you all will figure out what is it good for :)
    OH, and btw. to be sure you understood it, find out why the program has so stylish output. ( I mean, cmon, it does look nice when its scrolling so fast :) try putting your shell to fullscreen :))