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:
Pattern substitution issue.

 



ask
Novice

Jun 17, 2009, 9:26 AM

Post #1 of 10 (5792 views)
Pattern substitution issue. Can't Post

Hi, I need help in parsing a string containing "$1,$2,$3 " ..

Here is what i am doing. In the below, i am trying to replace destination file "$1_$3_$5abc" with the first field in the file name $f using the separator as _" . So

$1 should be replaced with 20090210

$3 should be replaced with ALL

$5 should be replaced with AUTO

But the out put is coming NULL. Please help me..



Script:

my $f="20090210_LUXECB_ALL_LUXECB_AUTO_VAR.xls" ;

my $delim ="_" ;

my @file_name_words= split(/$delim/,$f) ;

print " @file_name_words \n" ;

my $size= $#file_name_words ;

my $dest_file1="$1_$3_$5abc";


for ($i=0; $i<=$size; $i++ )
{

my $match_pat ;

$pat = $i + 1 ;
$match_pat = "\$" . $pat ;

print "Match $match_pat\n";
print " Value is $file_name_words[$i] \n";

$dest_file1 =~ s/(\$match_pat)/$file_name_words[$i]/g ;

print " dest file is $dest_file1\n";

}


Output:



20090210 LUXECB ALL LUXECB AUTO VAR.xls
Match $1
Value is 20090210
dest file is __abc
Match $2
Value is LUXECB
dest file is __abc
Match $3
Value is ALL
dest file is __abc
Match $4
Value is LUXECB
dest file is __abc
Match $5
Value is AUTO
dest file is __abc
Match $6
Value is VAR.xls
dest file is __abc


KevinR
Veteran


Jun 17, 2009, 10:04 AM

Post #2 of 10 (5788 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).


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)


ask
Novice

Jun 17, 2009, 10:16 AM

Post #3 of 10 (5785 views)
Re: [KevinR] Pattern substitution issue. [In reply to] Can't Post

thanks for the quick response. This works fine. But if the file name has "1" for which if we don't want replace with the first word in the original file, this would fail.

For. e.g . if the destination file is 1_2_3abc1 then it would try to replace every "1" with first word in file name.



This is the reason i was using $1. Please let me know if there is a way to avoid this.


KevinR
Veteran


Jun 17, 2009, 10:26 AM

Post #4 of 10 (5782 views)
Re: [ask] Pattern substitution issue. [In reply to] Can't Post

No, it will not replace the 1 in abc1. You can try that in the code I posted for you. A regexp only replaces the first search pattern unless you tell it to do more search and replace for the same pattern in the same string using the "g" modifier or other looping logic.


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_5abc1';

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";
}


The final string is : 20090210_ALL_AUTOabc1

But there might be other situations where unintended replacment does occur.
-------------------------------------------------


KevinR
Veteran


Jun 17, 2009, 10:29 AM

Post #5 of 10 (5780 views)
Re: [ask] Pattern substitution issue. [In reply to] Can't Post

You can pretty much forget this:

my $dest_file1 = "$1_$3_$5abc";

the first problem is that perl will interpolate the scalar variables $1, $3 and $5 in the double-quoted string and replace them with their stored values, and since they have no values the value of $dest_file1 is:

__abc

even if you use single quotes to kill the variable interpolation:

my $dest_file1 = '$1_$3_$5abc';

It still won't work, but for other reassons.
-------------------------------------------------


ask
Novice

Jun 17, 2009, 10:30 AM

Post #6 of 10 (5779 views)
Re: [KevinR] Pattern substitution issue. [In reply to] Can't Post

Yes. I am aware of "g". I need to use this as 1 may be repeated in the file name . For.e.g. I amy have the file name as 1_2_3_1_abc1 . Here first two 1's should be repalced with the first field name. But the last one should remain. With modifier g, it would replace all 1with the value. Please advice. Is there any way can we use $ or some thing to distinguish.


KevinR
Veteran


Jun 17, 2009, 10:34 AM

Post #7 of 10 (5779 views)
Re: [ask] Pattern substitution issue. [In reply to] Can't Post

Is there some reason you can't just do something like this:


Code
my $f="20090210_LUXECB_ALL_LUXECB_AUTO_VAR.xls" ;  
my $delim = "_" ;
my $dest_file1 = join('_',(split(/$delim/,$f))[0,2,4]).'abc';
print $dest_file1;

-------------------------------------------------


KevinR
Veteran


Jun 17, 2009, 10:36 AM

Post #8 of 10 (5778 views)
Re: [ask] Pattern substitution issue. [In reply to] Can't Post


In Reply To
Yes. I am aware of "g". I need to use this as 1 may be repeated in the file name . For.e.g. I amy have the file name as 1_2_3_1_abc1 . Here first two 1's should be repalced with the first field name. But the last one should remain. With modifier g, it would replace all 1with the value. Please advice. Is there any way can we use $ or some thing to distinguish.


You can seperate the process into two steps instead of trying to do it all in the regexp. Replace what you need in 1_2_3_1 and then append abc1 back to the string after doing all the paterrn search and replace.
-------------------------------------------------


ask
Novice

Jun 17, 2009, 1:15 PM

Post #9 of 10 (5775 views)
Re: [KevinR] Pattern substitution issue. [In reply to] Can't Post

Actually this file name(i.e. 1_3_1_2abc1) will be a configurable value in database. So doing in two steps is not possible. Any way , i will be using this as a limitation to this process.



thanks for your help.


KevinR
Veteran


Jun 17, 2009, 1:17 PM

Post #10 of 10 (5774 views)
Re: [ask] Pattern substitution issue. [In reply to] Can't Post

If the suggestion I have been able to give you still will not work for you try www.perlmonks.com or www.stackoverflow.com but try and define the nature of your problem better and include all pertinent information. Maybe some one else can come up with a solution for you based upon all the information.

Good luck,
Kevin
-------------------------------------------------

 
 


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

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