I can't get this script ot work and I wa wondering if anyone could help?
I need to open a file and use a subroutine to search each line for a regular expression. If it matches then I need to return a match from the subroutine and print the result?
Any help would be greatly appreciated. I know it is simple but I can't seem to figure it out for some reason.
Thanks,
JD
#!/usr/bin/perl -w
#
#
$filename = "data.txt";
open FILE, $filename or die("could not open $filename for reading: $!");
foreach my $line (<FILE>) {
my $matched_line = &find_match($line);
if ($matched_line) {
print "$matched_line\\n";
}
}
close FILE;
sub find_match {
$grep = @_;
if ($grep =~ m/blue/){
return $grep;
}
the scalar will equal the length of the list instead of the value of any of the list elements. Using $_[0] properly assigns the value of a scalar to a scalar.
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'data.txt';
open FILE, $filename or die "could not open $filename for reading: $!";
while (my $line (<FILE>) {
print $line if ($line =~ m/blue/);
}
close FILE;
i need a regex that will poplate $first with the first word and then $second with the second word after a space. How can i then return the two variables?
Any help would be greatly appreciated. Been trying to do this all day.