[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

m/ and s/

i would like to take each element in @commands replace xxx in $fileName, and store that new value in $newFileName.
Then use  $newFileName to create a new empty file in the current directory.
example: komlaaa.1.regularFile.xxx.input -----------> komlaaa.1.regularFile.td.input  -----means xxx is td
               komlaaa.1.regularFile.xxx.input -----------> komlaaa.1.regularFile.tdc.input  -----means xxx is tdc
and so on...


===================== I START IT AND THEN I STOCK ==========================
#!/usr/local/bin/perl

$fileName = komlaaa.1.regularFile.xxx.input;
@commands =  qw/ l t d c lt lc td tc dc ltd ldc tdc ltdc /;
foreach $com (@command){
    ($file=$_) =~ m/xxx/$fileName/
0
komlaaa
Asked:
komlaaa
2 Solutions
 
ozoCommented:
use strict;
my $fileName = 'komlaaa.1.regularFile.xxx.input';
my @commands =  qw/ l t d c lt lc td tc dc ltd ldc tdc ltdc /;
foreach my $com (@commands){
    (my $file=$fileName) =~ s/xxx/$com/;
    print $file,"\n";
}
0
 
rj2Commented:
#!/usr/local/bin/perl

$fileName = 'komlaaa.1.regularFile.xxx.input';
@commands =  qw/ l t d c lt lc td tc dc ltd ldc tdc ltdc /;
foreach $com (@commands){
    $newFileName=$fileName;
    $newFileName=~s/xxx/$com/;
    print "Creating file $newFileName\n";
    open(FILE,">$newFileName") || die("Can not open file $newFileName because $!");
    print FILE "";
    close(FILE);
}    
0
 
manav_mathurCommented:
komlaa

#!/usr/local/bin/perl
use strict;
use warnings;
my $fileName = "komlaaa.1.regularFile.xxx.input" ;
my @commands =  qw/ l t d c lt lc td tc dc ltd ldc tdc ltdc /;
my $com ;
foreach $com (@commands){
      my $tempfile = $fileName ;
                $tempfile =~ s/xxx/$com/g ;
      system ("touch $tempfile") ;
}

Should work for you

Cheers
Manav
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
ITcrowCommented:
Manav, touch is not going to work for windoz. I instead used open for write and close.

======================================================
$prefix = "komlaaa.1.regularFile";
$suffix  = "input";
my @commands =  qw/ l t d c lt lc td tc dc ltd ldc tdc ltdc /;
open(F, ">$prefix.$_.$suffix") && close(F) foreach( @commands );
======================================================
0
 
ITcrowCommented:
Manav, Just noticed original poster has used /usr/local/bin/perl which gives an indication that question poster may be on *nix. So your solution works as good. Mine is a bit portable, considering that it works the same way on *nix and windoze.
0
 
manav_mathurCommented:
ITcrow,
Actually I did use touch because of the #!/usr/local/bin/perl :) (Nice way you spell "windoze")
Where's the author of the question??

Ozo, you forgot to open the file.............;)

Manav
0
 
ITcrowCommented:
Isn't it called Windoze, hmm... it's been a while may be I should go check M$ again.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now