Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to declare variables per loop

by tospo (Hermit)
on Oct 11, 2011 at 14:16 UTC ( #930825=note: print w/ replies, xml ) Need Help??


in reply to How to declare variables per loop

Yes, in general, if you want to use lots of variables like that, you actually want a hash or an array. However, in this specific case - as you have presented it - there is no need at all for a variable because the name of the file can be derived from knowing the name of the variable following this simple pattern according to your example:
C:/Stuff/NAME_OF_VARIABLE.csv


Comment on Re: How to declare variables per loop
Re^2: How to declare variables per loop
by Anonymous Monk on Oct 11, 2011 at 14:44 UTC
    Thank you tospo,
    this is correct of course, but I thought it would make the code clearer since I do not have to write down all the file names then. Besides I thought I could code the other parameter this way too (e.g. color) - see my answer on dreadpiratepeter a few minutes earlier.
    VE
      except that you do write down all the file names when you assign them to variables. Much better to construct the names of the files on the fly. For example you could do something like
      my $storage_dir = "C:/Stuff/"; foreach my $category qw( abc def ghi ) { foreach my $subcategory qw( this that ){ foreach my $year ( 1..3) { my $file = sprintf("%s_%s_02d.csv", $category, $subcategory,$yea +r); my $path = $storage_dir.$file; # now do something with the file, i.e. generate your plot comman +d do_plot( $path ); } } } sub do_plot { my $path - shift; ### do the actual plotting here }
      Now you don't have to type all the combinations of categories, years etc but let the script handle that. An alternative approach would be to change the way you store the data by putting it all into a single file and starting each block of data with a string that gives yuo all those categories, as in:
      #abc_this_01 -- data goes here --- #def_this_01 -- more data --- etc
      Now your script doesn't need to kow anything about the categories and takes whatever is in your file, which presumably is the output of another script.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-07-30 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (239 votes), past polls