I am currently working with a hash array that I have sorted by its keys and values. I have been successful in splitting the hash up such that I can grab the key and value separately. I have created a loop function that iterates through the array of keys and values. Additionally, I have a most common number variable that keeps track of the hash value and ideally updates whenever it iterates through the loop and finds a value greater than the current value in the most common number variable. This is what my code looks like:
my $MCNum = 0;
my @sorted_pairs = %counts{$word}.sort: *.kv;
loop (my $i = 0; $i < @sorted_pairs; $i++) {
say "i: ", @sorted_pairs[$i].values;
say "i+1 ",@sorted_pairs[$i+1].values;
if @sorted_pairs[$i].values < @sorted_pairs[$i+1].values {
$MCNum = @sorted_pairs[$i+1].values;
$best_word = @sorted_pairs[$i+1].keys;
say "MCNumber is: ", $MCNum;
}
Which gives this output when I run the program:
Sorted Hash Array: [90's => 1 at => 1 dance => 1 did => 1 does => 1 doesn't => 1 don't => 1 droid => 1 dubwise => 1 feat => 1 hasn't => 1 if => 1 is => 5 letters => 1 life => 1 like => 1 man => 1 me => 5 monsterman => 1 my => 2 scenes => 1 sensation => 1 so => 3 song => 1 survives => 1 theme => 1 triangle => 1 weather => 1 would => 1 y'all => 1 you => 10 your => 1]
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (5)
i: (5)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (5)
i: (5)
i+1 (1)
i: (1)
i+1 (2)
i: (2)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (3)
i: (3)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (1)
i: (1)
i+1 (10)
i: (10)
i+1 (1)
i: (1)
i+1 ()
This leads to my confusion as I assume that the value should be skipped when it encounters 1's, but when it sees 2, 5, and/or 10, the most common number variable value should be updated. Instead, I've noticed that when running the program, it either never updates as shown in the code, or it consistently updates the variables regardless of if the number is smaller or larger. What am I missing?