FAQ
hi people

I'm trying to get the value of $1 into a var, the following don't work
and I can't figure out why

$_ = $html_body;
my ($resrow) = m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
#print $1;
print $resrow;

$resrow holds nothing, however if I print $1 I do have a match

$_ = $html_body;
m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
my $resrow = $1;
print $resrow;

doesn't work either.

How do I get $1 into a var for later use. It's driving me a little loopy
now.

I'm basically trying to extract the middle bit into a var of it's own. I
thought that would be easy....

Cheers

Angie

Search Discussions

  • Charles K. Clarkson at Jan 7, 2004 at 1:10 pm
    angie ahl wrote:
    :
    : I'm trying to get the value of $1 into a var, the following
    : don't work and I can't figure out why
    :
    : $_ = $html_body;
    : my ($resrow) = m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    : #print $1;
    : print $resrow;
    :
    : $resrow holds nothing, however if I print $1 I do have a
    : match
    :
    : $_ = $html_body;
    : m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    : my $resrow = $1;
    : print $resrow;

    Perhaps $html_body does not hold what you think it holds.
    Commenting one line or the other seemed to work fine for me.

    $_ = q|<!-- VM: resrow -->foo<!-- VM: /resrow -->|;

    my ($resrow) = m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    print $1;
    print $resrow;

    __END__
    Prints:
    foofoo


    HTH,

    Charles K. Clarkson
    --
    Head Bottle Washer,
    Clarkson Energy Homes, Inc.
    Mobile Home Specialists
    254 968-8328
  • Rob Dixon at Jan 7, 2004 at 1:28 pm

    Angie Ahl wrote:
    I'm trying to get the value of $1 into a var, the following don't work
    and I can't figure out why

    $_ = $html_body;
    my ($resrow) = m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    #print $1;
    print $resrow;

    $resrow holds nothing, however if I print $1 I do have a match

    $_ = $html_body;
    m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    my $resrow = $1;
    print $resrow;

    doesn't work either.

    How do I get $1 into a var for later use. It's driving me a little loopy
    now.

    I'm basically trying to extract the middle bit into a var of it's own. I
    thought that would be easy....
    Hi Angie.

    It looks like your regex isn't matching. $1 will keep the value captured by
    the last successful match.

    Try this code. (The 'for ($html_body) { .. }' aliases $_ with $html_body
    for the extent of the code block.)

    my $resrow;

    for ($html_body) {
    if (m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#) {
    $resrow = $1;
    }
    else {
    die "No match for HTML";
    }
    }

    print $resrow, "\n";

    So I guess you need to fix your regex. Is it possible that the actual
    text contains a newline? In which case you need to replace spaces with
    '\s+'.

    HTH,

    Rob
  • R. Joseph Newton at Jan 7, 2004 at 2:47 pm

    angie ahl wrote:

    hi people

    I'm trying to get the value of $1 into a var, the following don't work
    and I can't figure out why

    $_ = $html_body;
    my ($resrow) = m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    #print $1;
    print $resrow;

    $resrow holds nothing, however if I print $1 I do have a match
    How do you know? How are you testing to ensure that a match was found?

    $_ = $html_body;
    m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#;
    my $resrow = $1;
    print $resrow;

    doesn't work either.

    How do I get $1 into a var for later use. It's driving me a little loopy
    now.
    Just the way you show in your second example. If a match was found, the
    captured text will appear in $resrow.

    Better to find out:

    if (m#<!-- VM: resrow -->(.*?)<!-- VM: /resrow -->#) {
    print "$1\n";
    } else {
    print "Oops, back to the drawing board with this regex, huh?\n";
    }

    Joseph
  • Angie ahl at Jan 7, 2004 at 7:52 pm
    Thanks to everyone who helped me with this one, I had a deadline to meet
    that is now met.

    It was a missing ~ and me failing to use s on the end of my pattern

    may be of use to other users but for some reason my Linux server needs
    s///s; to match over newlines and my OSX set up doesn't, that didn't
    help ;)

    Angie
  • Dan Muey at Jan 7, 2004 at 7:55 pm

    Thanks to everyone who helped me with this one, I had a
    deadline to meet that is now met.

    It was a missing ~ and me failing to use s on the end of my pattern

    may be of use to other users but for some reason my Linux
    server needs s///s; to match over newlines and my OSX set up
    doesn't, that didn't help ;)
    OSX! Yummy! It probably has something to do with the Unix/Mac/Winders
    newline character issue. They all use different characters for that
    so it can cause issues.


    Dmuey
    Angie

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>
  • Jan Eden at Jan 8, 2004 at 8:46 am

    Dan Muey wrote:

    Thanks to everyone who helped me with this one, I had a
    deadline to meet that is now met.

    It was a missing ~ and me failing to use s on the end of my pattern

    may be of use to other users but for some reason my Linux
    server needs s///s; to match over newlines and my OSX set up
    doesn't, that didn't help ;)
    OSX! Yummy! It probably has something to do with the Unix/Mac/Winders
    newline character issue. They all use different characters for that
    so it can cause issues.
    Well, OS X is a BSD system, so it uses UNIX newline characters. I have not experienced the behaviour Angie described: Perl 5.8.1 on my OS X (10.3.2) does not match newlines using the magical dot if the pattern modifier /s is not set.

    Which environment/app did you use to test on your local OS X box?

    - Jan
    --
    The day Microsoft makes something that doesn't suck is the day they start selling vacuum cleaners.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 7, '04 at 12:26p
activeJan 8, '04 at 8:46a
posts7
users6
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase