Home > Archive > PERL Beginners > November 2005 > comparing arrays of strings
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
comparing arrays of strings
|
|
|
| Let's say I have, say, 4 arrays of strings.
A,B,C,D,G,I,H
A,C,E,G,H,K
A,B,C,G,H,L
A,C,F,G,H,M
As you can see, some of the strings are present in all four arrays. I
want to write a Perl script that will scan through these arrays and
pull out only those strings that exist in all four arrays and then
display them. Possible? What's the word for this process so I can find
a tutorial on it?
thanks,
matt
| |
| Purl Gurl 2005-11-27, 9:55 pm |
| Matt wrote:
> A,B,C,D,G,I,H
> A,C,E,G,H,K
> A,B,C,G,H,L
> A,C,F,G,H,M
>
> As you can see, some of the strings are present in all four arrays. I
> want to write a Perl script that will scan through these arrays and
> pull out only those strings that exist in all four arrays and then
> display them.
Commonly called an "intersection" in Perl, there are two rules,
both of which cannot be violated.
Each array cannot contain duplicate elements.
The total number of arrays must be set within this example code.
Premise is simple. A hash is created which keeps count of how many
times a unique element is found. If found a total number of times equal
to the total number of arrays, that element is in all arrays.
Again, no array can contain duplicate elements; this will fail if so.
Two versions follow. First example stores the elements common to
all arrays, in a new array @Intersection should you need to work
with those elements.
Second version simply prints each element common to all arrays.
if ($Hash{$element} == 4)
For that line, 4 is the total number of arrays. You can set this by hand,
or write code which counts how many arrays are present.
Purl Gurl
#!perl
@Array1 = qw (A B C D G I H);
@Array2 = qw (A C E G H K);
@Array3 = qw (A B C G H L);
@Array4 = qw (A C F G H M);
foreach $element (@Array1, @Array2, @Array3, @Array4)
{ $Hash{$element}++; }
foreach $element (keys %Hash)
{
if ($Hash{$element} == 4)
{ push (@Intersection, $element); }
}
print "@Intersection";
#!perl
@Array1 = qw (A B C D G I H);
@Array2 = qw (A C E G H K);
@Array3 = qw (A B C G H L);
@Array4 = qw (A C F G H M);
foreach $element (@Array1, @Array2, @Array3, @Array4)
{ $Hash{$element}++; }
foreach $element (keys %Hash)
{
if ($Hash{$element} == 4)
{ print "$element\n"; }
}
| |
| usenet@DavidFilmer.com 2005-11-27, 9:55 pm |
| Matt wrote:
> Let's say I have, say, 4 arrays of strings.
>
<snip>
> As you can see, some of the strings are present in all four arrays. I
> want to write a Perl script that will scan through these arrays and
> pull out only those strings that exist in all four arrays and then
> display them. Possible? What's the word for this process so I can find
> a tutorial on it?
The word for the process is "intersection." Are you doing this as a
learning exercise? Because it's already been written for you in the
List::Compare module from CPAN
http://search.cpan.org/~jkeenan/Lis...List/Compare.pm
For example:
#!/usr/bin/perl
use warnings; use strict;
use List::Compare;
my @list1 = qw/A B C D G I H/;
my @list2 = qw/A C E G H K/;
my @list3 = qw/A B C G H L/;
my @list4 = qw/A C F G M H/;
my $lc = List::Compare -> new(\@list1, \@list2, \@list3, \@list4);
print $lc->get_intersection;
__END__
### Output: ACGH
| |
| usenet@DavidFilmer.com 2005-11-27, 9:55 pm |
| Purl Gurl wrote:
> Again, no array can contain duplicate elements; this will fail if so.
FWIW, the List::Compare module doesn't care if arrays contain
duplicates.
| |
| Purl Gurl 2005-11-27, 9:55 pm |
| usenet wrote:
> Purl Gurl wrote:
(snipped)
> FWIW, the List::Compare module
Use of a module will only teach the author how to become a "copy and paste baby."
My preference is to exemplify how a reader can become a talented Perl programmer.
Purl Gurl
| |
|
| Wow...thanks, guys. I'll play with both of these: one for the "i really
want to learn Perl" side of me, and one for the "I really want to have
this project done" side.
| |
| Purl Gurl 2005-11-28, 7:55 am |
| Matt wrote:
(snipped)
> "I really want to have this project done"
You should be writing,
"I really want to have this project done correctly and with quality."
In this thread you are presented with a correct method of high
quality, and a frequently incorrect method of lesser quality.
Explain to readers why this is so.
Purl Gurl
| |
| usenet@DavidFilmer.com 2005-11-28, 6:57 pm |
| Purl Gurl wrote:
> In this thread you are presented with a correct method of high
> quality, and a frequently incorrect method of lesser quality.
The OP may disregard. PG has not yet embraced the concept of code
reuse. She would rather re-invent the wheel each time instead of using
a robust and reliable module from CPAN. It's her right to do so, but
her opinion is not shared by most of the Perl programming community.
| |
| Purl Gurl 2005-11-28, 6:57 pm |
| Purl Gurl wrote:
> Matt wrote:
(snipped)
[color=darkred]
> You should be writing,
> "I really want to have this project done correctly and with quality."
> In this thread you are presented with a correct method of high
> quality, and a frequently incorrect method of lesser quality.
> Explain to readers why this is so.
In lieu of a response, I will enlighten readers.
The module mentioned in this thread is not bundled with Perl.
To use that module, you must download it, perhaps from CPAN,
then install it, hopefully without all the install errors so common
to packaged modules.
Additonally, if a reader is using a Win32 platform, the module
package cannot be decompressed unless a reader knows to
remove the last ".tar" from the package extension.
Should a Win32 platform reader figure out how to decompress
the package, that reader still needs to know using "make" will
fail; use of Win32 "nmake" is required.
Most modules cannot be installed on Win32 platforms.
For all platforms, a module may or not may install because
of fixed paths in the installation package. If system paths are
different, a reader must know how to edit an install package
to work correctly.
Many modules depend on other modules, as this specific
module depends on Carp, which is bundled with Perl. Should
a supporting module not be installed, you have to install another
module or a series of modules.
You install a module then discover you cannot use it until you
find, install and configure other modules.
Once installed, a user will discover use of time tested methods
are anywhere from three-hundred percent to eight-hundred
percent more efficient, than a module, assuming a reader
can figure out correct syntax for a module.
For production, code using this module will almost always fail.
Almost all systems out there, do not have this module installed.
You place others in a position of having to install the module,
which, as you should know, can be a nightmare, or simply
not allowed by system administration.
If not allowed, a module can be installed in a custom directory,
in violation of acceptable use policies.
After jumping through a lot of hoops, in the end, you discover
a module will never work because you do not have the right
version of Perl installed.
Many modules are very nice. There are some I use frequently.
Contrasting this, many modules are garbage.
I am not one to promulgate use of modules blindly. I am not
one to deceive readers by proclaiming, "Use this module!
Use that module!" without being honest by warning readers
they might encounter a lot of problems trying to use a module.
Readers should consider using modules with great caution.
I have found there is no substitute for excellent coding techniques,
and have found use of modules creates "copy and paste babies"
who know diddly-squat about Perl programming.
Purl Gurl
|
|
|
|
|