Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Memory Leaks and %+

by MaphsterB (Initiate)
on Mar 01, 2012 at 13:36 UTC ( #957214=note: print w/replies, xml ) Need Help??

in reply to Memory Leaks and %+

Dang. I was hoping it was something wrong with my code, and therefore fixable. Unfortunately, for the intent & purpose of my particular script I'm stuck with 5.10. Network distribution and company policy and all that.

I will try it out on a newer version when I get the chance and report back, though.

In the meantime, I've found a quick n'dirty fix in case anyone else encounters this archaic headache. Just switch to @+ and @- instead of the more convenient %+:

#!/usr/bin/perl use strict; use warnings; open(my $fh, '<', 'EXAMPLE.TXT'); my $regexp = qr/(?<value1>\d+)\s+(?<value2>\d+)/; while(<$fh>) { next unless /$regexp/; my $value1 = substr($_, $-[1], $+[1] - $-[1]); my $value2 = substr($_, $-[2], $+[2] - $-[2]); print "GOT $value1 $value2\n"; }

And thanks for the responses!


Replies are listed 'Best First'.
Re^2: Memory Leaks and %+
by ruzam (Curate) on Mar 01, 2012 at 14:07 UTC

    Isn't it easier to just switch over to $1 and $2 at this point? Your new substr workaround isn't very readable (to me anyway), so it appears that you've lost the one thing you were hoping to gain by using %+.

    my $regexp = qr/(\d+)\s+(\d+)/; while(<$fh>) { next unless /$regexp/; my $value1 = $1; my $value2 = $2; print "GOT $value1 $value2\n"; }

      I completely agree yours is more readable that the OPs alternate version. But even more readable is:

      my $regexp = qr/(\d+)\s+(\d+)/; while(<$fh>) { my( $value1, $value2 ) = /$regexp/ or next; print "GOT $value1 $value2\n"; }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2021-03-07 18:53 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (122 votes). Check out past polls.