
KevinR
Veteran

Jun 17, 2009, 10:04 AM
Post #2 of 10
(13883 views)
|
Re: [ask] Pattern substitution issue.
[In reply to]
|
Can't Post
|
|
Your code is just wrong for a variety of reasons. Excuse me if I don;t take the time to explain the problems. What you want to do if we stick we the same basic concept, is use a hash instead of trying to cobble together dynamic variable names , which is possible but is a very bad idea 999.9999 times out of a hundred. I don't recommend this but I am just trying to stick to your general concept. Use a hash to store the patterns you want to replace in the string with their associated value (%words in the code below).
my $f="20090210_LUXECB_ALL_LUXECB_AUTO_VAR.xls" ; my $delim = "_" ; my $n = 1; my %words; my @f = split(/$delim/,$f); for (@f) { $words{$n++} = $_; } print " @file_name_words \n" ; my $dest_file1 = '1_3_5abc'; for my $i (1,3,5){ print " Value is $f[$i-1] replace with $words{$i}\n"; $dest_file1 =~ s/$i/$words{$i}/; print " dest file is $dest_file1\n"; } I assume you are trying to do it this way because you are either a student learning perl or just need to use perl but never learned it and now are trying to hurry some code together using logic you might apply using a programming language you might know already but which does not translate well to perl. -------------------------------------------------
(This post was edited by KevinR on Jun 17, 2009, 10:05 AM)
|