11.37. split with regular expresion

LIST = split REGEX, STRING;

Example 11-11. examples/regex/field_value_pairs.txt

fname    =    Foo
lname    = Bar
email=foo@bar.com

Example 11-12. examples/regex/parse_field_value_pairs.pl

#!/usr/bin/perl
use strict;
use warnings;

# data: field_value_pairs.txt
my $filename = shift or die "Usage: $0 filename\n";

open(my $fh, "<", $filename) or die "Could not open '$filename'\n";
while (my $line = <$fh>) {
    chomp $line;
    my ($field, $value) = split /\s*=\s*/, $line;
    print "$value=$field\n";
}

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed