How can I assign a variable's content to a variable's name in perl?

S0m3oN3

For example

$get = <>;
chomp($get);
$$get = "something";

if user inputted name for $get I want to assign "something" to $name

Arc676

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use a variable's value in a variable name in JavaScript?

From Dev

How can I assign a value to a variable by passing a string of its name?

From Dev

Why can't I assign python's print to a variable?

From Dev

can I assign "pass" keyword to a variable/name?

From Dev

Re: How can I escape meta-characters when I interpolate a variable in Perl's match operator?

From Dev

How do i use variable's value as a dynamic variable name?

From Dev

Can a variable's name contain `-`?

From Dev

Access Perl variable with it's package name in scalar

From Dev

How to create a variable inside a variable's name?

From Dev

Bash - How to use a variable in a variable's name

From Dev

How to print variable's name?

From Dev

Why can't I assign this perl sub to a variable?

From Dev

Why can't I assign a variable to a hash entry in Perl?

From Dev

How can I get file's name from variable with its full path in python?

From Dev

How can I assign an element to variable?

From Dev

How can I assign lineEdits to some variable?

From Dev

How can I assign Cell Part as Variable?

From Dev

How can I assign an element to variable?

From Dev

How can I assign variable from json?

From Dev

How can I count the number of 0's and 1's in a variable?

From Dev

how to assign lines of variable length to a variable in perl?

From Dev

How do I identify the variable's name as a string from self?

From Dev

How can I print to a variable in Perl?

From Dev

Using Varargs, can I assign values to variables based on the variable name?

From Dev

Assign variable content to object property name

From Dev

js: can I assign a class or an id to an html-element that's part of a variable definition?

From Dev

Can I assign a function to a variable?

From Dev

Perl tr operator is transliterating based on the variable's name not its value

From Dev

Perl tr operator is transliterating based on the variable's name not its value

Related Related

  1. 1

    How can I use a variable's value in a variable name in JavaScript?

  2. 2

    How can I assign a value to a variable by passing a string of its name?

  3. 3

    Why can't I assign python's print to a variable?

  4. 4

    can I assign "pass" keyword to a variable/name?

  5. 5

    Re: How can I escape meta-characters when I interpolate a variable in Perl's match operator?

  6. 6

    How do i use variable's value as a dynamic variable name?

  7. 7

    Can a variable's name contain `-`?

  8. 8

    Access Perl variable with it's package name in scalar

  9. 9

    How to create a variable inside a variable's name?

  10. 10

    Bash - How to use a variable in a variable's name

  11. 11

    How to print variable's name?

  12. 12

    Why can't I assign this perl sub to a variable?

  13. 13

    Why can't I assign a variable to a hash entry in Perl?

  14. 14

    How can I get file's name from variable with its full path in python?

  15. 15

    How can I assign an element to variable?

  16. 16

    How can I assign lineEdits to some variable?

  17. 17

    How can I assign Cell Part as Variable?

  18. 18

    How can I assign an element to variable?

  19. 19

    How can I assign variable from json?

  20. 20

    How can I count the number of 0's and 1's in a variable?

  21. 21

    how to assign lines of variable length to a variable in perl?

  22. 22

    How do I identify the variable's name as a string from self?

  23. 23

    How can I print to a variable in Perl?

  24. 24

    Using Varargs, can I assign values to variables based on the variable name?

  25. 25

    Assign variable content to object property name

  26. 26

    js: can I assign a class or an id to an html-element that's part of a variable definition?

  27. 27

    Can I assign a function to a variable?

  28. 28

    Perl tr operator is transliterating based on the variable's name not its value

  29. 29

    Perl tr operator is transliterating based on the variable's name not its value

HotTag

Archive