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
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");
2. Determine the character with the lowest or highest ASCII value of a list with the characters z, c, m
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");
3. How do two strings differ?
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");
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("\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");
5. Encode strings like 'Voilà un gâteau étonnant à la crème brûlée' with HTML entities.
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");
Path::Tiny is for average needs (amongst others: reading/writing/moving files; making/removing/reading directories). The module IO::All will do everything you can imagine and more.
9. Text file replacement
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");
}
10. Convert non-ASCII characters (e.g. gyönyörű élményekkel 😊) to their ASCII equivalents
12. Calculate average math score from a CSV file; format each CSV row: name;score_1;score_2;score_3.
# 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");
}
13. Merge the items a b c d from array_1 with the items 1 2 3 4 from array_2. The result should be: a1b2c3d4. Find a suitable module.
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");
14. Center a string in your terminal screen (width >= 80 pixels and exit if width < 80). Use Term::Size to retrieve the terminal size on Unix (or Term::Size::Win32 on Windows). Test your program with several screen sizes.
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");