Novice how to design this script?

Discussion in 'Perl Misc' started by arc, Jun 4, 2004.

  1. arc

    arc Guest

    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
     
    arc, Jun 4, 2004
    #1
    1. Advertising

  2. 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/clpmisc/clpmisc_guidelines.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
     
    Gunnar Hjalmarsson, Jun 4, 2004
    #2
    1. Advertising

  3. 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.php?option=content&task=view&id=9&Itemid=28

    gtoomey
     
    Gregory Toomey, Jun 4, 2004
    #3
  4. arc

    J Krugman Guest

    Set functions (Was: Novice how to design...)

    In <3763409.4OZSjEB05d@GMT-hosting-and-pickle-farming> Gregory Toomey <> writes:

    >Pass the lists to the "intersection" function below.
    >This returns names/codes common to both.
    >http://www.gregorytoomey.com/index.php?option=content&task=view&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.
     
    J Krugman, Jun 6, 2004
    #4
  5. arc

    Jim Keenan Guest

    (arc) wrote in message news:<>...
    > 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
     
    Jim Keenan, Jun 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ra

    VHDL novice question

    ra, Jul 13, 2004, in forum: VHDL
    Replies:
    6
    Views:
    2,920
    Allan Herriman
    Jul 13, 2004
  2. jbl
    Replies:
    2
    Views:
    487
    Joe Smith
    Dec 16, 2004
  3. sean
    Replies:
    1
    Views:
    621
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  4. mitcheroo

    sub-novice, wants to design game

    mitcheroo, Nov 27, 2005, in forum: Java
    Replies:
    6
    Views:
    383
    Oliver Wong
    Nov 28, 2005
  5. TwelveEighty

    Novice Tomcat design pattern question

    TwelveEighty, Dec 1, 2007, in forum: Java
    Replies:
    9
    Views:
    354
    Juha Laiho
    Dec 3, 2007
Loading...

Share This Page