The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix Arithmatic operation issue , datatype issue thambi Shell Programming and Scripting 23 02-19-2008 07:19 AM
issue with if loop in perl amitrajvarma Shell Programming and Scripting 4 01-09-2008 12:02 AM
perl problem - another 'die' issue. mjays Shell Programming and Scripting 4 08-15-2007 08:36 AM
perl help with pipes and file handles (simple issue) the_learner Shell Programming and Scripting 1 05-06-2007 05:34 AM
Perl problem (compiling issue) 01000101 Shell Programming and Scripting 3 05-24-2006 10:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-12-2008
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 138
Question perl issue ..

hi
one perl issue i have xml file with 2 values and one condition b.w them
Code:
<rule>
     <val1>12</val1>
     <cond>and</cond>
     <val2>13</val2>
</rule>
i read these values in hash in perl code
$one{val1} = 12
$one{cond} = and
$one{val2} = 13
now i want to form query from this
like if val >= val1 and val <= val2 then do something how to do this in code ?

Code:
if ( val >=  $one{val1} $one{cond} val <= $one{val2} )
this throws error how can i write this kind of statement ? i want this for server monitor script.
  #2 (permalink)  
Old 09-12-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That's a pretty wicked idea, but if you really think you want to do it,

Code:
if (eval "$val => $one{val1} $one{cond} $val <= $one{val2}")
... assuming the "val" in your example is really actually $val

You should realize that with the double quotes, what eval sees is just a string, no variables. Compare: print "$val => $one{val1} $one{cond} $val <= $one{val2}" -- eval will evaluate what the corresponding print prints.
  #3 (permalink)  
Old 09-12-2008
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 138
thanx for reply i think i should explain u whole situation

i have unix server monitor script in which i have many rules which are hard coded so even for slightest change i have to change code so i have created xml file with no of primary conditions , relation bw them like ( and / or ) actual condition and so on ...

i spent entire day on new code architecture but i think i have to go with little hard coding even i thought its pretty wicked idea but thanx for ur reply i learned something new ....
  #4 (permalink)  
Old 09-13-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
You can use a look up hash that points to subrotuines depending on the operator, an short example:

Code:
%com = (
   and => \&and,
);	

$one{val1} = 9;
$one{cond} = 'and';
$one{val2} = 13;
$val = 10;

if ($com{$one{'cond'}}->($val,$one{'val1'},$one{'val2'})) {
   print "true";
}

sub and {
   my ($v0,$v1,$v2) = @_;
   return(1) if ($v0 >= $v1 && $v0 <= $v2);
   return(0);
}
Note that '=>' is not a comparison operator, it is the comma operator.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 06:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0