I have two problems
a) I am trying to apply a subroutine to each file in a dir, and the subroutine only prints the result for the first file in the dir for all the files.
rfiles/msg00068.txt Location: TX Title: Analog IC Engineer - PLL BiCMOS Salary: $90k - $135 + bonus + stock opt's
rfiles/msg00069.txt Location: TX Title: Analog IC Engineer - PLL BiCMOS Salary: $90k - $135 + bonus + stock opt's
..... etc.
I Know that TX etc. is not the same value for all the files.
b) In the subroutine I am using a switch contruct so that if the first regex doesn't find anything, goto the second etc. This isn't working how I expected it to and my default which should print no match if the different regexs find nothing, doesn't happen. I tested the switch subroutines using one file at a time, and sometimes it works and other times it doesn't, I know the regexs work because if I try matching with the regex patterns that apply only to a particular file, I get the data I want. But since the pattern will be different in several files, I tried the switch structure.
I need help on how to fix this script. Thanks.
#!/usr/bin/perl
sub location{
local($result) = '';
print " Location: ";
SWITCH: {
# location match 1
(~ /(\_\_subject\_\_\:\s(US\s|US)\-\s(\w\w)\s(.*|\-\s(.*))\n)/ig) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# location match 2
(~ /((Location\:|Location)\s+((.*)*)\n)/g) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# location match 3
(~ /(\_\_subject\_\_\:\s)((\w+\s)*)/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
print "no match ";
} # end of CASE block
}
sub title{
local($result) = '';
print " Title: ";
SWITCH: {
# title match 1
(~ /((title\:|title)\s+(.*))\n/ig) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# title match 2
(~ /(\_\_subject\_\_\:\s(.*)\n)/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
print "no match ";
} # end of CASE block
}
sub salary{
local($result) = '';
print " Salary: ";
SWITCH: {
# salary match 1
(~ /((salary\srange\:|salary\:|salary)\s+(\$\d.*))\n/ig) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# salary match 2
(~ /(^(salary\srange\:|salary\:|salary)\s+.+(\$\d.*))\n/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
# salary match 3
(~ /(salary\s\-\s).+(\$\d.*)/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
print "no match";
} # end of CASE block
}
my $dir = 'rfiles';
my $pattern = '.txt$';
#Filenames ending in .txt
opendir DIR, $dir or die "Cannot readdir $dir:$!\n";
my @files = grep /$pattern/,(readdir DIR);
closedir DIR;
for (@files) {
my $file = "$dir/$_";
open IN, $file or die "Cannot open $file:$!\n";
while (<IN>) {
# do stuff with the contents of $file
$text .= $_; #reads the text and stores
}
close (IN);
print "\n$file ";
$_ = $text;
&location;
&title;
&salary;
}
a) I am trying to apply a subroutine to each file in a dir, and the subroutine only prints the result for the first file in the dir for all the files.
rfiles/msg00068.txt Location: TX Title: Analog IC Engineer - PLL BiCMOS Salary: $90k - $135 + bonus + stock opt's
rfiles/msg00069.txt Location: TX Title: Analog IC Engineer - PLL BiCMOS Salary: $90k - $135 + bonus + stock opt's
..... etc.
I Know that TX etc. is not the same value for all the files.
b) In the subroutine I am using a switch contruct so that if the first regex doesn't find anything, goto the second etc. This isn't working how I expected it to and my default which should print no match if the different regexs find nothing, doesn't happen. I tested the switch subroutines using one file at a time, and sometimes it works and other times it doesn't, I know the regexs work because if I try matching with the regex patterns that apply only to a particular file, I get the data I want. But since the pattern will be different in several files, I tried the switch structure.
I need help on how to fix this script. Thanks.
#!/usr/bin/perl
sub location{
local($result) = '';
print " Location: ";
SWITCH: {
# location match 1
(~ /(\_\_subject\_\_\:\s(US\s|US)\-\s(\w\w)\s(.*|\-\s(.*))\n)/ig) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# location match 2
(~ /((Location\:|Location)\s+((.*)*)\n)/g) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# location match 3
(~ /(\_\_subject\_\_\:\s)((\w+\s)*)/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
print "no match ";
} # end of CASE block
}
sub title{
local($result) = '';
print " Title: ";
SWITCH: {
# title match 1
(~ /((title\:|title)\s+(.*))\n/ig) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# title match 2
(~ /(\_\_subject\_\_\:\s(.*)\n)/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
print "no match ";
} # end of CASE block
}
sub salary{
local($result) = '';
print " Salary: ";
SWITCH: {
# salary match 1
(~ /((salary\srange\:|salary\:|salary)\s+(\$\d.*))\n/ig) && do{
$result = $3;
print "$result ";
last SWITCH;
};
# salary match 2
(~ /(^(salary\srange\:|salary\:|salary)\s+.+(\$\d.*))\n/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
# salary match 3
(~ /(salary\s\-\s).+(\$\d.*)/ig) && do{
$result = $2;
print "$result ";
last SWITCH;
};
print "no match";
} # end of CASE block
}
my $dir = 'rfiles';
my $pattern = '.txt$';
#Filenames ending in .txt
opendir DIR, $dir or die "Cannot readdir $dir:$!\n";
my @files = grep /$pattern/,(readdir DIR);
closedir DIR;
for (@files) {
my $file = "$dir/$_";
open IN, $file or die "Cannot open $file:$!\n";
while (<IN>) {
# do stuff with the contents of $file
$text .= $_; #reads the text and stores
}
close (IN);
print "\n$file ";
$_ = $text;
&location;
&title;
&salary;
}