Solved

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

Posted on 1998-06-18
13
146 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

860 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