?
Solved

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

Posted on 1998-06-18
13
Medium Priority
?
149 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 800 total points
ID: 1207893
Thanks, tian. The answer is as posted in the comments below.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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.…
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

770 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