Connecting Tech Pros Worldwide Forums | Help | Site Map

Appending the required string between the variable(concatenation) ?

Member
 
Join Date: Sep 2008
Posts: 65
#1: Oct 24 '08
Hi Everyone,

i need to add a string in between the filename.so that new file will have the
required filename.

what am doing is reading the files from the directory & extract only the filename and then pass this failename to create new file.

now what i need is instead of just passing filename, i need to add some string to this filename & then pass the filename to create a new file.

Expand|Select|Wrap|Line Numbers
  1. sub loadavg_file(){
  2. my $dir = 'C:\Performance_svap\INPUT_FILES\*.xls'; 
  3. my @file=glob("$dir"); 
  4. my @output;
  5. my $loadavg;
  6. my $str;
  7. my $str1;
  8. my @data;
  9. my $date;
  10. foreach my $f (@file){
  11.  $str = substr($f,32,39);
  12.  $str1 = substr($str,0,9);
  13.  $loadavg ="c:\\Performance_svap\\OUTPUT_FILES\\$str";
  14.  
  15. blaah blaah...
  16. ....
  17. ....
  18. }
  19. open(EX,">$loadavg") or die "Can't open open $loadavg:$!";
  20. print EX "$output[0]\t$output[1]\t$output[2]\t$output[3]\t$output[4]\n"; 
  21. close EX;
  22. }
  23.  
so, in the above script, $str variable holds the filename. now to this variable i need to append the required string in between ie,
Expand|Select|Wrap|Line Numbers
  1. $str will have = prstat-Ls-20080118-1800.xls value.
  2.  
now i need to append the string to this variable & it should look like
Expand|Select|Wrap|Line Numbers
  1. $str = prstat-Ls-LAVG-20080118-1800.xls
  2.  
i tried changing with first instance ie,
Expand|Select|Wrap|Line Numbers
  1. $str = substr($f,32,39);
  2. $str1 = substr($str,0,9);
  3. $str = "LAVG".$str;
  4. $loadavg ="c:\\Performance_svap\\OUTPUT_FILES\\$str";
  5.  
am not getting how append the string in between the file name.

any help or suggestions will great full for me.

Thanks,
Vijayarl

Newbie
 
Join Date: Oct 2008
Posts: 5
#2: Oct 24 '08

re: Appending the required string between the variable(concatenation) ?


You can use the period operator like this:

$str = $str1 . "LAVG" . $str;
Member
 
Join Date: Sep 2008
Posts: 65
#3: Oct 24 '08

re: Appending the required string between the variable(concatenation) ?


hey thanks for reply !!! i try it right away !!!

but what i feel is what you have suggested will give this i guess :
Expand|Select|Wrap|Line Numbers
  1. $str1 = substr($str,0,9); # holds the value =prstat-Ls
  2.  
so i guess i will get this ouput from your logic
Expand|Select|Wrap|Line Numbers
  1. $str=prstat-Ls-LAVG-prstat-Ls-20080118-1800.xls
  2.  
anyway i will cm back !!!

Regards,
Vijayarl
Member
 
Join Date: Sep 2008
Posts: 65
#4: Oct 24 '08

re: Appending the required string between the variable(concatenation) ?


hi scruff6119,

my guess was right... am getting output like this
Expand|Select|Wrap|Line Numbers
  1. prstat-Ls-LAVG-prstat-Ls-20080118-1800.xls
  2.  
i instead want the file name to look like this:
Expand|Select|Wrap|Line Numbers
  1. prstat-Ls-LAVG-20080118-1800.xls
  2.  
thanks for your reply

any help or idea ???

Regards,
Vijayarl
Member
 
Join Date: Sep 2008
Posts: 65
#5: Oct 24 '08

re: Appending the required string between the variable(concatenation) ?


hi there,

i got it...it was simply..
Expand|Select|Wrap|Line Numbers
  1. foreach my $f (@file){
  2. $str = substr($f,32,39);
  3. $str1 = substr($str,0,9);
  4. $str2 = substr($str,10,27);
  5. $str = $str1 . "-LAVG-" . $str2;
  6. $loadavg ="c:\\Performance_svap\\OUTPUT_FILES\\$str";
  7. }
  8.  
Thanks you...

Regards,
Vijayarl
Newbie
 
Join Date: Oct 2008
Posts: 5
#6: Oct 24 '08

re: Appending the required string between the variable(concatenation) ?


Could do it by regex too:

$str =~ s/(.*?Ls-)(\d{8}.*?\.xls)/$1LANG-$2/
Member
 
Join Date: Sep 2008
Posts: 65
#7: Oct 24 '08

re: Appending the required string between the variable(concatenation) ?


hey thanks scruff6119 !!!!

Even this worked...As am not so good at regex.. i didn't thought of this idea..

anyway's thanks for you help :-)

Regards,
Vijayarl
Reply