Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to access a hash from another file

by tospo (Hermit)
on Oct 14, 2011 at 13:45 UTC ( [id://931533]=note: print w/replies, xml ) Need Help??


in reply to How to access a hash from another file

OK, you got some solutions for getting this to work but please note that your approach just isn't a good idea in the first place (sorry).
YOu currently populate the hash by actually typing stuff into a file that you then save as a perl module. However, it can very easily be generated by the code using loops as I mentioned earlier in the other thread you are refering to. It sort of defeats the purpose of scripting if you then end up typing all the combinations of categories and sub-categories by hand into a file (i.e. hardcoding them).
Do yourself a favour and have a go at using loops to let the code generate all these tedious combinations of things and you will begin to see "The Light"(Tm) :-)
Good luck!
  • Comment on Re: How to access a hash from another file

Replies are listed 'Best First'.
Re^2: How to access a hash from another file
by wfsp (Abbot) on Oct 14, 2011 at 15:02 UTC
    Yes and no. If the logic gets torturous I find it better just to create a table even if it is almost wholly repetitive. If it is large (or small), if most entries are the same or even empty. Perl doesn’t mind tedious.

    As soon as there are nots, ors and ands and they start nesting and cascading all over the place I’m certain, nay, guaranteed, to get it wrong. I find it hard to write, the bugs hard to find and they are most often the sort of bugs that appear six months down the line. Did I mention my boolean algebra is weak?

    If it is appropriate and I can fit the logic into a table I get on much better, it is all reduced to a lookup. If it is not appropriate then yours is the way to go. What is appropriate, of course, is a matter of taste. :-)

      well, Perl doesn't mind tedious but you might and you are likely to get nice little errors in the long table that will be difficult to find. Why risk it? It's like wanting to print out a multiplication table by hardcoding all the results instead of calculating them. Yes, you can do it and Perl doesn't mind but it would defeat the purpose, wouldn't it?
Re^2: How to access a hash from another file
by vagabonding electron (Curate) on Oct 14, 2011 at 15:12 UTC
    Thank you tospo!
    You mean the parameter could by saved in csv-file which could look like:
    shorthand;title;indicator;name;yskala;legend THIS;This Evaluation;01;First;[:100];bottom THIS;This Evaluation;02;Second;[:100];bottom THIS;This Evaluation;15;Fifteen;[0:];top
    and be read into the hash?
    Of course it would simplify the build-up. I will try to accomplish this, though I have read some threads on this subject, as converting a text into a hash datastructure or Building an arbitrary-depth, multi-level hash and did not see "The Light" yet :-)
    I'll try.
    Thanks!
    VE

    Definitely OT but I could not stop myself: I made a funny typo today: "sue strict; sue warnings;". Well, Perl did not do it :-)
    VE

      not really. The crucial point is that the entire content of that csv file can be auto-generated in a few linew unless I'm missing something important here.
      Take this as a simple example:
      my @combinations = ( "1,1","1,2","2,1","2,2")
      vs.
      my @combinations; for my $i (1..2){ for my $j (1..2) { push @combinations, "$i,$j"; } }
      I'm generating an array in this case with strings that are combinations of elements, in this case simply the number 1 and 2 but you can easily substitute that with "THIS", "THAT" and "This Evaluation", "That Evalutation" etc, which is what you have (if I remember your other thread correctly).
      In the first example, I harcode the array by typing all the combinations in my script. I could also save them to a file in csv format, as dump of the array (using Data::Dumper), or whatever format you can think of - the problem remains: I'm hardcoding all possible but entirely predictabe and therefore scriptable combinations of elements.
      In the second example, I generate all possible combinations on the fly. Again, if I remember correctly, you only need to do that to generate a file name. For that reason you don't even need to save the data to an array or a hash - you just generate the string as a combination of all the elements and then use that to open your file - job done.
      Of course, in my simple example, the second version is longer than the first, but you can easily see how that changes quickly with more elements and more combinations.
      So, if the data can be generated on the fly, do it - that's the whole point of scripting. If not, i.e. sometimes you want the scale for a "THIS" plot to be "0:" and sometimes ":100" and tehre is no way to predict it, then you have to resort to a config file (for which you can use a number of options, such as YAML or simple config format - have a look around on the CPAN).
      Hope this helps.
        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
      I used to know a girl called sue strict. shudders

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found