Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Declaring and checking content of variables with consecutive names

by hippo (Chancellor)
on Sep 29, 2016 at 08:27 UTC ( #1172899=note: print w/replies, xml ) Need Help??


in reply to Re^2: Declaring and checking content of variables with consecutive names
in thread Declaring and checking content of variables with consecutive names

That approach would suffer from exactly the same problems as consecutively enumerated scalar variables. Perl has arrays of arrays so this is easily avoided by having just one single outer array each of whose elements are themselves an array. That way your code could declare just one @arr and then index into it for individual subarrays or within them for individual values.

  • Comment on Re^3: Declaring and checking content of variables with consecutive names
  • Download Code

Replies are listed 'Best First'.
Re^4: Declaring and checking content of variables with consecutive names
by rflesch (Novice) on Sep 29, 2016 at 09:37 UTC
    Thank you very much, I get your point regarding the two-dimensional array. On a related note, how would you store data to disk into consecutively named files? I would typically use a rootstring (something like $rootStr = 'dataFile' in Perl) and a counter $j, increment the counter in a loop, and print "$rootStr . $j" into a string "$fileNameString". Then I would want to name the file according to the contents of "$fileNameString". I understand, however, this is strongly discouraged in the Perl community. What approach would be better to make consecutively named files, using Perl?
      I understand, however, this is strongly discouraged in the Perl community.

      If I've understood your description correctly, there should be no discouragement at all to this. I assume you mean:

      my $fileroot = 'foo'; for my $j (0..999) { my $filename = $fileroot . $j; open (my $out, '>', $filename) or die "Cannot open file $filename fo +r writing: $!"; print $out "Hello world!\n"; close $out; }

      which seems a perfectly feasible way of achieving 1000 consecutively numbered files in one directory.

        Thank you again, hippo. Your code is exactly what I should have written in my reply, instead of merely describing my intentions. I used the word "discouraged" because I understood it is alway considered bad practice to put the name of a variable (i.e. the filename) into another variable.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1172899]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2019-09-15 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (180 votes). Check out past polls.

    Notices?