-2

I would like to ask a help to create a Perl code where I could create alphanumeric sequential vars (that could be used as array, hash or any other kind of var).

for ( my $x = 1; $x <= 10; $x++ ){
  my $var$x = "" *# to create empty variable with the word 'var' + the integer from x (var1, var2, var3, ...)* 
  for ( my $y = 1; $y < 10; $y++){
    my $var$x = $var$x.''.$x.''.$y *# to store/concatenate the values from $x+$y into var$x*
  }
  print "$var$x"
}

What should print:

var1 = 11, 12, 13, 14, 15, 16, 17, 18, 19
var2 = 21, 22, 23, 24, 25, 26, 27, 28, 29

and so on

Thank you

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • 1
    I don't understand what you want to do. Please [edit] your question and clean up your code. It's not valid syntax. There's also no alphabetical values in your sequence of numbers. It's just numbers. The `var` part is not a variable. Variables in Perl have a _sigil_. That's the thing in front of the name, like the `$` for a scalar value, the `@` for an array and the `%` for a hash. – simbabque Sep 07 '17 at 12:24
  • You question is still unclear. It is not clear what you are trying to get. You code is having error. What you mean by alphanumeric? In your sample output is not an alphanumeric output. – serenesat Sep 07 '17 at 13:09
  • 3
    [When you find yourself adding an integer suffix to variable names, think "**I should have used an array**".](https://stackoverflow.com/a/1829927/100754) See also [How can I use the value of a variable as a variable name in Perl?](https://stackoverflow.com/q/2314507/100754) – Sinan Ünür Sep 07 '17 at 13:14
  • Sorry for the code, but what I need is to create a new variable named by the word VAR + the integer from $x in every loop (var1, var2, var3,...). Having the variables created, they going to store the values from the first and second FOR loop (11, 12, 13...; 21, 22, 23...). Also, I am OK to create arrays within all first for loop, I just need some guidance on how to create and how to store the second loop values in it. – Emerson Soares Sep 07 '17 at 13:18
  • 1
    I have edited your code to add some indentation. You're welcome, but please do it yourself in the future. If you want a large group of strangers to read and understand your code, then surely it is polite to make it as easy to read as possible. – Dave Cross Sep 07 '17 at 13:49

2 Answers2

5

What you are asking for is a very bad idea. You want to create a variable, using the value of another variable as part of the name. This is known as "symbolic referencing" and there is a very good reason why it is one of the three things that use strict turns into a fatal error.

For a good discussion of the problems it can cause, see these three articles by Mark Dominus.

Almost certainly, the best solution to your problem is to use an array, hash or some other (more complex) data structure. But without knowing a lot more about what you're doing it is hard to make any concrete suggestions.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Thank you. In case of using hash or arrays, is it possible to create an incremental key's name, than I can add the second loop values to it? – Emerson Soares Sep 07 '17 at 13:46
  • @EmersonSoares: Well Perl has pre- and post-increment operators that work on strings as well as numbers (`$x="a";say ++$x`). So I assume you can do what you want, although it's far from clear to me what an "incremental key's name" is. – Dave Cross Sep 07 '17 at 13:55
3

Don't try to generate variable names!

All you need is

for my $x (1..10) {
   for my $y (1..9) {
      print "$x$y\n";
   }
}

Or if you want to populate a data structure instead of printing,

my @matrix;
for my $x (0..9) {
   for my $y (0..8) {
      $matrix[$x][$y] = ($x+1).($y+1);
   }
}

Same as previous:

my @matrix;
for my $x (1..10) {
   my @row;
   for my $y (1..9) {
      push @row, "$x$y";
   }

   push @matrix, \@row;
}
ikegami
  • 367,544
  • 15
  • 269
  • 518