#!/usr/bin/perl # Program name: next-value-of-hash.pl Run it, thusly: perl next-value-of-hash.pl Look at the next value of hash when sorting by values # From perlmonks Chatterbox 20170213 reply by [ambrus] # [stu96art] I am hoping to get some help with hashes. Working on looking at the next value when sorting by values, but don't know how to reference it. # [stu96art] My code is on my scratchpad: /padstu96art # [ambrus] stu96art: perhaps try to first assign the sorted key list to an array variable, then iterate through that array with indexes # [stu96art] Gotcha. That makes sense. Appreciate the help. # [ambrus] like my@s = sort keys %a; for my$i (keys@s) { print "current: $s[$i] => $a{$s[$i]}, next: ", ($i+1<@s ? "$s[$i+1] => $a{$s[$i+1]}" : "none"); } # [stu96art] Thanks again. I will work on that. use strict; use warnings; # Example hash from https://perlmaven.com/perl-hashes my %color_of = ( "apple", "red", "orange", "orange", "grape", "purple", "tomato", "red", "lettuce", "green", "peanut", "tan", ); my @s = sort keys %color_of; for my $i ( keys @s ) { print "current: $s[$i] => $color_of{$s[$i]}, next: ", ( $i + 1 < @s ? "$s[$i+1] => $color_of{$s[$i+1]}" : "none" ); print "\n"; } __END__