Solved

locate an entry and related information in the file(modified)

Posted on 1998-06-18
13
145 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
-----------------------------------
    m1              0-12
    m2+m3           0-56
    m4              0.2-13.4
    m6/m7           8 -56
    ....

the entry may be compound.What the user input is like:" (m3 3),(m1 19), (m4 23)(m2,23)..." and the program can locate those given input in the file and check if the given value is within range.(However,if program gets "m3 3",it should save this metric and its value and after geting m4 and its value,it then can calculate the value of (m2+m3) and then check the validity).
thanks in advance.
0
Comment
Question by:tian
  • 6
  • 4
  • 3
13 Comments
 

Author Comment

by:tian
ID: 1207881
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207882
Are there any other operators besides + and / ?
Can any m's appear in more than one range?

0
 
LVL 6

Expert Comment

by:alamo
ID: 1207883
I assume "after geting m4 and its value,it then can calculate the value of (m2+m3)" was a typo, and you meant "after gettingm2 and its value...".

If there is a coumpound metric and the user doesn't enter values for all the needed values, should there be an error message or does in that case the metric just not apply?
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 84

Expert Comment

by:ozo
ID: 1207884
If there's a non-compound metric and the user doesn't enter a corresponding value, should there be an error message?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207885
open(INPUT,"<user.input") or die "can't open user.input: $!";
{local $/=")";
 while( <INPUT> ){
   unless( ($m,$v) = /(\w+)\W+(\d+)/ ){
      warn $_; next;
   }
   $m{$m} = $v;
  }
}
open(METRICS,"<metric.file") or die "can't open metric.file: $!";
while( <METRICS> ){
  chomp;
  unless( ($m,$min,$max) = /(\S+)\s+(-?[^-]+)-(.*)/ ){
    warn $_;
    next;
  }
  ($e=$m)=~s/(\w+)/\$m{$1}/g;
  $error = '';
  {local $^W=1; local $SIG{__WARN__} = sub{ $error=$_[0]};
  $v = eval $e;
  }
  warn "$m: $error" if $error;
  warn "$m: $@" if $@;
  warn "$_: invalid\n" if $v<=$min || $max <= $v;
}

0
 
LVL 6

Expert Comment

by:alamo
ID: 1207886
Very nicely done, ozo. I would have done it a bit differently, because I made some different assumptions reading the question (negative values are allowable, range endpoints are allowable, metrics where none of the values are present are allowable, and an affirmative message when a metric passed). It could well be none of those assumptions are true, though.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207887
I thought I allowed negative values?  (though not negative exponents in range values)
Exact equality of floating point values is not to be trusted anyway, so I thought I'd let undefined 0 values end up invalid.
(although the user input values seem to be integers...
that may have been a bad assumption)
There's also potential for the eval to do nasty things if the right metrics are passed.

It may also be interesting to see how you would have done it with other assumptions.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1207888
\W+ when reading user.input gobbles a leading negative sign.

Here's my version, based on the assumptions above:

(all lines up to and including "$error = '';" are the same, I am replacing the rest of the program from there):

 $goodmetrics=0;
 ($e=$m)=~s/(\w+)/&checkmetric($1)/ge;
 if ($error) {
  print $error unless $goodmetrics==0;
 }
 else {
  $v = eval $e;
  if ($v >= $min && $v <= $max) {
   print "$m: Success\n";
  }
  else {
   print "$m: failed ($v)\n";
  }
 }
}

sub checkmetric {
 my $n = pop;
 if (exists $m{$n}) {
  $goodmetrics++;
  $m{$n};
 }
 else {
  $error .= "$m: Unknown metric '$n'\n";
  "";
 }
}
0
 
LVL 6

Expert Comment

by:alamo
ID: 1207889
Hmm, should have added  a "print "$m: $@" if $@; " like you did, forgot that part, but you got the idea.
0
 

Author Comment

by:tian
ID: 1207890
Thank both of you. Please submit your solution as answer.I would like to close this question.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1207891
tian, only one of us can provide the "answer"... you need to pick one of us, or else post another question just to give the other points.
0
 

Author Comment

by:tian
ID: 1207892
Ok,the second one
0
 
LVL 6

Accepted Solution

by:
alamo earned 200 total points
ID: 1207893
Thanks, tian. The answer is as posted in the comments below.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

809 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