print("\n"); print("-" x 35, "\n"); print("Challenge 1. Determine max or min values of a list with the numbers 0 7 5 3 22 23 11 34 51 32 5 3 1\n"); print("-" x 35, "\n"); use List::Util qw(max min); @numbers_list = qw( 0 7 5 3 22 23 11 34 51 32 5 3 1 ); print("Highest number: " . max(@numbers_list) . "\n"); print("Lowest number: " . min(@numbers_list) . "\n"); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 2. Determine the character with the lowest or highest ASCII value of a list with the characters z, c, m "); print("-" x 35, "\n"); use List::Util qw(minstr maxstr); print("Character with lowest ASCII value: " . minstr( 'z', 'c', 'm' ) . "\n"); print("Character with highest ASCII value: " . maxstr( 'z', 'c', 'm' ) . "\n"); print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 3. How do two strings differ?"); print("-" x 35, "\n"); use String::Diff; my($str_1, $str_2) = String::Diff::diff('abcefgijk', 'abcdefghijk'); print "$str_1\n"; # abcefgijk print "$str_2\n";# abc{d}efg{h}ijk print("-" x 35, "\n");
print("\n"); print("-" x 35, "\n"); print("Challenge 4. What do the two arrays ('a b c d' and 'c d e f') have in common and how do these arrays differ?"); print("-" x 35, "\n"); use Array::Utils qw(:all); @a = qw( a b c d ); @b = qw( c d e f ); @intersection = intersect(@a, @b); print("items that \@a and \@b share: @intersection\n"); @minus = array_minus( @a, @b ); print("items of \@a that are not in \@b: @minus\n"); @minus = array_minus( @b, @a ); print("items of \@b that are not in \@a: @minus\n"); print("-" x 35, "\n");
use HTML::Entities; $str_french = "Voilà un gâteau étonnant à la crème brûlée, dégusté à côté d'une île mystérieuse près du cœur de la forêt."; print (encode_entities($str_french), "\n"); $str_hyperlink = 'arcolinux.com'; print (encode_entities($str_hyperlink), "\n");
Second method use Math::Round:use POSIX; $float = 3.5; $ceil = ceil($float); print("result 'use POSIX': $ceil\n"); # 4 $floor = floor($float); print("result 'use POSIX': $floor\n");# 3 $float = -3.5; $ceil = ceil($float); print("result 'use POSIX': $ceil\n");# -3 $floor = floor($float); print("result 'use POSIX': $floor\n");# -4
use Math::Round qw( :all ); $float = 3.5; $rounded = round($float); print("result 'use Math': $rounded\n"); # 4 $float = -3.5; $rounded = round($float); print("result 'use Math': $rounded\n");# -4
# without module, with regex $str = " Hello World! "; $str =~ s/\s+//g; print("Result: |" . $str . "|\n");# with module 'String::Util' qw(:all) use String::Util qw(:all); $str = " Hello World! "; print("Result: |" . nospace($str) . "|\n");# in addition, the module has more interesting features as 'collapse', 'contains', 'startswith', 'endswith', 'sanitize'...
use Path::Tiny; $number_words = 0; $total_length_words = 0; $total_space = 0; $file = path("./lorem_ipsum.txt"); if ( $file->exists ) { @lines = $file->lines; # or @lines = $file->lines_utf8; chomp(@lines); foreach $line (@lines) { $line =~ s/\h+/ /g;# not necessary... $count_space = () = $line =~ / /g; $total_space += $count_space;# ... @single_line = split(/ /, $line); $number_words += scalar(@single_line); foreach $word (@single_line) { $total_length_words += length($word); } } print("Number of words: $number_words\n"); print("Number of characters (without space): $total_length_words\n"); print("Number of spaces: " . $total_space . "\n");# print("Number of spaces: " . ($number_words - 1) . "\n"); print("Average word length: " . $total_length_words / $number_words . "\n"); } else { print("$file does not exist.\n"); }
use Path::Tiny; $file = path("./lorem_ipsum.txt"); if ( $file->exists ) { $file->copy("./lorem_ipsum.copy.txt"); $all_text = $file->slurp; # or $all_text = $file->slurp_utf8; $all_text =~ s/[a-d]/x/g; $file->spew($all_text); } else { print("$file does not exist.\n"); }
use Text::ASCII::Convert; $non_ASCII_text = "Árvíztűrő tükörfúrógép, gyönyörű élményekkel teli hétvégét kívánok! 😊🌈🎉"; $text_converted = convert_to_ascii($non_ASCII_text); print("$text_converted\n"); # prints "Arvizturo tukorfurogep, gyonyoru elmenyekkel teli hetveget kivanok!"
use Format::Util::Numbers qw( commas to_monetary_number_format roundnear); print("12.6789012 -> ". roundnear( 0.01, 12.6789012) . "\n"); # => 12.68 print("-12.6789012 -> ". roundnear( 0.01, -12.6789012) . "\n");# => -12.68 print("12.6789012 -> ". roundnear( 0.1, 12.6789012) . "\n");# => 12.7 print("-12.6789012 -> ". roundnear( 0.1, -12.6789012) . "\n");# => -12.7 print("12345.679 -> ". commas(12345.679, 1) . "\n");# => 12,345.7 print("123456789 -> ". to_monetary_number_format(123456789) . "\n");# => 123,456,789.00
# content scores.csv with one blank line # Sebastian;4.5;6.0;7.2 # Daniel;5.5;6.5;7.0 # # Florence;7.1;7.9;7.8 use Path::Tiny; use Format::Util::Numbers qw( roundnear ); use Text::CSV; $csv = Text::CSV->new({ sep_char => ';' }); $file = path("./scores.csv"); if ( $file->exists ) { $sum = 0; $count = 0; $row = 0; @lines = $file->lines; chomp(@lines); foreach $line (@lines) { if($line) { $row++; if ($csv->parse($line)) { @fields = $csv->fields(); for ($i = 1; $i <= 3; $i++) { if ($fields[$i]) { $sum += $fields[$i]; $count++; } } } else { die("Line could not be parsed: $line\n"); } } } print("The average math score of $row students: " . roundnear( 0.1, $sum / $count) . "\n"); } else { print("$file does not exist.\n"); }
use List::MoreUtils qw(each_array); @array1 = ('a', 'b', 'c', 'd'); @array2 = (1, 2, 3, 4); # Create an iterator for the arrays $iter = each_array(@array1, @array2);# Loop through the arrays simultaneously while ( ($elem1, $elem2) = $iter->() ) { print "$elem1$elem2"; } print("\n");
use Term::Size; ($columns, $rows) = Term::Size::chars *STDOUT{IO}; die "Need 80 column screen" if ($columns < 80); $screen_width = $columns; $str = "We know what we are, but not what we may be."; $len = length($str); $empty = int( ($screen_width - $len) / 2); print("*" x $screen_width . "\n"); print(" " x $empty); print($str); print(" " x $empty . "\n"); print("*" x $screen_width); print("\n");