Solved

Grep Array

Posted on 2004-04-29
9
1,936 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
  • 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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 …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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