Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Novice how to design this script? (http://www.velocityreviews.com/forums/t886743-novice-how-to-design-this-script.html)

arc 06-04-2004 11:45 AM

Novice how to design this script?
 
Hi There,

I an currently tying myself in knots trying to write a perl script
which will basically get from one file a list of stations and from
another file another list of stations and then to write to third file
the common station names. If the names are different to get the
alternative from a mappings file. The common factor in all of this is
an identifying code which each of the stations has.

I have a very basic understanding of scalars, lists, and hash's.

---
arc

Gunnar Hjalmarsson 06-04-2004 11:48 AM

Re: Novice how to design this script?
 
arc wrote:
> I an currently tying myself in knots trying to write a perl script
> which will basically get from one file a list of stations and from
> another file another list of stations and then to write to third
> file the common station names.


Are you? Then post what you have so far, and somebody will likely help
you correct possible mistakes.

But before posting, please study the posting guidelines for this group:

http://mail.augustmail.com/~tadmc/cl...uidelines.html

to see what you should do before posting. Btw, they also include
advice with respect to the subject line of a post.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Gregory Toomey 06-04-2004 12:42 PM

Re: Novice how to design this script?
 
arc wrote:

> Hi There,
>
> I an currently tying myself in knots trying to write a perl script
> which will basically get from one file a list of stations and from
> another file another list of stations and then to write to third file
> the common station names. If the names are different to get the
> alternative from a mappings file. The common factor in all of this is
> an identifying code which each of the stations has.
>
> I have a very basic understanding of scalars, lists, and hash's.
>
> ---
> arc




Put each set of station names or codes into a list. Pass the lists to the "intersection" function below.
This returns names/codes common to both.
http://www.gregorytoomey.com/index.p...id=9&Itemid=28

gtoomey

J Krugman 06-06-2004 02:12 PM

Set functions (Was: Novice how to design...)
 
In <3763409.4OZSjEB05d@GMT-hosting-and-pickle-farming> Gregory Toomey <nospam@bigpond.com> writes:

>Pass the lists to the "intersection" function below.
>This returns names/codes common to both.
>http://www.gregorytoomey.com/index.p...id=9&Itemid=28


Hi, Greg. First, a typo (or two) in your page:

Example: Return the elements that occur more than once:

* duplicate(['apple','banana', 'pear', 'banana'])

Example: Return the elements that occur only once:

* duplicate(['apple','banana', 'pear', 'banana', 'apple'])

The "duplicate" is duplicated. I think you wanted the second one
to be "unique" (it's the only function defined on that page that
does not correspond to an Example). Also (and assuming you indeed
intended the second example above to refer to "unique"), the wording
of your description is ambiguous. My first reading was that

unique(['apple','banana', 'pear', 'banana', 'apple'])

would return ('pear') (the only element that occurs exactly once
in the input list, whereas, looking at the code I see that it would
return ('apple', 'banana', 'pear').

Also, a couple of suggestions. A set theorist would say that union
and intersection can take any number of arguments (including zero
or uncountably many!). That's hard to implement in Perl, but at
least one could generalize your functions to handle @_ arrays of
any size (including zero). Also, since you're implementing sets
as array refs, shouldn't union and intersection return array refs
as well?

# UNTESTED

sub union {
die "union: bad args\n" if grep defined $_ && ref $_ ne 'ARRAY', @_;

# return undef (= "the universe") if one of the arguments
# is undef
return undef if grep !defined, @_;

my %hash = map +($_ => 1), map @$_, @_;
[ keys %hash ];
}

sub intersection {
die "intersection: bad args\n" if grep defined $_ && ref $_ ne 'ARRAY', @_;

# the line below is so that we ignore any element in @_ that
# is the universe, since it does not affect the intersection
my @args = grep $_, @_;

# return undef (= "the universe") if @_ is empty
return undef unless @args;

my %hash;
++$hash{$_} for map @$_, @args;
[ grep $hash{$_} == @_, keys %hash ];
}

jill
--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.


Jim Keenan 06-06-2004 05:40 PM

Re: Novice how to design this script?
 
fastidious007@fastmail.fm (arc) wrote in message news:<fd80715e.0406040345.574ba01e@posting.google. com>...
> Hi There,
>
> I an currently tying myself in knots trying to write a perl script
> which will basically get from one file a list of stations and from
> another file another list of stations and then to write to third file
> the common station names. If the names are different to get the
> alternative from a mappings file. The common factor in all of this is
> an identifying code which each of the stations has.
>

Have a look at CPAN module List::Compare:
http://search.cpan.org/~jkeenan/List-Compare-0.3/

Jim Keenan


All times are GMT. The time now is 07:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.