in reply to Saving Results of a while loop

I'm not sure exactly what problem you're having, but this code does have a couple of things wrong with it:

Try fixing these up, because your idea of saving the values in an array should do what you want. If this doesn't fix it, describe the error a little more specifically, and someone here will probably be able to help more.

HTH

Replies are listed 'Best First'.
Re^2: Saving Results of a while loop
by tadman (Prior) on Aug 01, 2001 at 18:49 UTC
    Update:
    As Hofmator was kind enough to point out, I was confusing qr with quotemeta, which are two entirely different beasties, though for some reason I had the two confused.

    Using \Q and \E is still one way to avoid having to go backslash crazy on your regex, though, and this was the original inspiration for this follow-up.

    The original post is preserved below.
    Using qr() on \b is going to look for "backslash,b" instead of a word break. Try this instead:
    $ip = "192.168.3.5"; # ... if ($line =~ /\b\Q$ip\E\b/) { # ... }
    Using \Q and \E ensure that no matter what $ip has in it, it will be matched very literally. You could also take this approach:
    $ip = qr("192.168.3.5"); # ... if ($line =~ /\b$ip\b/) { # ... }
    This makes sure that $ip is "regex-ready" before being used, by virtue of the qr() function.

      Well, I have to say no and no

      1. qr// works fine with \b, e.g.
        my $string = q/This is a test./; my $regex1 = qr(\bis); my $regex2 = qr(is); print $string =~ /$regex1/g, "\n"; print $string =~ /$regex2/g, "\n"; # prints is isis
      2. Your second part
        $ip = qr("192.168.3.5");
        matches e.g. "192 16833!5" but not what was being looked for. Did you maybe want to use the quotemeta function?? Then
        my $ip = "192.168.3.5"; $ip = quotemeta $ip;
        would be the way to go.

      -- Hofmator

        quotemeta would be the one. Looks like I got a little carried away there. In some of my programs I've aliased "qm" to be "quotemeta", which would account for some of the confusion on my part. The rest I can just chalk up to plain old fashioned ignorance.

        Thanks for pointing this out.