Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

resolving variable names within variable names.

by Octavian (Monk)
on Jun 04, 2002 at 14:07 UTC ( [id://171482]=perlquestion: print w/replies, xml ) Need Help??

Octavian has asked for the wisdom of the Perl Monks concerning the following question:

This question is mainly for the UNIX users out there...I need to write a program that will display all the configurable kernal parameters on a system, show their formula, and show their resolved value. what I have so far is below, but to explain it first, I run kmtune -S /stand/system and throw it into an array to get all the parameters I need, and it displays the formula for them if any....the hexidecimal values are no problem, along with evaluating simple formulas...where I run into a snag is when a formula relys on the result of another parameter. I guess I dont understand enough about how to get around this to be able to explain it correctly, but if you know what kmtune's output looks like, you will see where I am running into a snag...here is what I got so far..
#!/usr/local/bin/perl chomp(@datafile = `/usr/sbin/kmtune -S /stand/system`); foreach (@datafile) { tr/[A-Z]/[a-z]/; ($key,$value) = split(/\s+/); if ($key eq "Parameter" || $value eq "") { next; } if (grep/^0x/i,$value) { $value = hex($value); } $var{$key} = $value; } foreach $key(keys %var) { ### Evaluating the hash variable generates errors on the ones that hav +e varibales embedded in them. #print "$key:", eval $var{$key}, "\n"; ### This works, but doesnt resolve formulas in the variables. print "$key:", $var{$key}, "\n"; }
Any help or ideas would be greatly appreciated...and if there is another way to get the formulas AND the end result, that would work, but I would prefer to find a nifty perly way to do it :)

Replies are listed 'Best First'.
(jeffa) Re: resolving variable names within variable names.
by jeffa (Bishop) on Jun 04, 2002 at 15:43 UTC
    We discussed this problem recently at Parse C-like define statements. Using a variation on the solution presented by danger and sfink:
    # after your first foreach loop foreach my $key (keys %var) { 1 while $var{$key} =~ s/([a-z_]+)/$var{$1}/; $var{$key} = eval "$var{$key}"; }
    should do the trick.

    On another note - i would remove the first line in your first foreach loop (the one with the tr) and change the last line of that loop to:

    $var{$key} = "\L$value\E";
    UPDATE - or maybe not :) - i overlooked the fact that the first few lines of the output have uppercase keys - my mistake.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      you da man...that worked great....I thank you for that...I get in that deep and my brain starts to hurt....course now looking at the logic of it it seems easy :P
Re: resolving variable names within variable names.
by Octavian (Monk) on Jun 04, 2002 at 14:58 UTC
    I got a couple requests from people to display some output from kmtune, since alot of you dont have it...so here is all of the output from kmtune: as you can see...some of the values are numbers, but other are math formulas...and some are math formulas that contain other variables in it.... eventually it all resolves down to just a number... hope this helps :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://171482]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2025-11-12 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.