Link to home
Start Free TrialLog in
Avatar of uluttrell
uluttrell

asked on

Assign Scalar Variable

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.
Avatar of ultimatemike
ultimatemike

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.
Misread your question.  To assign it to a scalar variable replace


print $1;

with

my $result = $1;
Avatar of uluttrell

ASKER

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.

Avatar of FishMonger
You need to add parentheses.

($scalar) = /something=(.*)/;
FishMonger, I tried that.  The code prints an empty file.
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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=(.*)/);
}
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.
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.
FishMonger, It was a typo in the script.  As always, thank you for your help.  I appreciate it.
You're welcome...glad i was able to help (with both questions)!

Ron