Solved

locate an entry and related information in the file

Posted on 1998-06-17
7
186 Views
Last Modified: 2010-03-04
Hello,
Does any one have an good idea  to implement following question in perl effectively.
suppose there is a file the format is:

metrics        range

entry1           0-12
entry2           0-56
entry3           0.2-13.4
....
....

when the program gets an entry name and its value from user(e.g. "entry2   24"),it can locate this entry and its range in the file.Then check if the given value is within range. and report "success" or "fail" to the user.
0
Comment
Question by:tian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1207859
Before I give you some code, would you please confirm that this is not an assignment for any class? Posting class assignments is against EE rules.

(I haven't heard of any classes giving assignments in perl, but the nature of the problem and the way it's phrased is very assignment-like, and I am just making sure).

0
 

Author Comment

by:tian
ID: 1207860
It's difinitely not.
0
 

Author Comment

by:tian
ID: 1207861
It's difinitely not. In fact, I already implemented it:
read file one line at one time,then use "split" to parse it and commpare corresponding items with user input.....  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1207862
Sorry about that then... here's my solution to it, based on the file format you posted.. if your solution is as good or better then just reject it and ask them to delete this question

open(IN, "<metrics.dta") || die("Open failed - $!\n");
while (<IN>) {
 chomp;
 # data format: <entryname>   <lowerbound>-<upperbound>
 if (/^([a-zA-Z0-9]+)\s+(-*[\d\.]+)-(-*[\d\.]+)\s*$/) {
  $lowerbound{$1}=$2;
  $upperbound{$1}=$3;
 }
 else {
  print "Bad line in metrics file, ignored: $_\n";
 }
}
close(IN);

Then the test routine:

sub TestMetric {
 my $value = pop;
 my $metric = pop;
 if (!exists $lowerbound{$metric}) {
  print "No metric '$metric' found\n";
 }
 elsif ($lowerbound{$metric} <= $value && $value <= $upperbound{$metric}) {
  print "true\n";
 }
 else {
  print "false\n";
 }
}

Hope this helps..
0
 

Author Comment

by:tian
ID: 1207863
thank you very much.
 But how about if metric user given is just part of a compound metric in the file.
for example:

metric_name       range
-----------------------------------
m1                       1~12
m3+m4                23~34
m7                    45~23
.......
what the user input is " (m3  3),(m1 19), (m4 23)..." and the program can make a report indicate the "false" or true" for each metric entry in the file
0
 

Author Comment

by:tian
ID: 1207864
Sould I post modified question again?
0
 
LVL 6

Expert Comment

by:alamo
ID: 1207865
I just now saw your comments... yes, the new question is a different than this one because the compound metrics means you have to save the inputs and loop through the metrics, calculating each one. All the code changes.

I can't take a look at it quite at this moment unfortunately, if the new question is still unanswered later I will answer it, in any event good luck!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question