Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Use of uninitialized value when using regex

by Erez (Curate)
on Sep 12, 2010 at 13:50 UTC ( #859847=note: print w/ replies, xml ) Need Help??


in reply to Re: Use of uninitialized value when using regex
in thread Use of uninitialized value when using regex

the expression $var =~ /regex/ returns a true or false value, not the captured value. The special variable $1 contains the value you captured in the parentheses, so you should use something along the following lines:

my $build; if ($vadata =~ /=(\d+)\s*$/) { $build = $1; }
and use $build only if it's not null.

UPDATE: actually the OP was matching in list context, not scalar context, so a messed up answer. Please ignore and see above and below for a more enlightened discussion...

"Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert..


Comment on Re^2: Use of uninitialized value when using regex
Download Code
Re^3: Use of uninitialized value when using regex
by liverpole (Monsignor) on Sep 12, 2010 at 14:14 UTC
    Hi Erez,

    In scalar context:

    my $build = ($vdata=~/=(\d+)$/);
    you're correct; you will get 0 or 1.  But in list context:
    (my $build) = ($vdata=~/=(\d+)$/);
    you will get the captured values.  For example:
    use strict; use warnings; my $text = "The quick brown fox jumps over the lazy dog"; my ($h, $i, $j) = $text =~ /(\S*h\S*).*?(\S*i\S*).*?(\S*j\S*)/; print "$h, $i, $j\n"; __END__ Output: The, quick, jumps

    Hi roboticus,

    I, too, thought this a simple case of not matching the newline.  However, I can't explain why I'm getting the following:

    use strict; use warnings; # my $dir1 = 'Z:\My Documents\Workspace'; # my $versionFile = "$dir1\\version.txt"; # open (VFILE, $versionFile)|| die $!; # my $vdata= <VFILE>; # close (VFILE); try_this("Version=2010-09-01_17-29-04 Build=26"); try_this("Version=2010-09-01_17-29-04 Build=26\n"); try_this("Version=2010-09-01_17-29-04 Build=26\n\n"); sub try_this { my ($vdata) = @_; print "=" x 69, "\n"; print "Str: [$vdata]\n"; printf "Asc: %s\n\n", join ' ', map { ord $_ } split //, $vdata; (my $build) = ($vdata=~/=(\d+)$/); print "Build = [$build]\n\n";#this is the problem line } __END__ Output: ===================================================================== Str: [Version=2010-09-01_17-29-04 Build=26] Asc: 86 101 114 115 105 111 110 61 50 48 49 48 45 48 57 45 48 49 95 49 55 45 50 57 45 48 52 32 66 117 105 108 100 61 50 54 Build = [26] ===================================================================== Str: [Version=2010-09-01_17-29-04 Build=26 ] Asc: 86 101 114 115 105 111 110 61 50 48 49 48 45 48 57 45 48 49 95 49 55 45 50 57 45 48 52 32 66 117 105 108 100 61 50 54 10 Build = [26] ===================================================================== Str: [Version=2010-09-01_17-29-04 Build=26 ] Asc: 86 101 114 115 105 111 110 61 50 48 49 48 45 48 57 45 48 49 95 49 55 45 50 57 45 48 52 32 66 117 105 108 100 61 50 54 10 10 Use of uninitialized value $build in concatenation (.) or string at D:\Documents and Settings\jonorton\a.pl line 25. Build = []

    The first string (no newlines) matches as expected, and the third (2 newlines) fails, also as expected.  But the second (1 newline) matches, despite the newline at the end.  I'm not sure what to make of that.

    This is on perl v5.10.0 for MSWin32-x86-multi-thread (Windows xp), binary build 1004 from ActiveState.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://859847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-08-23 18:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls