Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

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.
0
uluttrell
Asked:
uluttrell
  • 6
  • 5
  • 2
  • +1
1 Solution
 
ultimatemikeCommented:
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
 
ultimatemikeCommented:
Misread your question.  To assign it to a scalar variable replace


print $1;

with

my $result = $1;
0
 
uluttrellAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
FishMongerCommented:
You need to add parentheses.

($scalar) = /something=(.*)/;
0
 
uluttrellAuthor Commented:
FishMonger, I tried that.  The code prints an empty file.
0
 
geotigerCommented:
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
 
FishMongerCommented:
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
 
uluttrellAuthor Commented:
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
 
FishMongerCommented:
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
 
FishMongerCommented:
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
 
FishMongerCommented:
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
 
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.
0
 
uluttrellAuthor Commented:
FishMonger, It was a typo in the script.  As always, thank you for your help.  I appreciate it.
0
 
FishMongerCommented:
You're welcome...glad i was able to help (with both questions)!

Ron
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now