Solved

Grep Array

Posted on 2004-04-29
9
1,931 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
Comment Utility
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
Comment Utility
#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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is the
    print "Match found: $1\n";
printing the numbers you want to add?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ozo, It is printing nothing.  Yes, I do need the space.  I will try your suggestion.
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
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…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now