in reply to Re^2: Consumes memory then crashs
in thread Consumes memory then crashs
There doesn't seem any good reason to build up your regex a bit at a time in separate variables; then concatenate those bits into another variable; and then interpolate that into a regex:
$re1='(gain)'; $re2='(:)'; $re3='(Overall)'; $re4='(:)'; $re5='(\\d+)'; $re6='(:)'; $re7='(\\d+)'; $re=$re1.$re2.$re3.$re4.$re5.$re6.$re7; if ($lookup =~ m/$re/isg) {
And even less reason to repeat the same 3-stage exercise over and over again each time around a loop. Especially as it means that the regex engine needs to recompile the combined regex every time around that loop even though nothing changes.
And why capture 7 different bits of the string you are matching against:
$re = '(gain)(:)(Overall)(:)(\\d+)(:)(\\d+)'; ... "$name $7\n"
when 5 of them are constants; and you are only using one of them?
Equally, the is nothing to be gained from assigning a constant string to a variable and then interpolating it into a regex:
$reg1='(ERROR)'; ... elsif ($lookup =~ m/$reg1/isg)
Cleaning up those; and a few other things up; adding strict and -w; and moving the regexing into a subroutine (to make later threading easier), I get:
#! perl -slw use strict; use LWP::Simple; sub lookup { my( $hf, $name ) = @_; my $lookup = get( "http://rscript.org/lookup.php?type=track&time=62899200&user=$ +name&skill=all" ); print "Looking up $name...\n"; if( $lookup =~ m/gain:Overall:\d+:(\d+)/isg ) { print { $fh } "$name $7\n"; } elsif( $lookup =~ m/(ERROR)/isg ) { print { $fh } "$name doesn't exist \n" } else{ print { $fh } "$name 0\n"; } } my $names = 'zezima'; open( LOOKUP, '>>rstlookup.txt' ) or die $!; while( my( $name ) = $names =~ m/([a-z0-9_]+)/isg ) { lookup( \*LOOKUP, $name ); } close( LOOKUP );
Which will probably not run much more quickly as you are IO-bound, but (I hope you'll agree) is easier to read and will at least consume less cpu.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Consumes memory then crashs
by allhellno (Novice) on Mar 24, 2012 at 15:25 UTC | |
by BrowserUk (Patriarch) on Mar 24, 2012 at 15:40 UTC | |
by mbethke (Hermit) on Mar 24, 2012 at 19:35 UTC | |
by davido (Cardinal) on Mar 24, 2012 at 21:20 UTC | |
by BrowserUk (Patriarch) on Mar 24, 2012 at 23:12 UTC | |
by zwon (Abbot) on Mar 25, 2012 at 05:34 UTC | |
| |
by mbethke (Hermit) on Mar 25, 2012 at 07:24 UTC | |
| |
by allhellno (Novice) on Mar 24, 2012 at 16:00 UTC | |
by BrowserUk (Patriarch) on Mar 24, 2012 at 23:17 UTC | |
by Corion (Patriarch) on Mar 24, 2012 at 15:27 UTC |