Solved

Grep Array

Posted on 2004-04-29
9
1,943 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:uluttrell
[X]
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
9 Comments
 
LVL 48

Expert Comment

by:Tintin
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:

#!/usr/bin/perl
my @array = qw(trail.out);

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

Expert Comment

by:ozo
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;
0
 

Author Comment

by:uluttrell
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.
0
Independent Software Vendors: 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

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

Expert Comment

by:Tintin
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?
0
 

Author Comment

by:uluttrell
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+).
0
 
LVL 84

Accepted Solution

by:
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\)
0
 

Author Comment

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

Author Comment

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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

737 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