Front page | perl.beginners |
Postings from February 2009
Re: Reading 2 array lists
From: Chas. Owens
February 23, 2009 06:43
Re: Reading 2 array lists
Message ID: firstname.lastname@example.org
On Fri, Feb 20, 2009 at 13:53, mritorto <email@example.com> wrote:
Chas. Owens, not Owen.
> I got it to work using your script and the list compare module
> I figured that I had to copy the files to a certain directory . The
> directions cspan gave were wrong for windows
You should probably not be using CPAN if you are on windows (unless
you are using Strawberry Perl or Cygwin Perl, but in those cases CPAN
will do the right thing). Use the PPM* command instead.
> what does this mean?
> my $comparison = List::Compare->new(\@atlas, \@isis);
List::Compare is a class. new is one of its methods. The new method
instantiates a object which is stored in $comparison. This object
holds all of the information about the comparison of @atlas and @isis.
There are \s on the two arrays because the new method wants
references to the arrays instead of the arrays themselves. This is
for at least two reasons:
If they were passed as new(@atlas, @isis), then you would not know
where @atlas stops and @isis starts inside the method. Perl has
variadic parameters**. This means a function will take any number of
arguments. Because they take any number of arguments arrays are
flattened (that is turned in to lists) during the call.
The other reason is it is more efficient to pass two references than
to pass however many items are in both @atlas and @isis.
> print $comparison->get_symmetric_difference; ( the words from
> $comparsion on ward)
The $comparison object holds all of the information about the
comparison. We just need to ask it the right question to get it to
tell use what we want to know. The get_symmetric_difference method is
that question. I found the method by looking through the docs***.
The description says "Get those items which appear at least once in
either the first or the second list, but not both." This seemed to be
what you desired.
> and are these certain modules
> use warning
> use strict
Modules that do not start with an uppercase letter are called pragmas
(or pragmata, depending on whether you think there are octopuses in
the sea or octopi). Pragmas are pragmatic instructions to the Perl.
That is they change the way Perl works. The warnings pragma tells
Perl to print a message to STDERR every time it sees something that
might be an error (such as using an undefined value). The strict
pragma enforces a stricter programing style than the one Perl allows
by default. Specifically it forces you to declare variables (with my,
our, or use vars) and turns off two things: symbolic references (we
don't need them anymore, we have real references) and the
interpretation of barewords as strings. The declaration of variables
is what we really want, the other two are just misfeatures of the
language we are turning off.
ActivePerl comes with the entirety of perldoc in HTML form. Look in
your start menu for ActiveState. You can also use perldoc.perl.org.
It too contains the entirety of perldoc. I would suggest starting at
http://perldoc.perl.org/perl.html. You may want to buy Learning
Perl**** and Programming Perl*****.
** by default that is; however, there are things called prototypes
that can change this, but they were an experiment and are
fundamentally broken and should not be used unless you know exactly
what the are good for.
The most important skill a programmer can have is the ability to read.