Jdavis wrote:
> hello,
>  I have a hash. To use this hash with a module I need it in this form...
>
> @data = (
>     ["12am","1am","2am","3am","4am","5am","6am", "7am", "8am"],  ### key
>     [   251,   102,  55,   36,   113,  200,    32,    99,   4],  ###val
> );
>
> I confused.. How could I get a hash to the above structure

Hi 'J'. ( It'd be nice to know a name! )

I think the following does what you want. I've written some code to buil
a %data hash as I think you have it from the contents of your post. The final
'while' loop is the bit you're interested in: it takes the hash and builds the
@data array from it as you've described. I've dumped the hash and the array
so that you can see what's in them.

Cheers,

Rob

    use strict;
    use warnings;

    use Data::Dumper;

    #   Build the array from the supplied data
    #
    my @data;
    while ( <DATA> ) {
        next unless /\[/;
        s/#.+$//;
        push @data, [/\w+/g];
    }
    die unless @data == 2;
    die unless @{$data[0]} == @{$data[1]};

    #   Build the 'source' hash as I imagine it to be
    #
    my %data;
    @[EMAIL PROTECTED] = @{$data[1]};

    #   Empty the @data array and rebuild it from the hash
    #
    @data = ();
    while (my ($k, $v) = each %data) {
        push @{$data[0]}, $k;
        push @{$data[1]}, $v;
    }

    #   Show what we've done
    #
    print Data::Dumper->Dump([\%data, [EMAIL PROTECTED], [qw(*data *data)]);

    __END__

    @data = (
        ["12am","1am","2am","3am","4am","5am","6am", "7am", "8am"],  ### key
        [   251,   102,  55,   36,   113,  200,    32,    99,   4],  ###val
    );

output

    %data = (
              '3am' => '36',
              '12am' => '251',
              '4am' => '113',
              '5am' => '200',
              '1am' => '102',
              '6am' => '32',
              '7am' => '99',
              '2am' => '55',
              '8am' => '4'
            );
    @data = (
              [
                '3am',
                '12am',
                '4am',
                '5am',
                '1am',
                '6am',
                '7am',
                '2am',
                '8am'
              ],
              [
                '36',
                '251',
                '113',
                '200',
                '102',
                '32',
                '99',
                '55',
                '4'
              ]
            );




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to