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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: formatted hash problem
by ww (Archbishop) on Feb 05, 2010 at 15:40 UTC

    There are a few things wrong with this, Spooky:

    1. Do use tags properly (see Markup in the Monastery and Writeup Formatting Tips; narrative goes in <p>...</p>
    2. At least, link to previous (possibly a use for hashes?); probably better to add to that thread or state your remaining problems coherently and comprehensively.
    3. SOPW should not address a specific Monk
    4. Do use tags properly (see Markup in the Monastery and Writeup Formatting Tips; narrative goes in <p>...</p>
    5. Show some effort (for example, if you're "not sure how Perl would handle 021: (sic) an exponential value" try delving into the docs).
    6. This is not code-a-matic!

    After 70+ posts, you're well past deserving any slack on these fundamentals. - -.

      WW- I relayed to Kenneth I printed off "Writeup Formatting Tips" in addition to "How (Not) To Ask A Question." I also printed "A Guide to PerlMonks Acronyms and Abbreviations" as I didn't know what "SoPW" meant. By the way (BTW?), is this an OK format to respond to you or other Monks if it's strictly just a response?
        1. No, use a private message (from the CB).
        2. No need to print; save a tree. Those links are readily available, especially if you put them in your Free Nodelet.

        ...and yes ( ;-) ), this violates the precepts in the first bullet... but maybe it will be seen and heeded someday by someone who happens upon it.

Re: formatted hash problem
by kennethk (Abbot) on Feb 05, 2010 at 15:26 UTC
    What have you tried? Why has it not worked? See How (Not) To Ask A Question. As this is connected to previous threads (I assume a use for hashes?), you should reference them so other monks can follow and assist. You also should not put an entire post in <code> tags - it makes exposition more difficult to read. See Writeup Formatting Tips.

    I do not entirely follow how your input maps to your output, even after reading the previous thread. Based upon my best guess, you should use a hash of arrays of arrays - see perllol for info on more advanced data structures. Note that while Perl would handle the scientific notation value with no difficulty, it doesn't matter since you are not doing math with it. As far as Perl is concerned, this is just a series of space-delimited strings. Does this do what you intend?

    #!/usr/bin/perl use strict; use warnings; my %relays; while (<DATA>) { chomp; my @result = split /\s/; my $id = shift @result; for my $i (0 .. $#result) { push @{$relays{$id}[$i]}, $result[$i]; } } for my $id (keys %relays) { for my $field_ref ($relays{$id}) { for my $values_ref (@$field_ref) { print "$id @$values_ref\n"; } } } __DATA__ relay01 238 933 set relay02 238 934 9876536 2345.56 relay01 238 934 reset relay02 239 935 876555 23456.88 relay01 239 999 initiate relay03 240 877 899998 87698 relay03 241 888 98989898 3.34e-10
      ... not sure how Perl would handle an exponential value...

      Further to kennethk's post: In almost as many characters as are quoted above, Spooky could have actually seen how Perl handles an exponential value. Sorry if the following rather pounds this into the ground:

      >perl -wMstrict -le "print 3.34e-10; print '3.34e-10'; print 1 + '3.34e-10'; printf qq{%15.15e \n}, 3.34e-10; print 'etc...'; " 3.34e-010 3.34e-10 1.000000000334 3.340000000000000e-010 etc...
      Kenneth, I printed off "How (Not) To Ask A Question" and "Writeup Formatting Tips" (quite a few pages!). I'll also try your code and get back to you. It may be next week as we're expecting quite a snow storm and I'll be leaving a little early. Thanks again!
        There are plenty of printed documents on my desk that I could care less about. Read "How (Not) To Ask A Question" and meditate upon what it says - this is a monastery of sorts after all. Remember that this is a free service where people much more experienced in Perl than you or I offer their (literally) valuable advice and insight out of the kindness of their hearts. Before you ask your neighbor for help with a home improvement project, would you not contemplate the best way to implement it and probably do some basic ground work like clearing space? It sounds like you are getting dangerously close to being considered a bad neighbor, which does not bode well for when down the line you encounter more Perl challenges.
      Kenneth, ..for some reason, for some relays the seconds field is blank. I'd like to delete that entry but not sure the best way to do this using your code above which works very well.
        Again, your question asking skills are leaving something to be desired. I don't know what the "seconds" field is. Copy the code above, put some data that does the wrong thing into the data block, and then tell us what the right thing would be.

        One possibility if a field is not getting populated is related to my split expression. My regular expression /\s/ splits on exactly one white space character. If your data has multiple spaces separating two values, you will get empty elements. /\s+/ will match one-or-more white space characters (perlre) and may fix this.