Brian H. Oak <> wrote: > Mathieu, > > Thank you, you gave me just the clue I needed! BTW, the parameter > thing is required or else it returns a blank string. Here's what > works: > > ############################################################ > # Position and character would normally be determined # by user > input, but are hard-coded here for brevity > my $position = 2; # Is always a number, 1-4 > my $character = "~"; # Can be any "special" char > > # Build substitution for later use > my $substitute; > if ( $position == 1 ) { # s/^(.*)$/~$1/ > $substitute = sub { $_[0] =~ s/^(.*)$/$character$1/; return $_[0] }; } > elsif ( $position == 2 ) { # s/^(.*)(\.[^.]+)$/$1~$2/ > $substitute = sub { $_[0] =~ s/^(.*)(\.[^.]+)$/$1$character$2/; > return $_[0] }; } > elsif ( $position == 3 ) { # s/^(.*\.)([^.]+)$/$1~$2/ > $substitute = sub { $_[0] =~ s/^(.*\.)([^.]+)$/$1$character$2/; > return $_[0] }; } > elsif ( $position == 4 ) { # s/^(.*)$/$1~/ > $substitute = sub { $_[0] =~ s/^(.*)$/$1$character/; return $_[0] }; > } else { die "Invalid position: $position\n"; } > > # Loop thousands of times > for ( 0..99_000 ) { > # Filename would be different every time > my $filename = "filename.plus.ext"; > #$filename = $substitute->( $filename ); > $substitute->( $filename ); > print "$filename\n"; > } > ############################################################
Actually you don't need the return. In a subroutine the elements of @_ are aliases to the original parameters, so modifying $_[0] modifies (or tries to) the original parameter. See 'perldoc perlsub'. If you want to use a return value (which would be my personal preference for this sort of thing), you should localise the parameter before changing it in the sub (i.e. "my"), and assign the return value where you call the sub. Also, that "if" statement screams "array" to me. Plus, you don't need a regexp matching operation to simply add a single character to the start or end of a string. So, I think your code could be a bit simpler... use strict; use warnings; my $character = '~'; my $position = 4; my @transforms = (sub { $_[0] = "${character}$_[0]" }, sub { $_[0] =~ s/(\.[^.]+)$/${character}$1/; }, sub { $_[0] =~ s/([^.]+)$/${character}$1/; }, sub { $_[0] = "$_[0]${character}" } ); my $sub = $transforms[$position - 1]; defined $sub or die "Invalid position: $position\n"; for (0..10) { my $filename = "filename.plus.ext"; $sub->($filename); print "$filename\n"; } (Note the use of "use strict; use warnings;". I strongly recommend them both.) HTH -- Brian Raven ========================================= Atos Euronext Market Solutions Disclaimer ========================================= The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful. If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of Atos Euronext Market Solutions. Atos Euronext Market Solutions Limited - Registered in England & Wales with registration no. 3962327. Registered office address at 25 Bank Street London E14 5NQ United Kingdom. Atos Euronext Market Solutions SAS - Registered in France with registration no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 75009 Paris France. L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bien vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre systeme. Le contenu de ce message electronique ne represente pas necessairement la position ou le point de vue d'Atos Euronext Market Solutions. Atos Euronext Market Solutions Limited Société de droit anglais, enregistrée au Royaume Uni sous le numéro 3962327, dont le siège social se situe 25 Bank Street E14 5NQ Londres Royaume Uni. Atos Euronext Market Solutions SAS, société par actions simplifiée, enregistré au registre dui commerce et des sociétés sous le numéro 425 100 294 RCS Paris et dont le siège social se situe 6/8 Boulevard Haussmann 75009 Paris France. ========================================= _______________________________________________ ActivePerl mailing list ActivePerl@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs