http://qs321.pair.com?node_id=932109


in reply to Re^3: How to access a hash from another file
in thread How to access a hash from another file

Thank you tospo.
The problem is that not each number can be combined with each evaluation name. Therefore I do need a config file. In fact I happened to populate the hash %module reading the csv-file in the following way:
open(my $fh, "<", "parameter.csv") or die "$!"; my $header = <$fh>; while(<$fh>) { my @line = split(/;/, $_); my $t = $line[0]; ${module}{$t}{name} = $line[1]; my $n = $line[2]; ${module}{$t}{indicator}{$n}{ind} = $line[3]; ${module}{$t}{indicator}{$n}{yscala} = $line[4]; } close $fh;
The file names in the gnuplot command are generated afterwards on the fly whereby only the names are created which are allowed by config file.
After that I used the code from wfsp from Re^2: How to declare variables per loop in the following way:
my @cmds; for my $number (sort keys %{${module}{$topic}{indicator}}){ my $cmd = build_command($number, %args); push @cmds, $cmd; } for (my $i=0; $i<@cmds; $i++) { if($i == 0 or $i % 2 == 0) { $cmds[$i] = "$multiplot\n\n$cmds[$i]"; } } for (my $i=0; $i<@cmds; $i++) { if($i % 2 == 1 or $i == $#cmds) { $cmds[$i] = "$cmds[$i]\n\n unset multiplot\n\n"; } } my $slurpline; for (my $i=0; $i<@cmds; $i++) { $slurpline.=$cmds[$i]; }

The gnuplot command itself is very short now:
open my $P, "|-", "gnuplot" or die; printflush $P qq[ $postscript $slurpline ]; close $P;

Thanks again!
VE

Replies are listed 'Best First'.
Re^5: How to access a hash from another file
by tospo (Hermit) on Oct 18, 2011 at 12:53 UTC
    Whatever works best for you! Just a short comment: you can easily combine the 4 for loops in your code example into a single one without pushing the commands into an array of commands, just increment $i inside the loop then do your tests for i%2 using one if-else control structure.
    The whole set-up still looks awkward to me but that doesn't matter and you should go with it if it works. After some more experience you might come back to this and then immediately see a better way of doing it. If I now look at some of my early Perl scripts it causes me physical pain :-). It's just the way it is. Good luck with your further adventures in Perl!