For example
$get = <>;
chomp($get);
$$get = "something";
if user inputted name for $get I want to assign "something" to $name
For example
$get = <>;
chomp($get);
$$get = "something";
if user inputted name for $get I want to assign "something" to $name
There are ways to do this, but you really shouldn't. Use a hash instead.
use strict;
use warnings;
my $get = <>;
chomp $get;
my %input;
$input{$get} = "something";
A "hash" (or "dictionary" in some other languages) is like* an array of key-value pairs. This achieves what you describe because you can assign a value to a variable without having a fixed identifier for that variable.
*: Hashes are not arrays.
See this tutorial on Perl101. The following code snippets in this answer are taken from this site.
You can create a hash using the following syntax:
my %stooges = (
'Moe', 'Howard',
'Larry', 'Fine',
'Curly', 'Howard',
'Iggy', 'Pop',
);
Access and modify values using {}
. You can use this syntax to add new values too.
print $stooges{'Iggy'}; #get existing
$stooges{'Iggy'} = 'Ignatowski'; #Overwrite
$stooges{'Shemp'} = 'Howard'; #Set new
You can delete key-value pairs using the delete
function.
delete $stooges{'Curly'};
You can get a list of values or keys using the same (key)words:
my @stooge_first_names = keys %stooges;
my @stooge_last_names = values %stooges;