Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Assign Scalar Variable

Posted on 2003-10-24
14
390 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 100
Filtering a file to table 9 101
syslog unix file 20 76
what are these modules in perl script 4 103
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

829 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