CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Unexpected results from assignment with regex match

 



daltonmv
New User

Nov 13, 2009, 9:04 AM

Post #1 of 4 (3761 views)
Unexpected results from assignment with regex match Can't Post

This one doesn't make sense at all. Unsure

I'm trying to grab the primary or main version number from a string kept in a file that is maintained by product patches. Here is a snippet of Perl code that reads the single line from that file and attempts to isolate that primary version number.

30 printf "We\'re running Perl %vd.\n", $^V;
31
32 $SID="%W%";
33 $RELEASE="%I%";
34
35 use Getopt::Std;
36 use Sys::Hostname;
37 use Date::Format;
38
39 #raw_NBU_version=$(</usr/openv/netbackup/bin/version)
40 #full_NBU_version=${raw_NBU_version##NetBackup-Solaris}
41 #whittled_NBU_version=${full_NBU_version##* }
42 #main_NBU_version=${whittled_NBU_version%%.*}
43 #if (( main_NBU_version < 6 ))
44 #then
45 # echo "NBU Version must be 6 or greater!"
46 # exit 99
47 #fi
48 $NBU_version_file = "/usr/openv/netbackup/bin/version";
49 open(my $NBU_VERSION, "<", $NBU_version_file) or die "Can't open NBU version file: $! $?\n";
50 if ( defined(my $NBU_version = <$NBU_VERSION>) ) {
51 chomp($NBU_version);
52 print "$NBU_version\n";
53 my $main_NBU_version = ($NBU_version =~ s/^NetBackup-Solaris10\s+(\d+)\.\d+\.\d+.*/$1/);
54 print "The main NBU version is \"$main_NBU_version\"\n";
55 # if (my $main_NBU_version = ($raw_NBU_version =~ /^NetBackup-Solaris10\s+(\d+)\..*/)) {
56 # print "$main_NBU_version\n";
57 # }
58 }
59 close($NBU_VERSION);
60

When I run it, I get this:

We're running Perl 5.8.4.
NetBackup-Solaris10 6.5.4
The main NBU version is "1"

I can't for the life of me figure out where the digit "1" comes from.

If I run a "sed" script using essentially the same regex against the same input file, I get what I expected to get:

sed -e 's/^NetBackup\-Solaris10 \([0-9]\{1,\}\)\.[0-9]\{1,\}\.[0-9]\{1,\}/\1/' /usr/openv/netbackup/bin/version
6

What do I not understand about the Perl implementation of regex processing? Thanks for any help offered.


ichi
User

Nov 13, 2009, 9:30 AM

Post #2 of 4 (3760 views)
Re: [daltonmv] Unexpected results from assignment with regex match [In reply to] Can't Post


Code
my $main_NBU_version = ($NBU_version =~ s/^NetBackup-Solaris10\s+(\d+)\.\d+\.\d+.*/$1/);

should be $NBU_version right?


FishMonger
Veteran / Moderator

Nov 13, 2009, 10:09 AM

Post #3 of 4 (3754 views)
Re: [daltonmv] Unexpected results from assignment with regex match [In reply to] Can't Post

Change:

Code
my $main_NBU_version = ($NBU_version =~ s/^NetBackup-Solaris10\s+(\d+)\.\d+\.\d+.*/$1/);


To:

Code
my ($main_NBU_version) = $NBU_version =~ /^NetBackup-Solaris10\s+(\d+)\.\d+\.\d+.*/;



daltonmv
New User

Nov 13, 2009, 10:34 AM

Post #4 of 4 (3751 views)
Re: [FishMonger] Unexpected results from assignment with regex match [In reply to] Can't Post

Wow! That did it! Thanks! Smile

We're running Perl 5.8.4.
NetBackup-Solaris10 6.5.4
The main NBU version is "6"

Now, why did it? Enquiring minds want to know...

I don't quite understand the significance of the enclosing parenthesis: "my ($main_NBU_version)".

Thank you very much!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives