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

Monks, I'm in need of a second pair of eyes. I have the following bit of code to try to sort on the cat_name field.

foreach my $category (sort { ($list->{$_}->{cat_name} cmp $list->{$_}- +>{cat_name}) } keys %$list) { say $list->{$category}{cat_name}; }

I'm getting the following errors over and over:

Use of uninitialized value $_ in hash element at ./facebook.pl line 34 +. Use of uninitialized value $_ in hash element at ./facebook.pl line 34 +. Use of uninitialized value in string comparison (cmp) at ./facebook.pl + line 34. Use of uninitialized value in string comparison (cmp) at ./facebook.pl + line 34.

These repeated errors are followed by a listing of the categories, but they are in random order and are not sorted alphabetically. I have no idea why I am getting unitialized value errors. Each element in the hash has a cat_name key with a value.

Here's a sample of the data I'm trying to sort:

$list = { '174972792548177' => { 'places' => [ { '264651060353078' => { 'n +ame' => 'WHIP City Towing', 'l +ink' => 'https://www.facebook.com/WHIP-City-Towing-264651060353078/' } }, { '177166582312074' => { 'l +ink' => 'https://www.facebook.com/Center-City-Service-177166582312074 +/', 'n +ame' => 'Center City Service' } }, { '965119126910980' => { 'l +ink' => 'https://www.facebook.com/Michaels-Towing-965119126910980/', 'n +ame' => 'Michael\'s Towing' } } ], 'cat_name' => 'Towing Service' }, '199850580046875' => { 'cat_name' => 'Garage Door Services', 'places' => [ { '233152426881482' => { 'l +ink' => 'https://www.facebook.com/413doors/', 'n +ame' => '413 Doors & Home Services' } }, { '432129326916445' => { 'l +ink' => 'https://www.facebook.com/Jfcsbose/', 'n +ame' => 'Jfcsbose' } }, { '471289059572856' => { 'l +ink' => 'https://www.facebook.com/Conte-Door-Service-Inc-471289059572 +856/', 'n +ame' => 'Conte Door Service, Inc.' } } ] }, '127806747288007' => { 'places' => [ { '152911074751051' => { 'l +ink' => 'https://www.facebook.com/pages/Pioneer-Valley-Rider-Training +/152911074751051', 'n +ame' => 'Pioneer Valley Rider Training' } } ], 'cat_name' => 'Traffic School' }, };

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Getting unexpected errors when trying to sort hash of hashes
by salva (Canon) on May 07, 2016 at 06:32 UTC
    Most of your problems would go away if you replace the unfriendly data structure inside the places slots by a simple hash:
    for my $entry (values %$list) { $entry->{places} = { map %$_, @{$entry->{places}} }; }
Re: Getting unexecpted errors when trying to sort hash of hashes
by nysus (Parson) on May 06, 2016 at 22:41 UTC

    DOH, hit me as soon as I hit submit. Replaced the two $_ with $a and $b.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      That's called Rubber duck debugging, and it has been pointed out to me a few times by some of the greats on this site :)

      Just saying something out loud is often enough what one needs to fix a problem. PerlMonks is great for this.