Steve Bertrand wrote:
John W. Krahn wrote:
Steve Bertrand wrote:
[ snip ]
In the third line of the following code, I want the replacement pattern
to be (whitespace * $_[1]). I'll deal with the undef issue after I
figure out how to use {$num} on essentially nothing. I haven't been able
to make it work with the likes of s/blah/\s{3}/g; etc:
sub to_word ($;$) {
return unless defined $_[0] && $_[0] ne '';
my $word = $_[0] =~ s/[^\w\s]/\s{3}/g;
return unless defined $word;
}
In the substitution operator, the left part is a regular expression and
the right part is just a string, so the code above is replacing a single
character matching '[^\w\s]' with the string 's{3}' (the backslash is
interpolated away.)
You cannot put "whitespace" in the string half because whitespace is a
regular expression character class and it has no meaning in a string
context.
If you want to replace a pattern with the value of $_[1] space
characters then you could do this:
s/blah/ ' ' x $_[1] /eg;
The /e option treats the string on the right as Perl code and evaluates
it and returns the result.
Wow. Thanks for such detailed information as to why it was broken, what
needed to be changed, and most importantly why it needed to be changed.
I wasted about two hours of my coding time trying to come up with a
special JAPH, but I just haven't been able to make map() do what I think
it should do ;)
What did you think it should do? What did you want it to do?
Anyway, for the archive, using a patched version of Data::Type,:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Types qw(:word);
my $string = "hello%world!";
print "orig: $string\n";
to_word($string, 1);
print "new: $string\n";
__END__
pearl% ../data-types/datatypes.pl
orig: hello%world!
new: hello world
...and the patch (wohoo!, my first real attempt at supplying a patch to
a real person!) Please, please give me feedback on the following patch
before I submit it. Does it look ok? Did I update everything
accordingly? Is my "diff" command acceptable for general use?:
http://ipv6canada.com/data-type.patch
+sub is_word ($) {
+ return unless defined $_[0] && $_[0] ne '';
+ return if $_[0] =~ /[\w\s]+/;
+ return 1;
+}
The other is_* functions in that module use anchors with their regular
expressions so you probably should as well:
+ return if $_[0] =~ /^[\w\s]+$/;
Steve
ps. I've errored out on function calls to the module, and haven't had
time to look into whether this module responds with anything other than
a bool. Just look at the patch, and let me know if I'm on my way, capiche?
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/