# Perl: Incrementing Variable Names in a Loop?

**URL:** <https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792>\
**Category:** Factual Questions\
**Created:** [January 8, 2005, 10:28pm UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792 "2005-01-08T22:28:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluecanary](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bluecanary/32/3614_2.png) [@bluecanary](https://boards.straightdope.com/u/bluecanary)\
**Post date:** [January 8, 2005, 10:28pm UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/1 "2005-01-08T22:28:22Z")

</div>

Is it possible to increment the name of a variable (an array) within a for or foreach loop in standard Perl (not PHP)?

I want to be able to put data from each iteration of the loop in an array with a different name (e.g. @results0 for the first iteration, @results1 for the second iteration and so on).  
I have discussed this with a couple of colleagues and they disagree on whether it is possible (neither of them is particularly experienced in Perl, and neither could explain how it would be done if it could).

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [January 8, 2005, 11:53pm UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/2 "2005-01-08T23:53:51Z")

</div>

Maybe use a multi-dimensional array?

[http://www.suite101.com/article.cfm/perl/94552](http://www.suite101.com/article.cfm/perl/94552)

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [January 9, 2005, 12:03am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/3 "2005-01-09T00:03:29Z")

</div>

To answer your question more directly, I don’t think you can create dynamic variable names with Perl. Creating a multi-dimensional array can serve the same purpose though.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 9, 2005, 12:05am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/4 "2005-01-09T00:05:04Z")

</div>

> [@bluecanary](#):
>
> Is it possible to increment the name of a variable (an array) within a for or foreach loop in standard Perl (not PHP)?
> 
> I want to be able to put data from each iteration of the loop in an array with a different name (e.g. @results0 for the first iteration, @results1 for the second iteration and so on).

Never, ever do this. It’s possible, but involves the use of symbolic references, which leads to all sorts of problems. The correct procedure is to simply use a two-dimensional array. E.g.

$results[$counter] = [1, 2, 3, 4];

or

$results[$counter] = @array;

[Why symbolic references are bad](http://www.perl.com/doc/FMTEYEWTK/style/slide24.html).

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 9, 2005, 12:06am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/5 "2005-01-09T00:06:09Z")

</div>

> [@ccwaterback](#):
>
> To answer your question more directly, I don’t think you can create dynamic variable names with Perl. Creating a multi-dimensional array can serve the same purpose though.

You can, but if you do, I’ll come over to your house and punch you.

---

<div class="post-metadata">

**Author:** ![HMS\_Irruncible](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/hms_irruncible/32/7394_2.png) [@HMS\_Irruncible](https://boards.straightdope.com/u/HMS_Irruncible)\
**Post date:** [January 9, 2005, 12:12am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/6 "2005-01-09T00:12:35Z")

</div>

What Friedo said. You can dynamically create variable names but it’s such a stupid idea that I refuse to be part of it. The multi-dimensional array facility does excactly what you want, and it’s designed with exactly that purpose in mind.

---

<div class="post-metadata">

**Author:** ![bluecanary](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bluecanary/32/3614_2.png) [@bluecanary](https://boards.straightdope.com/u/bluecanary)\
**Post date:** [January 9, 2005, 12:13am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/7 "2005-01-09T00:13:24Z")

</div>

LOL.

I’ve never used a two-dimensional array before though. Are there any pitfalls I need to watch out for in the implementation?

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 9, 2005, 12:31am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/8 "2005-01-09T00:31:12Z")

</div>

Just remember that in Perl, complex data structures are built with references. Square brackets construct an anonymous array reference, and a backslash takes a reference to a named array.

Thus, the following is good:

```auto

my @stuff = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] );
print $stuff[0][2]; # prints "3"

push @stuff, \@another_array;
push @stuff, [qw/this is an array ref/];

```

The following is bad:

```auto

my @stuff = ( ( 1, 2, 3 ), ( 4, 5, 6 ), ( 7, 8, 9 ) );
# that just makes a flat nine-element array

push @stuff, @another_array; # puts another_array in scalar context and pushes its size
push @stuff, ( qw/this is not an array ref/ ); # ditto

```

You can loop over the nested arrays thusly:

```auto

foreach my $rep( @reports ) { 
    foreach my $data( @$rep ) { 
        print "$data
";
    }
}

```

More here:  
[http://www.perldoc.com/perl5.8.4/pod/perlreftut.html](http://www.perldoc.com/perl5.8.4/pod/perlreftut.html)  
[http://www.perldoc.com/perl5.8.4/pod/perldsc.html](http://www.perldoc.com/perl5.8.4/pod/perldsc.html)

---

<div class="post-metadata">

**Author:** ![bluecanary](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bluecanary/32/3614_2.png) [@bluecanary](https://boards.straightdope.com/u/bluecanary)\
**Post date:** [January 9, 2005, 12:41am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/9 "2005-01-09T00:41:59Z")

</div>

OK thanks. Hopefully I’ll be able to implement this in the (rather complex) loop when I go back on Linux tomorrow.

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [January 9, 2005, 1:53am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/10 "2005-01-09T01:53:30Z")

</div>

> [@friedo](#):
>
> You can, but if you do, I’ll come over to your house and punch you.

OK, after you punched me into submission :), exactly how can these dynamic names be created? Just curious.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 9, 2005, 2:08am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/11 "2005-01-09T02:08:32Z")

</div>

> [@ccwaterback](#):
>
> OK, after you punched me into submission :), exactly how can these dynamic names be created? Just curious.

[Here](http://www.perl.com/doc/FMTEYEWTK/style/slide24.html) is an example.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 9, 2005, 2:11am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/12 "2005-01-09T02:11:00Z")

</div>

> [@friedo](#):
>
> [Here](http://www.perl.com/doc/FMTEYEWTK/style/slide24.html) is an example.

Oops, actually that example has a mistake. ++$varname should be ++$$varname. Here’s a [better example](http://www.perl.com/doc/FMTEYEWTK/style/slide25.html).

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [January 9, 2005, 3:33am UTC](https://boards.straightdope.com/t/perl-incrementing-variable-names-in-a-loop/283792/13 "2005-01-09T03:33:49Z")

</div>

> [@friedo](#):
>
> Oops, actually that example has a mistake. ++$varname should be ++$$varname. Here’s a [better example](http://www.perl.com/doc/FMTEYEWTK/style/slide25.html).

OK, got it, and will never do it. 🙂
