I'm an intermittent perl user and its been a while since I did anything with perl. I've put a high level overview of the problem i'm trying to solve at the end of this in case the detailed bit doesnt make sense.

-------------------------------------

I'm using selectall_arrayref from DBI to get some data from a database.

I want to use the data returned in the query, in sequence, to populate or adjust the values in a hash. I was thinking I could iterate over the array and do what I need to do to the values in the hash.

I can access the data in the arrayref returned by the query by using @{$months->[$x]}, but when I try to use @{$months->[$x]} as the key for the hash, I get nothing - $monthhash{ @{$months->[$x]} } which I dont understand.

code below ---------------------
my $months = $dbh->selectall_arrayref("select to_char(last_day(add_mon +ths('14-May-2004', (rownum-1))),'MON-RRRR') dt from all_objects where rownum <= months_between(sysdate,'31-May-2004')+1"); my $month; my %monthhash; my $x = 0; foreach $month (@$months) { $monthhash{@$month[0]} = 0; print "check $monthhash{@$month[0]} @$month[0] @{$months->[$x]} ** + $monthhash{ (@{$months->[$x]}) }\n"; $x++; }

sample output of print

check 0 MAY-2004 MAY-2004 **

High level description.

I have a sequence of months (think of them as the columns on a spreadsheet) and I will have numbers for some of them but not others. I need to make sure that any gaps are filled with the most recent number; for example. I have values for May, Aug & Sep and I need to fill in the blanks. So my thinking is to populate the data I know about and then cycle through the months from left to right and where I find a blank I take the preceding value and replace the blank then move on.

Original
May 04 Jun 04 Jul 04 Aug 04 Sep 04 Oct 04 50 100 110 May 04 Jun 04 Jul 04 Aug 04 Sep 04 Oct 04 50 50 50 100 110 110

There's bound to be several ways of doing this, so please feel free to suggest things, bear in mind that this is the simple version, I will have to be doing this for data where there will be a fair bit of manipulation going on before the numbers are populated.

Thanks in advance

Edit: g0n - extra code tags


In reply to Using array value (from array ref) as hash key?? by Haddock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.