Haddock has asked for the wisdom of the Perl Monks concerning the following question:
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.
OriginalMay 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 advanceEdit: g0n - extra code tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using array value (from array ref) as hash key??
by ikegami (Patriarch) on May 25, 2007 at 05:20 UTC | |
by Haddock (Initiate) on May 25, 2007 at 05:54 UTC | |
by ikegami (Patriarch) on May 25, 2007 at 06:08 UTC | |
by Haddock (Initiate) on May 28, 2007 at 00:36 UTC | |
|
Re: Using array value (from array ref) as hash key??
by ikegami (Patriarch) on May 25, 2007 at 05:01 UTC | |
|
Re: Using array value (from array ref) as hash key??
by blazar (Canon) on May 25, 2007 at 11:24 UTC |