Appending the required string between the variable(concatenation) ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijayarl
    New Member
    • Sep 2008
    • 65

    Appending the required string between the variable(concatenation) ?

    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.

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

    any help or suggestions will great full for me.

    Thanks,
    Vijayarl
  • scruff6119
    New Member
    • Oct 2008
    • 5

    #2
    You can use the period operator like this:

    $str = $str1 . "LAVG" . $str;

    Comment

    • vijayarl
      New Member
      • Sep 2008
      • 65

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

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

      Regards,
      Vijayarl

      Comment

      • vijayarl
        New Member
        • Sep 2008
        • 65

        #4
        hi scruff6119,

        my guess was right... am getting output like this
        Code:
        prstat-Ls-LAVG-prstat-Ls-20080118-1800.xls
        i instead want the file name to look like this:
        Code:
        prstat-Ls-LAVG-20080118-1800.xls
        thanks for your reply

        any help or idea ???

        Regards,
        Vijayarl

        Comment

        • vijayarl
          New Member
          • Sep 2008
          • 65

          #5
          hi there,

          i got it...it was simply..
          Code:
          foreach my $f (@file){
          $str = substr($f,32,39);
          $str1 = substr($str,0,9);
          $str2 = substr($str,10,27);
          $str = $str1 . "-LAVG-" . $str2;
          $loadavg ="c:\\Performance_svap\\OUTPUT_FILES\\$str";
          }
          Thanks you...

          Regards,
          Vijayarl

          Comment

          • scruff6119
            New Member
            • Oct 2008
            • 5

            #6
            Could do it by regex too:

            $str =~ s/(.*?Ls-)(\d{8}.*?\.xls )/$1LANG-$2/

            Comment

            • vijayarl
              New Member
              • Sep 2008
              • 65

              #7
              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

              Comment

              Working...