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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just use the $1 variable, that stores the value first pattern matched inside ().


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;


my $result = $1;
uluttrellAuthor Commented:
here are the lines of my code
print FH "$scalar \n";

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

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

You need to add parentheses.

($scalar) = /something=(.*)/;
uluttrellAuthor Commented:
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.

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";

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

pattern else
another pattern
another pattern else
uluttrellAuthor Commented:
Thanks for the explanation FishMonger.  The code now prints the entire line instead of just the mactched pattern.
Here is the code:
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";
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
   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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day 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
   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.
uluttrellAuthor Commented:
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.
uluttrellAuthor Commented:
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)!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.