Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

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


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

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.

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

Replies are listed 'Best First'.
Re^6: Declaring and checking content of variables with consecutive names
by rflesch (Novice) on Sep 29, 2016 at 10:12 UTC
    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.
      > it is always considered bad practice to put the name of a variable (i.e. the filename) into another variable

      Yes, it is, but in this case, we're putting the value of a variable into another variable, which is OK and practical.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

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












    Results (276 votes). Check out past polls.

    Notices?