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

S

Shark

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
 
H

Henry Law

Shark said:
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.
 
J

Joe Smith

Shark said:
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
 
T

Tad McClellan

Shark said:
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.
 
J

Jürgen Exner

Shark said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top