ghenry has asked for the wisdom of the Perl Monks concerning the following question:

Dear Master Monks,

In GD::Graph::linespoints - Various questions, you can see my basic script. The problem now, is that I want to add 4 weeks worth of graphs and keep this going for the rest of the year.

If I wanted to keep the rest of the years data in the script (to begin with, I'll move it out later) are references the best way to do this? A HoHs?

e.g (I have forgotten to add Sat and Sun:)

my $times = { "May" => { "wk1" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk2" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk3" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk4" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, }, "Jun" => { "wk1" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk2" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk3" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk4" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, }, "Jul" => { "wk1" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk2" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk3" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, "wk4" => { Mon => "24.50", Tues => "23.40", Wed => "22.40", Thu => "25.01", Fri => "24.10", }, }, };

Thanks.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: GD::Graph::linespoints - References
by polettix (Vicar) on May 16, 2005 at 16:07 UTC
    The structure is quite static, you could also use the __DATA__ part of your script. A possible approach could be:
    #!/usr/bin/perl # Load data inside %times hash my %times; while (<DATA>) { next unless /\S/; # just in case... my ($month, $weeknum, $day, $val) = split /;/; $times{$month}{"wk$weeknum"}{$day} = $val; } # ... all your program here ... __DATA__ May;1;Mon;24.50 May;1;Tues;23.40 May;1;Wed;22.40 May;1;Thu;25.01 May;1;Fri;24.10 ... and so on...
    However, you should decide which is the best data structure suitable for what you intend to do with your data, then think about how storing it somewhere. You can also look for Storable or XML::Simple for ways to do the latter.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.

      Cool, I like the XML idea. I might try that.

      Remind me what the __DATA__ tag is called again, so I can go read up on it, I haven't used that before.

      .oO(Is it an inbuilt filehandle kind of thing?)

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
        Correct, it's an inbuilt filehandle. You can see something about it in perldoc SelfLoader. The same should apply using __END__ instead of __DATA__: the filehandle is still DATA, but I don't know if it is portable with ancient versions of Perl or if it has other issues.

        TheDamian also produced Inline::Files as an extension of the concept.

        Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

        Don't fool yourself.