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

Here is my code. How do I loop through @Data which would be the row returned from my DBI query and assign the hashkey and values to my anonymous hash record.

my $DataCount = 0; while ( @Data = $ExcelQuery->fetchrow_array ) { # Loop through row data and assign to an anonymous hash in an arr +ay $AssetRecord = {$ExcelColumns[0] => $Data[0], $ExcelColumns[1] => $Data[1], $ExcelColumns[2] => $Data[2], $ExcelColumns[3] => $Data[3], $ExcelColumns[4] => $Data[4], $ExcelColumns[5] => $Data[5], $ExcelColumns[6] => $Data[6], $ExcelColumns[7] => $Data[7], $ExcelColumns[8] => $Data[8], $ExcelColumns[9] => $Data[9], $ExcelColumns[10] => $Data[10], $ExcelColumns[11] => $Data[11], $ExcelColumns[12] => $Data[12], $ExcelColumns[13] => $Data[13], $ExcelColumns[14] => $Data[14], $ExcelColumns[15] => $Data[15], $ExcelColumns[16] => $Data[16], $ExcelColumns[17] => $Data[17], $ExcelColumns[18] => $Data[18], $ExcelColumns[19] => $Data[19], $ExcelColumns[20] => $Data[20], $ExcelColumns[21] => $Data[21], }; $AssetEntries[$DataCount] = $AssetRecord; $DataCount ++; }
I had something akin to this
my $RowCount = 0; while (<@Data>){ $AssetRecord = $ExcelColumns[$RowCount] => $Data[$RowCount]; $RowCount ++; }

This of course creates a new anonymous hash so when the row is finished only the last hash reference and value gets stored in the array.

I am missing something simple.

I probably should just create an object. But before going OO want to understand my references better.



"No matter where you go, there you are." BB

Replies are listed 'Best First'.
Re: Record creation in nested loops
by deibyz (Hermit) on Mar 10, 2005 at 11:22 UTC
    I'm not sure if I'm understanding correcly the question, but isn't something like this what you're searching for:

    $AssetRecord = { map { $ExcelColumns[$_] => $Data[$_] } 0..$#Data } ;

    map will create the full list of pairs and then you create an anonymous hash with outer {}.

      Spot on thank you very much.


      "No matter where you go, there you are." BB
Re: Record creation in nested loops
by japhy (Canon) on Mar 10, 2005 at 12:50 UTC
    I'd do:
    @$AssetRecord{@ExcelColumns} = @Data;
    It's called a hash slice.

    And you seem to think that while (<@array>) { ... } is sensible code, when it really is quite awful. Don't use that type of idiom. If you want to loop over an array, use for.

    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      I don't particularly think that while (<@array>){....} is sensible code it was just a fragment of what I have tried. I use foreach more often than not. But out of curiousity why do you refer to the while(<@array>) {...} as awful code?

      "No matter where you go, there you are." BB
        It uses <...> in its file-globbing context. This means any elements in @array that contain wildcard characters for your shell will be expanded into all matching filenames. It also means that if any of your elements have whitespace in them, they'll be split on that whitespace.
        @array = ("this", "is not", "cool"); print "[$_]" while <@array>; # [this][is][not][cool]
        It's misuse of an operator, and I'm irked that Perl lets it fly without even alerting you to the potential gaffe you're making.
        _____________________________________________________
        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart