Solved

Making a scalar from a string.

Posted on 2004-03-20
3
124 Views
Last Modified: 2010-03-04
I am implementing a look up table in Perl using Hashes and would like to make a string into a scalar in order to select the appropriate hash - confused? Check out the code below!

%RunTestIdle = (0,"Run-Test-Idle",1,"Select-DR-Scan");
%SelectDRScan = (0,"Capture-DR",1,"Select-IR-Scan");
%CaptureDR = (0,"Shift-DR",1,"Exit1-DR");

This program accesses a text file and pulls out several bits of information
$selector = $zeroOrOne;      #$zeroOrOne is obtained from the text file
$currentState = $array[0];   #$array[0] will be a string and will be equal to one of the hash names
                                          #Eg. $currentState = "SelectDRScan";

so based on the two variables above I would like to impliment the following which should return either "Capture-DR" or "Select-IR-Scan" based on the value of $selector.
$nextState = $currentState{$zeroOrOne};

However this wont work because perl will look for a Hash called %currentState. If I could force the $currentState string to a scalar of the same name it would work.

I could always implement an if statement to figure out which hash to select based on the $currentState but was wondernig if there is a simpler way of doing this.

Any comments or if anyone can suggest a better way to implement a look-up table let me know!

Thanks, - Troy
0
Comment
Question by:Troyh
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 10642831
${$currentState}{$zeroOrOne}
0
 
LVL 84

Expert Comment

by:ozo
ID: 10642845
#or, you could declare
%nextState=(
RunTestIdle => {0,"Run-Test-Idle",1,"Select-DR-Scan"},
SelectDRScan => {0,"Capture-DR",1,"Select-IR-Scan"},
CaptureDR => {0,"Shift-DR",1,"Exit1-DR"},
);
#then do
$nextState = $nextState{$currentState}{$zeroOrOne};
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10643319
Hi ozo,
> $nextState = $nextState{$currentState}{$zeroOrOne};

That's an elegant way for a state machine. Good one!

Cheers!

Stefan
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now