in reply to Pulling 5 Character Strings out of an Array of Text Files

Welcome to the Monastery, editholla!

You don't specify clearly where the letter at the end of your string is coming from, so in this example, I've just hardcoded it in.

I've made a few small changes, most notably using the three-argument form of open, and outputting the actual error message that was set if there is one ($!). I also changed from using a bareword file handle to a lexical one.

After each file found is opened, we loop through the file line-by-line, then using a regex capture, if anything is found fitting the criteria we put the found string into a hash as its key, thus eliminating duplicates.

use strict; use warnings; my $way = "test"; my (@rit, $rid); opendir $rid, $way; while ( my $entry = readdir $rid ) { next unless -f $way . '/' . $entry; next if $entry eq '.' or $entry eq '..'; push @rit, $entry; } closedir $rid; my %data; for (@rit){ my $cat = "$way/$_"; open my $fh, '<', $cat or die "Can't open $cat: $!"; while (my $line = <$fh>){ chomp $line; if ($line =~ /(4\w{3}A)/){ $data{$1}++; } } } for my $k (keys %data){ print "$k\n"; }

Output:

4060A 4099A

Replies are listed 'Best First'.
Re^2: Pulling 5 Character Strings out of an Array of Text Files
by editholla (Initiate) on Jan 05, 2016 at 15:18 UTC
    I don't get any errors running your code but I also don't get any output. I have input my path to $way but made no other changes. Let me know if you have any insight. Thank you!