On Mon, Oct 5, 2009 at 16:49, Slick <jho251...@yahoo.com> wrote:
> Just clarification.  At this time I have not written any of my code (dont' 
> know where to begin yet,
snip

Choose a simple project. If you are already familiar with the standard
UNIX utilities, reimplementing them is a good way to learn the
language.  For instance, here is a minimal version of cat:

#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <>) {
    print $line;
}

Take a look at the man page for cat and try to add some more of its
functionality (like line numbering).

snip
> however the website that I am looking at perl.begin.org seems to have 
> diffrent methods for one item.  I have seen @ for arrays written like this:  
> �...@myarray ,but I have also seen an array per the perl.begin.org writting 
> like:    $primes[$num_primes].  Are they both the same? Because it would be 
> easier in my opinion to write:
>
> @myarray = 4;
>  instead of writing this:
> $primes[$num_primes] = 2;
>
> Am I mising something, or are these two things interchangable?
snip

@a is an array, $a[4] is the 5th element of the array @a.  Examine this code:

#!/usr/bin/perl

use strict;
use warnings;

#decale the array @a
my @a;

#read lines from STDIN
while (my $line = <STDIN>) {
        #if the line matches the word exit by itself, then exit this loop
        last if $line =~ /^exit$/;

        #remove the end-of-line character(s)
        chomp $line;

        #add $line to the end of the array @a
        push @a, $line;
}

#create a list of numbers from 0 to the last index of @a ($#a)
#and loop over that list assigning each item to $i
for my $i (0 .. $#a) {
        #print $i and the corresponding item in @a
        print "$i: $a[$i]\n";
}



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to