Solved

Grep Array

Posted on 2004-04-29
9
1,940 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

820 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