$text = "If music be the food of love play on."; @words = (split( /\s+/, $text)); @words_sort = sort ({length($a) <=> length($b)} @words); print("Longest word: $words_sort[-1]\n"); print("\n" . "-" x 35, "\n");
A better solution can be found here > challenge 4@array1 = (1, 2, 3, 4, 5); @array2 = (3, 4, 5, 6, 7); @intersection = grep { $elem = $_; grep { $elem == $_ } @array2 } @array1; print "Intersection: @intersection\n";
print( scalar( grep { !($_ % 2) } qw( 1 2 3 4 5 ) ) . "\n" );
sub count_even_words { scalar( grep { !(length($_) % 2) } @{$_[0]} ); } print("Number of even character items: " . count_even_words([qw(ab abc abcd abcde abcdef abcdefg abcdefgh)]) . "\n");
sub count_even_digits { scalar( grep { !(split('',$_) % 2) } @{$_[0]} ); } print("Number of even digits: " . count_even_digits([qw(12, 123, 1234, 12345, 123456, 1234567, 12345678)]) . "\n");
%mc_hash = ( 100 => 10, 50 => 40, 10 => 60, 200 => 70 ); @mc_sel = grep { $_ > 50 or $mc_hash{$_} > 50 } keys %mc_hash; print("@mc_sel\n");
$sides_dice = 6; @array = qw(5 1 2 1 3 3 1 4 3 1 2 1 3 4 4 5 6 2 3 1); @freq = (); for ($i = 1; $i <= $sides_dice; $i++) { push( @freq, scalar( grep { $_ == $i } @array) ); print($i . " | " . ("*" x $freq[$i - 1]) . "\n"); }
@list = (); foreach ( qw( 1 2 3 5 8 13 21 34) ) { push ( @list, $_ ) if ($_ < 10); }