Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^3: How to declare variables per loop

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

in reply to Re^2: How to declare variables per loop
in thread How to declare variables per loop

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930848]
marioroy At a large firm, had to awaken a security guy a few times so that nobody would catch him sleeping.
marioroy On a large dairy farm, almost went off the road with a 10 wheeler from exhaustion. On the farm, almost went off the silo with a big tractor. At the Fransiscan monastery, almost slipped off the dome while painting it.
[marioroy]: In Saudi Arabia caught fire but didn't burn. I think angels exist.
Lady_Aleena clean up after dinner finally.
[Discipulus]: uch! so we are twice lucky to have MCE..;=)
[marioroy]: Working a midnight graveyard shift, on the way home ran out of gas. just 2 minutes later somebody stops with a small tank of gas. It's wierd. There's always someone nearby for some reason.
[Lady_Aleena]: I was the one who stop[ed in the middle of a cold night to pick up a small family walking to a gas station. I got a few gallons of free gas for the round trip, and the grandmother said she'd pray for me.
[marioroy]: Folks do not know it, but MCE was ridiculously difficult. At Niagra Falls, heard a voice on enabling Grace while in the mist boat. That 10x performance is possible.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-05-29 08:24 GMT
Find Nodes?
    Voting Booth?