@@ -5279,7 +5279,82 @@ sub factorial {
52795279### Perl Regex
52805280
52815281<details >
5282- <summary >How do you perform regular expresions in Perl?</summary ><br ><b >
5282+ <summary >Check if the word `electroencefalografista` exists in a string</summary ><br ><b >
5283+
5284+ ```
5285+ my $string = "The longest accepted word by RAE is: electroencefalografista";
5286+ if ($string =~ /electroencefalografista/) {
5287+ print "Match!";
5288+ }
5289+ ```
5290+ </b ></details >
5291+
5292+ <details >
5293+ <summary >Check if the word `electroencefalografista` does not exists in a string</summary ><br ><b >
5294+
5295+ ```
5296+ my $string = "The longest not accepted word by RAE is: Ciclopentanoperhidrofenantreno";
5297+ if ($string !~ /electroencefalografista/) {
5298+ print "Does not match!";
5299+ }
5300+ ```
5301+ </b ></details >
5302+
5303+
5304+ <details >
5305+ <summary >Replace the word `amazing`</summary ><br ><b >
5306+
5307+ ```
5308+ my $string = "Perl is amazing!";
5309+ $string =~ s/amazing/incredible/;
5310+ print $string;
5311+ # Perl is incredible!
5312+ ```
5313+ </b ></details >
5314+
5315+ <details >
5316+ <summary >Extract `hh:mm:ss` with capturing group `()` in the following datetime</summary ><br ><b >
5317+
5318+ ```
5319+ my $date = "Fri Nov 19 20:09:37 CET 2021";
5320+ my @matches = $date =~ /(.*)(\d{2}:\d{2}:\d{2})(.*)/;
5321+ print $matches[1];
5322+ # Output: 20:09:37
5323+ ```
5324+ </b ></details >
5325+
5326+ <details >
5327+ <summary >Extract all the elements that are numbers in an array</summary ><br ><b >
5328+
5329+ ```
5330+ my @array = ('a', 1, 'b', 2, 'c', 3);
5331+ my @numbers = grep (/\d/, @array); # Note: \d involves more digits than 0-9
5332+ map {print $_ . "\n" } @numbers;
5333+ ```
5334+
5335+ </b ></details >
5336+
5337+ <details >
5338+ <summary >Print all the linux system users that starts with d or D</summary ><br ><b >
5339+
5340+ - With a Perl one liner : D
5341+ ```
5342+ open(my $fh, '<', '/etc/passwd');
5343+ my @user_info = <$fh>;
5344+ map { print $& . "\n" if $_ =~ /^d([^:]*)/ } @user_info;
5345+ close $fh;
5346+ ```
5347+
5348+ - Avoiding one-liners
5349+
5350+ ```
5351+ foreach my $user_line (@user_info) {
5352+ if ($user_line =~ /^d([^:]*)/) {
5353+ print $& . "\n";
5354+ }
5355+ }
5356+ ```
5357+
52835358</b ></details >
52845359
52855360### Perl Files Handle
@@ -5303,7 +5378,7 @@ sub factorial {
53035378# We can use:
53045379# '>' Write (it clears a previous content if exists).
53055380# '>>' Append.
5306- open(my $fh, '>>', 'file_name.ext') or "Error: file can't be opened";
5381+ open(my $fh, '>>', 'file_name.ext') or die "Error: file can't be opened";
53075382print $fh "writing text...\n";
53085383close($fh);
53095384```
@@ -5313,7 +5388,7 @@ close($fh);
53135388<summary >How can you read a file and print every line?</summary ><br ><b >
53145389
53155390```
5316- open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
5391+ open(my $fh, '<', 'file_to_read.ext') or die "Error: file can't be opened";
53175392my @file = <$fh>;
53185393foreach my $line (@file) {
53195394 print $line;
@@ -5323,7 +5398,7 @@ foreach my $line (@file) {
53235398We can use the file handle without assigning it to an array:
53245399
53255400```
5326- open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
5401+ open(my $fh, '<', 'file_to_read.ext') or die "Error: file can't be opened";
53275402
53285403foreach my $line (<$fh>) {
53295404 print $line;
0 commit comments