Grep Array

Posted on 2004-04-29
Last Modified: 2007-12-19
Greetings Perl Experts,
This is not a homework assignment.  
I have written the following code.
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";

The code returns that the match is found; however, it does not print $value.
Question by:uluttrell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 48

Expert Comment

ID: 10954421
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:

my @array = qw(trail.out);

foreach my $element (@array) {
  if (/AlphaBetaGamma \(4,0\) ==> (\d+)/) {
     print "Match found: $1\n:;
LVL 84

Expert Comment

ID: 10955777
#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;

Author Comment

ID: 10968919
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 84

Expert Comment

ID: 10969224
Is the
    print "Match found: $1\n";
printing the numbers you want to add?
LVL 48

Expert Comment

ID: 10973906
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?

Author Comment

ID: 10979491
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+).
LVL 84

Accepted Solution

ozo earned 250 total points
ID: 10979829
Is it printing too many numbers?  Is it not printing enough numbers?
What are the $element values it gets wrong?
Do you need a space in the \(4,0\)?  if so, try \(4,\s*0\)

Author Comment

ID: 10981464
Ozo, It is printing nothing.  Yes, I do need the space.  I will try your suggestion.

Author Comment

ID: 10982029
Ozo, you are a genius!  Thanks for your help.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question