appropriate module/hints on how to solve the following problem?

Discussion in 'Perl Misc' started by Shark, May 5, 2007.

  1. Shark

    Shark Guest

    Hi, I'm working on a solution to handle aliases of strings (not
    variables) correctly. The idea is to read in a config file of the
    following format:

    NAME1 alias1 alias2 alias3
    NAME2 blah1 blah2
    NAME3 m1 m2


    After reading in the above, the following two should be equivalent:

    alias1:blah1
    alias2:blah1
    ....
    ....

    I can explain in more detail what functionality I want if anyone has
    specific questions. This seemed like a very simple problem and I can
    cook up a hack in C++ but in perl i'm a newbie. Any help will be
    appreciated.

    thanks!
    Craig
     
    Shark, May 5, 2007
    #1
    1. Advertising

  2. Shark

    Henry Law Guest

    Shark wrote:
    > Hi, I'm working on a solution to handle aliases of strings (not
    > variables) correctly. The idea is to read in a config file of the
    > following format:
    >
    > NAME1 alias1 alias2 alias3
    > NAME2 blah1 blah2
    > NAME3 m1 m2
    >
    >
    > After reading in the above, the following two should be equivalent:
    >
    > alias1:blah1
    > alias2:blah1
    > ....
    > ....


    I'm not sure that I understand what you're trying to do once the "alias
    chain" has been read in. But if "alias1", "alias2" etc are all to map
    to "NAME1", and "blah1", "blah" to "NAME2", then one way that occurs to
    me would be to set up a hash and populate it as you read in the config
    file, so that $hash{NAME1} would be itself, and $hash{alias1} would have
    "NAME1" as its data. Then when you're parsing a string you would be
    able to convert the aliases into the names, and see the name as a
    primitive, so to speak.

    Code, to show what I mean; it's not tested and almost certainly won't work:

    my %alias_hash;
    while (my $config_line = <CONFIG>) {
    my @tokens = split /\S*/,$config_line;
    my $real_name = shift @tokens;
    $alias_hash{$real_name} = $real_name;
    while (my $alias = shift @tokens) {
    $alias_hash{$alias} = $real_name;
    }
    }
    # now when you're parsing an input line you can replace each of its
    # tokens with $alias_hash{$that_token} and you'll end up with a string
    # containing only your NAMEs.

    --

    Henry Law Manchester, England
     
    Henry Law, May 5, 2007
    #2
    1. Advertising

  3. Shark

    Joe Smith Guest

    Shark wrote:
    > Hi, I'm working on a solution to handle aliases of strings (not
    > variables) correctly. The idea is to read in a config file of the
    > following format:
    >
    > NAME1 alias1 alias2 alias3
    > NAME2 blah1 blah2
    > NAME3 m1 m2
    >
    >
    > After reading in the above, the following two should be equivalent:
    >
    > alias1:blah1
    > alias2:blah1


    If I understand you correctly, this might do it:

    my %primary_name;
    while (<$config>) {
    my($primary,@aliases) = split;
    $primary_name{$_} = $primary for @aliases;
    }
    ...
    while (<$data>) {
    chomp;
    my($first,$second) = split /:/,$_;
    my $first_part = $primary_name{$first} || $first;
    my $second_part = $primary_name{$second} || $second;
    print "$first:$second translates to $first_part:$second_part\n";
    }

    That would print

    alias1:blah1 translates to NAME1:NAME2
    alias2:blah1 translates to NAME1:NAME2

    If the first part is the name of a parameter (or its alias) and the
    second part is the value it should be set to, then

    $parameter{$first_part} = $second_part;


    -Joe
     
    Joe Smith, May 5, 2007
    #3
  4. Shark <> wrote:

    > The idea is to read in a config file of the
    > following format:
    >
    > NAME1 alias1 alias2 alias3
    > NAME2 blah1 blah2
    > NAME3 m1 m2
    >
    >
    > After reading in the above, the following two should be equivalent:
    >
    > alias1:blah1
    > alias2:blah1
    > ...
    > ...
    >
    > I can explain in more detail what functionality I want if anyone has
    > specific questions.



    Why is "alias3" missing?

    Why is "m1" missing from the 1st one and "m2" missing for the 2nd one?

    What should it do if there is a 4th line like:

    NAME4 foo alias1 bar

    ??

    > This seemed like a very simple problem



    Then it should be easy to describe it fully enough to be able to
    suggest code that solves it...

    Please make a short (less than 20-30 lines) and *complete* program
    that illustrates the problem you are having. People should be able
    to run your program by copy/pasting the code from your article.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 5, 2007
    #4
  5. Shark wrote:
    > Hi, I'm working on a solution to handle aliases of strings (not
    > variables) correctly. The idea is to read in a config file of the
    > following format:
    >
    > NAME1 alias1 alias2 alias3
    > NAME2 blah1 blah2
    > NAME3 m1 m2
    >
    > After reading in the above, the following two should be equivalent:
    > alias1:blah1
    > alias2:blah1
    > ...
    > ...


    For the last 5 minutes I've been trying to understand what it is you want.
    First of all what do you mean by "the following two should be equivalent":

    Should "alias1:blah1" be equivalent to "alias2:blah1" (that's what you
    wrote) or do you want the elements in each pair to be equivalent, i.e.
    "alias2" is equivalent to "blah1" and "alias2" is equivalent to "blah1"?

    Either way, I can't see the logic how you determine the equivalence based on
    the sample input data.

    In the first option the first two entries from the NAME1 line are each
    combined with the first entry in the NAME2 line and then considered
    equivalent. What happened to alias3? What happens if I combine the second
    entry of the third line ("m3")with the first entry in the first line
    ("alias1") as "m3:alias1"? Is that equivalent to the "alias1:blah1"?

    In the second option what happens to "alias3"? Is that equivalent to
    "blah1", too? Like all elements in the first line are equivalent to the
    first element in the second line? If so then does this propagate to the
    following lines, too, e.g. all elements in a line are equivalent to the
    first element in the next line? I believe this would make all elements
    members of the same equivalence class.

    I think you need to be much more precise in your specification before
    anybody can help you with a solution.

    jue
     
    Jürgen Exner, May 5, 2007
    #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. moongateclimber

    linux/JNI problem: looking for hints

    moongateclimber, Apr 15, 2004, in forum: Java
    Replies:
    3
    Views:
    738
    moongateclimber
    Apr 19, 2004
  2. Replies:
    2
    Views:
    488
  3. Alan Stewart
    Replies:
    0
    Views:
    95
    Alan Stewart
    Feb 9, 2007
  4. Matthew Lefavor
    Replies:
    0
    Views:
    126
    Matthew Lefavor
    Aug 7, 2013
  5. Ian Kelly
    Replies:
    0
    Views:
    120
    Ian Kelly
    Aug 7, 2013
Loading...

Share This Page