in reply to Perl Hashes, keys under keys (I think)?

Try changing your assignment from this:

$records{"$owner_record[1]"} = [ "$owner_record[5]", "$owner_record[0] +", ... ];

To this:

$records{"$owner_record[1]"} = { USER => "$owner_record[5]", FILENAME +=> "$owner_record[0]", ... };

Replies are listed 'Best First'.
Re^2: Perl Hashes, keys under keys (I think)?
by mmartin (Monk) on Sep 19, 2011 at 15:48 UTC

    hbm,

    Hey thanks for the reply. I did try exactly that but the way the output looked didn't seem right. It looked liked it added them as elements instead of keys to those elements. But I could have just misunderstood.

    Ok so if I do that this is my output:
    $records{"$owner_record[1]"} = [ USER => "$owner_record[5]", FILENAME => "$owner_record[0]", PID => "$owner_record[6]", TIME => "$owner_record[9]", DATE => (join ":", "$owner_record[10] $owner_record[11]"), + ELAPSED => [] ]; ___OUTPUT___ $VAR1 = { '999?SheetMusic' => [ 'USER', 'bill', 'FILENAME', '/ud/NEW-PrOdUcTs/NWA', 'PID', '5151515', 'TIME', '10:51:00', 'DATE', 'Sep 16', 'ELAPSED', [] ], '667!NEWPRODUCT' => [ 'USER', 'matthew', 'FILENAME', '/ud/QC-PROD/NWO', 'PID', '5555555', 'TIME', '10:25:00', 'DATE', 'Sep 16', 'ELAPSED', [] ], '001!SCHEDULE' => [ 'USER', 'richard', 'FILENAME', '/ud/QC-DATA/CONTROL', 'PID', '5005444', 'TIME', '10:50:44', 'DATE', 'Sep 16', 'ELAPSED', [] ] };


    Thanks,
    Matt


    .

      Remember that [] is used to reference to an array element and a {} is for a hash. So you need to change your code to:

      # v $records{"$owner_record[1]"} = { USER => "$owner_record[5]", FILENAME => "$owner_record[0]", PID => "$owner_record[6]", TIME => "$owner_record[9]", DATE => (join ":", "$owner_record[10] $owner_record[11]"), + ELAPSED => [] }; # ^
Re^2: Perl Hashes, keys under keys (I think)?
by mmartin (Monk) on Sep 19, 2011 at 15:52 UTC

    Sorry I just noticed they were curly-brackets and not square ones. Cool, this is what I get now:

    $records{"$owner_record[1]"} = { USER => "$owner_record[5]", FILENAME => "$owner_record[0]", PID => "$owner_record[6]", TIME => "$owner_record[9]", DATE => (join ":", "$owner_record[10] $owner_record[11]"), + ELAPSED => [] }; ___OUTPUT___ $VAR1 = { '999?SheetMusic' => { 'PID' => '5151515', 'TIME' => '10:51:00', 'DATE' => 'Sep 16', 'ELAPSED' => [], 'FILENAME' => '/ud/NEW-PrOdUcTs/NWA', 'USER' => 'bill' }, '667!NEWPRODUCT' => { 'PID' => '5555555', 'TIME' => '10:25:00', 'DATE' => 'Sep 16', 'ELAPSED' => [], 'FILENAME' => '/ud/QC-PROD/NWO', 'USER' => 'matthew' }, '001!SCHEDULE' => { 'PID' => '5005444', 'TIME' => '10:50:44', 'DATE' => 'Sep 16', 'ELAPSED' => [], 'FILENAME' => '/ud/QC-DATA/CONTROL', 'USER' => 'richard' } };

    Awesome, thanks for your help!

    What is the difference between using curly-brackets there and square-brackets?


    Thanks,
    Matt

      What is the difference between using curly-brackets there and square-brackets?

      In this context, curly brackets are the anonymous hash 'composer' (to use the terminology of perlref), square brackets are the anonymous array composer. See Making References items 3 and 2, respectively, in perlref.

      Update: Maybe one should say "the anonymous { hash => 'composer' } and the anonymous [ qw(array composer) ]".


        AnomalousMonk,

        Hey thanks for the reply. You know what, now that you say that I do remember seeing that. Thanks!


        Thanks Again,
        Matt
      you got it!