Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Executing stored substitutions...

Reply
Thread Tools

Executing stored substitutions...

 
 
gibbering poster
Guest
Posts: n/a
 
      01-22-2004
Suppose I has some coderefs in an array, and wanted to loop through them
and apply them... more specifically to a string:

my @subs = (
{ s/\s+//g }, { s/-/ /g },
);

my $string = "\tJoe-Smith ";

for (@subs) {
local *_ = $string;
do &{$_}; # I know this is horribly wrong
}

print $string; # Want this to print "Joe Smith"


Can someone please show me the way here? Sorry in advance if I missed a
perldoc.

Thanks!

PS ... Is there a way to catch the return value of those substitions
(i.e. their success or failure)?



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-22-2004
gibbering poster wrote:
> Suppose I has some coderefs in an array, and wanted to loop
> through them and apply them... more specifically to a string:
>
> my @subs = (
> { s/\s+//g }, { s/-/ /g },


You need to say that they are code refs:

sub { s/\s+//g }, sub { s/-/ /g },

> );
>
> my $string = "\tJoe-Smith ";
>
> for (@subs) {
> local *_ = $string;
> do &{$_}; # I know this is horribly wrong
> }


You may want to try this:

for my $sub (@subs) {
local $_ = $string;
do &{$sub};
$string = $_;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-22-2004
gibbering poster <(E-Mail Removed)> wrote:

> Suppose I has some coderefs in an array,


> my @subs = (
> { s/\s+//g }, { s/-/ /g },
> );



There are no coderefs in that array you know.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      01-22-2004
"gibbering poster" <(E-Mail Removed)> wrote in message news:<JwEPb.14542$(E-Mail Removed) om>...
> Suppose I has some coderefs in an array, and wanted to loop through them
> and apply them... more specifically to a string:
>
> my @subs = (
> { s/\s+//g }, { s/-/ /g },
> );


Those are (empty) hashrefs...

> my $string = "\tJoe-Smith ";
>
> for (@subs) {
> local *_ = $string;
> do &{$_}; # I know this is horribly wrong
> }
>
> print $string; # Want this to print "Joe Smith"
>
> Can someone please show me the way here? Sorry in advance if I missed a
> perldoc.
>
> Thanks!


One way to do what you had in mind:

my @subs = (
sub { $_[0] =~ s#\s+##g },
sub { $_[0] =~ s#-# #g },
);

foreach ( @subs ) {
$_->($string);
}
print "$string\n";

But really, if you want to apply *all* the substitutions on the
string(s), put all of them in one sub...

If, on the other hand, you want to *selectively* perform the
substitutions (or run whatever code you like) use a dispatch table:

my $dt = {
whitespace => sub { $_[0] =~ s#\s+##g },
dashes => sub { $_[0] =~ s#-# #g },
# other coderefs...
};

foreach my $hrkey( qw[whitespace dashes] ) {
$dt->{$hrkey}->($string);
}
print "$string\n";

The following documentation are good reading in this case:

perlreftut
perldsc
perllol

> PS ... Is there a way to catch the return value of those substitions
> (i.e. their success or failure)?


From perlop:

s/PATTERN/REPLACEMENT/egimosx

Searches a string for a pattern, and if found, replaces that pattern
with the replacement text and returns the number of substitutions
made.

HTH - keith
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing batch files stored in Jar Lionel van den Berg Java 9 11-13-2007 03:16 AM
Horrible performance when executing stored procedure staeri@gmail.com ASP .Net 2 11-08-2006 04:42 PM
Sample code for Connecting and Executing a stored procedure is SQL server using ASP.NET Peri ASP .Net 1 06-13-2005 04:56 AM
OleDbException executing reader on Access stored view Marco Frontini ASP .Net 2 01-12-2005 07:20 PM
Server Error in '/' Application - when executing stored query - novice question sean ASP .Net 0 12-02-2003 05:43 AM



Advertisments