Solved

Making a scalar from a string.

Posted on 2004-03-20
3
129 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 185
perl match and sort unique result 2 143
perl search and replace 6 171
Perl for loop for 2000 ms 7 105
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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

828 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