Solved

Assign Scalar Variable

Posted on 2003-10-24
14
377 Views
Last Modified: 2010-03-04
This is not a homework assignment.
I am using a regex to match a pattern on the line.  If the pattern is found, I want to assign a scalar variable with the text that appears after the =.  Need help in assigning the variable.
0
Comment
Question by:uluttrell
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9616153
Just use the $1 variable, that stores the value first pattern matched inside ().


So:


while (<>) {
     /^.*=(.*)$/;
     print $1;

}


This will check each line against the regular expression once, and print out the part after the =
The ^ indicates the beginning of the line, and the $ indicates the end of the line.  (.*) says to match
as many characters as possible and store that value in $1.
0
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9616160
Misread your question.  To assign it to a scalar variable replace


print $1;

with

my $result = $1;
0
 

Author Comment

by:uluttrell
ID: 9616247
here are the lines of my code
$scalar=/pattern=(.*?)/;
print FH "$scalar \n";

When i view the contents of the FH, tne numeral one appears.

0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9616565
You need to add parentheses.

($scalar) = /something=(.*)/;
0
 

Author Comment

by:uluttrell
ID: 9616633
FishMonger, I tried that.  The code prints an empty file.
0
 
LVL 12

Expert Comment

by:geotiger
ID: 9616735
There are many ways to do this. This is the one that I use often:

my $re = qr{(\w+)}/q;   # you can define your pattern here

my @words = ($your_text_line =~ $re);


Hope this helps.

GT
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9616747
I should have clairfied this, but in addition to adding the parenteses, you also need to remove the question mark or add $ (end of string anchor).  The (.*?) expression says to match any char zero or more times but as few as possibe; which in this case (since you're not using the $ anchor) means zero times.  Here's the test that I ran and it's output.

while (<DATA>) {
   ($scalar) = /pattern=(.*)/;
   print "$scalar\n";
}

__DATA__
the pattern=pattern
the pattern=pattern else
the pattern=another pattern
the pattern=another pattern else


--output--
pattern
pattern else
another pattern
another pattern else
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:uluttrell
ID: 9617187
Thanks for the explanation FishMonger.  The code now prints the entire line instead of just the mactched pattern.
Here is the code:
===Begin code.pl
#!/usr/bin/perl
use strict;
use warnings;
my $file = ">results.txt"; #The results file
my $log = "log.txt"; #The log file
open (FH,$file) || die("Can't open $file\n"); #Open the results file
open(LOG,$log) || die ("Can't open $log\n");# Open the log file
while(<LOG>){   # While the log file is open do
  my $line = $_;  # Define the line as the current line
  chomp $line;  # Remove blank space from end of line
  if (/patternA/){ #
  my ($fromhost) = /fromhost=(.*)/;
  print FH "$fromhost\n";
        }
  }
===End code.pl
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 100 total points
ID: 9617598
Off hand, I don't see why your code is printing the entire line.
However, I have some questions and comments.

1)  Both  if (/patternA/) and ($fromhost) = /fromhost=(.*)/ are being applied to $_ not $line.
2)  Since you're not using $line anywhere in this code, why assign it?
3)  I assume patternA and fromhost=... are both on the same line, right?
4)  Could you post a couple of the actual lines from your log file so I can run a more realistic test?
5)  Personally, I perfer to code the opening of FH like this:
   my $file = 'results.txt'; #The results file
   open(FH, ">$file") or die "Can't open $file <$!>\n"; #Open the results file


Here's another test I ran; see if it does what you need.

#!/usr/bin/perl -w

use strict;

my $file = 'results.txt'; #The results file
my $log = 'log.txt'; #The log file

open(FH, ">$file") or die "Can't open $file <$!>\n"; #Open the results file
open(LOG, $log) or die "Can't open $log <$!>\n";   # Open the log file
while(<LOG>){   # While the log file is open do
   chomp;
   if (/patternA/){
      my ($fromhost) = /fromhost=(.*)/;
      print FH "$fromhost\n";
   }
}
close FH;
close LOG;

==end code==

--contents of log.txt--
the patternA is fromhost=apple
the patternA is fromhost=pear
the patternA is fromhost=orange
the patternA is fromhost=grape
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9617632
If my understanding of what you need is correct, then you could reduce the while loop to this.

while(<LOG>){   # While the log file is open do
   chomp;
   print FH "$1\n" if (/patternA/ && /fromhost=(.*)/);
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9617859
I ran your code against my test data set and it outputed the desired results.  I still didn't see any reason why it printed the entire line for you.  The only difference between your code and my revision of it, is style; they both should produce the same results.
0
 

Author Comment

by:uluttrell
ID: 9618003
Could have been because I was using cygwin.  I will try it on a Unix platform and let you know how it works.
Thank you.
0
 

Author Comment

by:uluttrell
ID: 9618082
FishMonger, It was a typo in the script.  As always, thank you for your help.  I appreciate it.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9618101
You're welcome...glad i was able to help (with both questions)!

Ron
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

20 Experts available now in Live!

Get 1:1 Help Now