Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to declare variables per loop

by dreadpiratepeter (Priest)
on Oct 11, 2011 at 13:58 UTC ( #930821=note: print w/ replies, xml ) Need Help??


in reply to How to declare variables per loop

This has XY Problem written all over it. Try explaining to us in more broad terms what you are trying to accomplish that you think you need all these variables. I bet there is a far more elegant solution.



-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."


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:27 UTC
    Thank you very much!
    I compose commands for gnuplot in the following way (it is all a single line):
    plot $years $yscala "$abc_this_01" title "" with errorbars lt rgb "$co +lor_abc" lw 2, "$abc_this_01" title "{/Helvetica=10 ABC_Stuff}" with +lines lt rgb "$color_abc" lw 3,"$def_this_01" title "" with errorbars + lt rgb "$color_def" lw 2, "$def_this_01" title "{/Helvetica=10 DEF_S +tuff}" with lines lt rgb "$color_def" lw 3, "$ghi_this_01" title "" w +ith errorbars lt rgb "$color_ghi" lw 2, "$ghi_this_01" title "{/Helve +tica=10 GHI_Stuff}" with lines lt rgb "$color_ghi" lw 3, "$jkl_this_0 +1" title "" with errorbars lt rgb "$color_jkl" lw 2, "$jkl_this_01" t +itle "{/Helvetica=10 JKL_Stuff}" with lines lt rgb "$color_jkl" lw 5, + "$mno_this_01" with steps lt 1 lw 5 title "{/Helvetica=10 Abnormalit +y Border}"
    and the files looks like this:
    # Value Low High 2006 78.6 76.5 80.6 2007 86.3 84.7 87.6 2008 92.7 92.3 93.1 2009 95.3 94.9 95.6 2010 96.8 96.5 97.1 2011 97.0 96.5 97.5
    It plots diagramms with the values (plus y-errorbars) from four sources (abc, def ...) and with the abnormality border. There are ca. 20 topics ("this", "that" etc.) with up to 8 subgroups (01, 02 etc.).
    I have indeed the feeling that the composition is somehow ugly. Leastwise it works :-)
    However I would be very glad for any suggestion how I could organize the data better.
    Thanks again.
    VE
      Blowing some white space into your command shows that there is a pattern that we might take advantage of.
      plot $years $yscala "$abc_this_01" title "" with errorbars lt rgb "$color_abc" lw 2, "$abc_this_01" title "{/Helvetica=10 ABC_Stuff}" with lines lt rgb "$c +olor_abc" lw 3, "$def_this_01" title "" with errorbars lt rgb "$color_def" lw 2, "$def_this_01" title "{/Helvetica=10 DEF_Stuff}" with lines lt rgb "$c +olor_def" lw 3, "$ghi_this_01" title "" with errorbars lt rgb "$color_ghi" lw 2, "$ghi_this_01" title "{/Helvetica=10 GHI_Stuff}" with lines lt rgb "$c +olor_ghi" lw 3, "$jkl_this_01" title "" with errorbars lt rgb "$color_jkl" lw 2, "$jkl_this_01" title "{/Helvetica=10 JKL_Stuff}" with lines lt rgb "$c +olor_jkl" lw 5, "$mno_this_01" with steps lt 1 lw 5 title "{/Helvetica=10 Abnormality +Border}"
      We could consider putting the data into a hash and then feed that into a template. I've used sprintf here but there are many alternatives. I've made some guesses about the actual spec.
      #! /usr/perl/bin use warnings; use strict; my %args = ( years => 2011, yscala => q{xxx}, sources => [ { source => q{abc}, color => q{red}, }, { source => q{def}, color => q{blue}, }, { source => q{ghi}, color => q{yellow}, }, { source => q{jkl}, color => q{green}, }, ], topic => q{this}, number => q{01}, abnormality_border => q{mno}, ); my $cmd = build_command(%args); print $cmd; sub build_command { my %args = @_; my $cmd = sprintf( qq{plot %s %s\n\n}, $args{years}, $args{yscala} ); for my $source (@{$args{sources}}){ $cmd .= sprintf( qq{"C:/Stuff/%s_%s_%s.csv" title "" with errorbars lt rgb "%s_%s +" lw 2,\n}, $source->{source}, $args{topic}, $args{number}, $source->{color}, $source->{source}, ); $cmd .= sprintf( qq{"C:/Stuff/%s_%s_%s.csv" title "{/Helvetica=10 ABC_Stuff}" wit +h lines lt rgb "%s_%s" lw 3,\n\n}, $source->{source}, $args{topic}, $args{number}, $source->{color}, $source->{source}, ); } $cmd .= sprintf( qq{"C:/Stuff/%s_%s_%s.csv" with steps lt 1 lw 5 title "{/Helvetica +=10 Abnormality Border}"}, $args{abnormality_border}, $args{topic}, $args{number}, ); return $cmd; }
      output
      plot 2011 xxx "C:/Stuff/abc_this_01.csv" title "" with errorbars lt rgb "red_abc" lw + 2, "C:/Stuff/abc_this_01.csv" title "{/Helvetica=10 ABC_Stuff}" with line +s lt rgb "red_abc" lw 3, "C:/Stuff/def_this_01.csv" title "" with errorbars lt rgb "blue_def" l +w 2, "C:/Stuff/def_this_01.csv" title "{/Helvetica=10 ABC_Stuff}" with line +s lt rgb "blue_def" lw 3, "C:/Stuff/ghi_this_01.csv" title "" with errorbars lt rgb "yellow_ghi" + lw 2, "C:/Stuff/ghi_this_01.csv" title "{/Helvetica=10 ABC_Stuff}" with line +s lt rgb "yellow_ghi" lw 3, "C:/Stuff/jkl_this_01.csv" title "" with errorbars lt rgb "green_jkl" +lw 2, "C:/Stuff/jkl_this_01.csv" title "{/Helvetica=10 ABC_Stuff}" with line +s lt rgb "green_jkl" lw 3, "C:/Stuff/mno_this_01.csv" with steps lt 1 lw 5 title "{/Helvetica=10 +Abnormality Border}"
      The initial hash (the data) could be kept in a separate file and read in by the script. I often find keeping the data and code separate can ease updating/maintainance. Taking it a bit further keeping the actual templates in a separate file wouldn't hurt either.

      When you have it working nicely you would obviously take the white space out.

      updated: added output and included the csv file.

        Thank you wfsp, I put my answer at the end of the thread.
        VE
      You need Chart::Gnuplot. It should polish your interface. It is able to generate your chat on the fly. You can apply the loops here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-10-21 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (96 votes), past polls