Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Declaring and checking content of variables with consecutive names

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


in reply to Declaring and checking content of variables with consecutive names

Since it is hopefully clear by now from the excellent answers which you have already received that going down this route would be A Bad Thing, I thought it might be worth writing a perlcritic policy to check for this. However, having done a little research today I am delighted to discover that such a policy already exists: Perl::Critic::Policy::Bangs::ProhibitNumberedNames.

This sounds like a really good addition in and of itself. The Perl::Critic::Bangs bundle also has some other tempting-looking policies as well. Just thought this was worth mentioning as it was new to me and I would have used it long ago if I had known about it.

  • Comment on Re: Declaring and checking content of variables with consecutive names

Replies are listed 'Best First'.
Re^2: Declaring and checking content of variables with consecutive names
by rflesch (Novice) on Sep 29, 2016 at 07:29 UTC
    If you read data fields from an instrument into arrays periodically (for example, data obtained with a digital oscilloscope every second) wouldn't it make sense to give the arrays consecutive names such as @arr0000, @arr0001, (..), @arr0234, (...)? Similarly, if you save these data to disc, the name of the array would become the corresponding file name. We do this all the time (but we don't use Perl in this context).

      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.

        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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-09-15 13:51 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?