malik51
malik51

Reputation: 23

Modified variable variables

$m = 'this_is_m';
$this_is_m = 'this_is_m_not_full :(';
$this_is_m_full = 'this_is_m_FULL!! :D';

print ${$m};

(Hi first :P) Outputs:

this_is_m_not full :(

Any idea how to output this_is_m_FULL!! :D using $m??

What I've already tried:

print $"{$m}_full";
print $'{$m}_full';
print $'$m_full';
print ${$m}_full';

None worked... Thanks in advance,,

Upvotes: 1

Views: 88

Answers (3)

domvoyt
domvoyt

Reputation: 416

The following should work:

print ${$m.'_full'};

This is because the string inside the braces will get evaluated first, becoming

print ${'this_is_m' . '_full'}  
  ->  print ${'this_is_m_full'}  
  ->  print $this_is_m_full

Take a look at this manual page if you want more information on this.

Upvotes: 1

aurora
aurora

Reputation: 9627

The solution would be:

print ${$m . '_full'};

But that feels very hackish.

Upvotes: 2

user142162
user142162

Reputation:

To get your desired output, you need to do the following:

print ${$m . "_full"};

Upvotes: 2

Related Questions