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?

Avinash Sonee
  • 1,701
  • 2
  • 15
  • 17
  • 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. – simbabque Nov 02 '12 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. – Avinash Sonee Nov 02 '12 at 12:03
  • Everytime you read something like "*variable variable name*", hashes solve the problem. – memowe Nov 02 '12 at 14:09

1 Answers1

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";
choroba
  • 231,213
  • 25
  • 204
  • 289