2

I have a perl script (abc.pl) and 2 config files (one var.pl and one config.txt)

Now

in var.pl

$admin_userid = "admin";
$guest_userid = "guest";

in config.txt - this has the value of user - can be either admin or guest

user=admin/guest

in abc.pl

require var.pl

$get_user = admin or guest (get this value from config.txt)
**$myfinal_userid = ??**

I want the value of myfinal_user_id as admin if user in config.txt is admin and guest if it is guest.

i.e. based on $get_user's value I want the value of userid - ${$get_user}."_userid" eg: if config.txt has user=admin, the $get_user = admin and I want $myfinal_userid = $admin_userid. similarly for guest. This has to be dynamic.

Finally what I want is, know the user from config.txt and based on it, get the userid from var.pl and store that in myfinal_userid.

Let me know how can I achieve this in perl?

3
  • Your explanation is unclear. I don't get what you want. Please try to explain it more clearly. What are you ultimately trying to do? Also tell us the next step. I think your overall process can be optimized so that this problem goes away. Commented Nov 2, 2012 at 12:00
  • Finally what I want is, know the user from config.txt and based on it, get the userid from var.pl and store that in myfinal_userid. Commented Nov 2, 2012 at 12:03
  • Everytime you read something like "variable variable name", hashes solve the problem. Commented Nov 2, 2012 at 14:09

1 Answer 1

5

Use a hash to store the id's:

my %id = ( admin => 'admin',
           guest => 'guest',
         );

my $get_user = 'admin';  # Read this from the config.
my $final_id = $id{$get_user};

my $other_user = 'guest';
my $another_final_id = $id{$other_user};

print $final_id, "\n", $another_final_id, "\n";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.