Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > appropriate module/hints on how to solve the following problem?

Reply
Thread Tools

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

 
 
Shark
Guest
Posts: n/a
 
      05-05-2007
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

 
Reply With Quote
 
 
 
 
Henry Law
Guest
Posts: n/a
 
      05-05-2007
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
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      05-05-2007
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-05-2007
Shark <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-05-2007
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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
Re: Solve all the Problems in this Group by following this link... Dr. Bill Computer Support 5 09-16-2005 07:44 PM
Requesting suggestions for appropriate use of VLANs Jamie Cisco 1 09-17-2004 08:58 PM
RE: The Web server reported the following error when attempting to create or open the Web project located at the following URL: <URL> =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?= ASP .Net 0 06-07-2004 07:36 AM



Advertisments