Solved

Making a scalar from a string.

Posted on 2004-03-20
3
131 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
[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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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

707 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