SPOILER Re: SPUG: scalar swap challenge

David Dyck david.dyck at fluke.com
Thu Oct 9 21:34:22 CDT 2003


On Thu, 9 Oct 2003 at 10:43 -0700, Colin Meyer <cmeyer at helvella.org> wrote:

> $a .= $b;
> $b = substr $a, 0, length($a) - length($b);
> $a = substr $a, length($b), length($a);

Good, but how about using the replacement argument to substr.

#!/usr/bin/perl
$a = 'one';
$b = 'two';

print "$a  $b\n";
$b = substr($a, 0, length $a, $b);
print "$a  $b\n";

__END__

    substr EXPR,OFFSET,LENGTH,REPLACEMENT
	Extracts a substring out of EXPR and returns it.
	 ...  specify the replacement string as the 4th
	argument.  This allows you to replace parts of the
	EXPR and return what was there before in one
	operation, just as you can with splice().



More information about the spug-list mailing list