uluttrell
asked on
Grep Array
Greetings Perl Experts,
This is not a homework assignment.
I have written the following code.
==Begin code.pl
#!/usr/bin/perl
my @array = "trial.out";
my $element;
for $element(@array) {
$ring =~ /AlphaBetaGamma (4, 0) ==> (\d+)/;
print "Match Found\n";
my $value = $1;
#print "$ring\n";
print "$value\n";
}
==End code.pl
The code returns that the match is found; however, it does not print $value.
TIA
This is not a homework assignment.
I have written the following code.
==Begin code.pl
#!/usr/bin/perl
my @array = "trial.out";
my $element;
for $element(@array) {
$ring =~ /AlphaBetaGamma (4, 0) ==> (\d+)/;
print "Match Found\n";
my $value = $1;
#print "$ring\n";
print "$value\n";
}
==End code.pl
The code returns that the match is found; however, it does not print $value.
TIA
#I suspect you may also have meant do something like
open F,"<trial.out" or die "Couldn't open trial.out $!";
@array = <F>;
close F;
open F,"<trial.out" or die "Couldn't open trial.out $!";
@array = <F>;
close F;
ASKER
I have more details. I am parsing through a log file and assigning matches to an array. The array that is created is an array of strings all separated by a newline. I want to remove everything before the regex and assign the (\d+) to a second array so that I can add all the number in the array.
Is the
print "Match found: $1\n";
printing the numbers you want to add?
print "Match found: $1\n";
printing the numbers you want to add?
Is your ultimate goal to simply add all the numbers from the log file, or are you also using the other data in the file?
ASKER
Ozo, no, the 'print "Match found: $1\n" is not printing the numbers I want to add.
Tintin, yes, the ultimate goal is simply to add teh number that match (\d+).
Tintin, yes, the ultimate goal is simply to add teh number that match (\d+).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ozo, It is printing nothing. Yes, I do need the space. I will try your suggestion.
ASKER
Ozo, you are a genius! Thanks for your help.
Your example is a little confusing, but I think it should be rewritten like:
#!/usr/bin/perl
my @array = qw(trail.out);
foreach my $element (@array) {
if (/AlphaBetaGamma \(4,0\) ==> (\d+)/) {
print "Match found: $1\n:;
}
}