Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello!
I was wondering what is the way of using an auto-incrementing counter as a variable name. Consider you are reading a file:
$counter=0; while(<>) { $counter++; $variable_name = "bin_".$counter; }

How can I then access this name? So like that I will get to know that I have the values "bin_1", "bin_2" etc

Replies are listed 'Best First'.
Re: Use an incrementing counter as a variable
by haukex (Archbishop) on Sep 11, 2017 at 11:46 UTC

    It's theoretically possible, but "it's stupid to use a variable as a variable name".

    Usually, a hash serves well in these situations:

    use warnings; use strict; my $counter = 0; my %hash; while (<>) { $counter++; $hash{"bin_$counter"} = $_; # or whatever } use Data::Dumper; print Dumper(\%hash);
Re: Use an incrementing counter as a variable (arrays)
by LanX (Saint) on Sep 11, 2017 at 12:25 UTC
    I think what you rather want is an array called @bin

    use strict; use warnings; my $counter=0; my @bin; while( my $line = <> ) { $counter++; $bin[$counter] = $line; }

    see perldata#List-value-constructors for details on arrays (numbers as indices) and hashes (strings as keys)

    and please always use

    use strict; use warnings;

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Anonymous one: Variations on this approach are

      c:\@Work\Perl\monks\Anonymous Monk\1199083>perl -wMstrict -MData::Dump + -le "my @bin; ;; while(my $line = <>) { push @bin, $line; } ;; dd \@bin; " junk_1.txt ["line uno\n", "line two\n", "line 3\n"] c:\@Work\Perl\monks\Anonymous Monk\1199083>perl -wMstrict -MData::Dump + -le "my @bin = <>; ;; dd \@bin; " junk_1.txt ["line uno\n", "line two\n", "line 3\n"]
      where junk_1.txt contains
      line uno line two line 3
      Both these variations relieve you of the need to maintain the  $counter counter (assuming it's initialized to 0).


      Give a man a fish:  <%-{-{-{-<

Re: Use an incrementing counter as a variable
by Happy-the-monk (Canon) on Sep 11, 2017 at 11:54 UTC

    Greetings,

    I don't actually get, why you want this. If I read you right, first you are saying you want the counter as a name.

    what is the way of using an auto-incrementing counter as a variable name

    Then at the end you are saying, you want it as a value, and your code shows you add it as a value.

    I will get to know that I have the values "bin_1", "bin_2" etc

    So it looks to me line you have to make up your mind about what it is you want. Nevertheless, this solution gives you both the names and the values:

    my $counter=0; my %names; while(<>) { $counter++; $names{"bin_".$counter} = "bin_".$counter; } use Data::Dumper qw(Dumper); print Dumper \%names;

    So maybe that's a starting point.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: Use an incrementing counter as a variable
by thanos1983 (Parson) on Sep 11, 2017 at 15:54 UTC

    Hello Anonymous Monk,

    I think/assume you want to know the line number as you read the file. If so no need to use counters etc, Perl has got your back. Use the perlvar/Variables related to filehandles/$.. Then combine it with a hash as the fellow monks has provided you with several examples.

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; use feature qw(say); while (<>) { chomp; say $. . "\t" . $_; } __END__ $ perl test.pl test_1.txt 1 Line 1 2 Line 2 3 Line 3

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Use an incrementing counter as a variable
by Anonymous Monk on Sep 11, 2017 at 13:10 UTC
    Never pollute the variable-name space with variables whose names contain data. Always use a hash for this purpose. Period.