Link to home
Start Free TrialLog in
Avatar of uluttrell
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
Avatar of Tintin
Tintin

Your code prints "Match Found" as it will do that for every element in the array regardless of whether it matches of not.  

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:;
  }
}
#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;
Avatar of uluttrell

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?
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?
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+).
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ozo, It is printing nothing.  Yes, I do need the space.  I will try your suggestion.
Ozo, you are a genius!  Thanks for your help.