Q:

Using Regex to match pattern and store into array

I had a similar post on this (see other post), but what I am trying to do has slightly varied since then. I am using the same regex matching /79(week\d+[a-z])/i from that question.
I have a file that contains six lines, each line a different path. As an example, there are 3 lines including the string "cat" and 3 lines including the string "dog".
file.txt
/mypath/sd-urt7-dfc-adfj345h-d0-79week48a-DFC-lk-my_cat.text  
/mypath/sd-urt7-afd-parent-79week46d-AFD-lk-my_cat.text  
/mypath/sd-urt7-ert-parent-79week50c-ERT-lk-my_cat.text 
/mypath/sd-urt7-dfc-adfj345h-d0-79week48a-DFC-lk-my_dog.text  
/mypath/sd-urt7-afd-parent-79week46d-AFD-lk-my_dog.text  
/mypath/sd-urt7-ert-parent-79week49b-ERT-lk-my_dog.text 

I want to take the "weekxxX" portion out of each path and print it out into a file. Here is what I have so far:
use strict;
use warnings;
use feature qw(say);
use autodie;

    open (my $fh, '<', "file.txt") or die "Couldn't open `file.txt`\n";

    foreach my $line (<$fh>){ #goes through each line in the file

        chomp $line; #chomps new line character from each line

        if ($line =~ /cat/) { #if the path includes string "cat"
            #use the regex at top of post to get weekxxX of each
            #path containing cat and print all 3 weekxxX into 
            #another file called cat.  

            open (my $cat, '>', "cat.txt") or die;
            print $cat "";
            close $cat; 

        }
        elsif ($line =~ /dog/) { #otherwise if the path includes string "dog"3

            #use the regex at top of post to get weekxxX of each
            #path containing cat and print all 3 weekxxX into 
            #another file called dog.  

            open (my $dog, '>', "dog.txt") or die;
            print $dog "";
            close $dog;
        }
    }
    close $fh;

I am thinking this can be done by using regex on all three paths (depending on whether it is dog or cat), push all three weeks in format weekxxX into an array and then print that array into the file? I'm really not sure how to implement this. 

A:

use warnings;
use strict;
use feature 'say';
use autodie qw(open);

my $file = shift // die "Usage: $0 filename\n";  #/

# Patterns that determine what file to write to    
my ($p1, $p2) = qw(cat dog);

# Open output files, one for each pattern
my %fh_out = map { open my $fh, '>', "$_.txt"; $_ => $fh } ($p1, $p2); 

open my $fh, '<', $file;

while (<$fh>) {
    if ( my ($week, $type) = /79(week\d+[a-z]).*?($p1|$p2)/i ) {
        say {$fh{$type}} $week;
    }
}

A:

use strict;
use warnings;
use feature 'say';

open(my $in_fh, '<', "file.txt")
   or die("Can't open \"file.txt\": $!\n");    
open(my $cat_fh, '>', "cat.txt")
   or die("Can't create \"cat.txt\": $!\n");
open(my $dog_fh, '>', "dog.txt")
   or die("Can't create \"dog.txt\": $!\n");

while (<$in_fh>) {
   chomp;
   if ( my ($week) = /79(week\d+[a-z])/i) {
      if    (/cat/) { say $cat_fh $week; }
      elsif (/dog/) { say $dog_fh $week; }
   }
}

