CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [ask] Pattern substitution issue. : Edit Log



KevinR
Veteran


Jun 17, 2009, 10:04 AM


Views: 5784
Re: [ask] Pattern substitution issue.

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).


Code
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)


Edit Log:
Post edited by KevinR (Veteran) on Jun 17, 2009, 10:05 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives