NinjaAnte
NinjaAnte

Reputation: 23

Perl - Using regex on a text file to get specific string input in an array

So im trying to learn how to use regular expressions in Perl. I have a textfile.txt that contains information and i want to access specific portions of that textfile. The textfile.txt contains the following entry (first 3 lines):

Jan    2016-01-01 Friday   12:00
Feb    2016-02-01 Monday   23:45
Mar    2016-03-01 Tuesday  15:30

What I want to do is to put the names of the month "Jan/feb/mar" in one array, their numerical value "2016-01-01" in a second array. My current script takes the entire first line and puts it in the same element. This is my code for writing to the array so far:

while (<FILE>) {
push (@newArray, $_);
}
close FILE

How would I go about only putting the entries of the date (2016-01-01) or the name of the month (Jan/feb/mar) into the array from the file, instead of putting the entire line into the array element?

Upvotes: 0

Views: 102

Answers (2)

choroba
choroba

Reputation: 241998

Create capture groups () to extract information from a matching regular expression:

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

my (@months, @dates);
while (<DATA>) {
    if (my ($month, $date) = /^(...) \s+ ([0-9-]+)/x) {
        push @months, $month;
        push @dates, $date;
    }
}
print "@months\n@dates\n";

__DATA__
Jan    2016-01-01 Friday   12:00
Feb    2016-02-01 Monday   23:45
Mar    2016-03-01 Tuesday  15:30

If you want to only accept month names, you can change the first group from (...) to (A(?:pr|ug)|Dec|Feb|J(?:an|u[ln])|Ma[ry]|Nov|Oct|Sep).

Upvotes: 0

Sobrique
Sobrique

Reputation: 53498

I wouldn't use a regex but instead split:

#!/usr/bin/perl

use warnings;
use strict;

use Data::Dumper; 

my @month_words;
my @month_dates;
my %month_lookup;

while ( <DATA> ) {
   my ( $mon, $date, $day, $time ) = split; 
   push ( @month_words, $mon );
   push ( @month_dates, $date ); 
   $month_lookup{$mon} = $date; 
}

print Dumper \@month_words, \@month_dates, \%month_lookup;

__DATA__
Jan    2016-01-01 Friday   12:00
Feb    2016-02-01 Monday   23:45
Mar    2016-03-01 Tuesday  15:30

This prints the two arrays, and the hash:

$VAR1 = [
          'Jan',
          'Feb',
          'Mar'
        ];
$VAR2 = [
          '2016-01-01',
          '2016-02-01',
          '2016-03-01'
        ];
$VAR3 = {
          'Mar' => '2016-03-01',
          'Feb' => '2016-02-01',
          'Jan' => '2016-01-01'
        };

Upvotes: 3

Related Questions