Zesa Rex Zesa Rex - 20 days ago 4x
Perl Question

Is there a way to create variable Arrays in Perl?

So, well I am trying around again and now I am stuck.

while (<KOERGEBNIS>){
my $counter = 0;
my $curline = $_;

for (my $run = 0; $run < $arrayvalue; $run++){
if ($curline =~ m/@tidgef[$counter]/){
my $row = substr($curline, 0, 140);
push @array$counter, $row;
print "Row $. was saved in ID: @filtered[$counter]\n";


Background is that I want to save all lines beginning with the same 8 characters in the same array so I can count the lines and start working with those arrays. The only thing I could think of right now is with switch and cases but I thought I'd ask first before throwing this code to garbage.

if theres a line in a .txt like this:


I took the first 8 chars of each line and used uniq to filter duplicates and saved them in the array @tidgef, now I want to save Line1 and Line2 in @array1 or even better @array50004000 and Line4 and Line4 to @array2 or @array60004001.

I hope I explained my problem well enough! thank you guys


You're hovering dangerously close to an idea called "symbolic references" (also known as "use a variable to get a variable's name"). It's a very bad idea, for all sorts of reasons.

It's a much better idea to use this as an excuse to learn about complex data structures in Perl. It's not really clear what you want to do with this data, but this example should get you started:


use strict;
use warnings;
use 5.010;

use Data::Dumper;

my %lines;

while (<DATA>) {
  my $key = substr($_, 0, 8);
  push @{$lines{$key}}, $_;

say Dumper \%lines;