Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

m/ and s/

Posted on 2004-10-28
7
Medium Priority
?
245 Views
Last Modified: 2010-03-05
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
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 12442146
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
 
LVL 10

Accepted Solution

by:
rj2 earned 800 total points
ID: 12442173
#!/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
 
LVL 16

Assisted Solution

by:manav_mathur
manav_mathur earned 400 total points
ID: 12442182
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:ITcrow
ID: 12442550
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
 
LVL 5

Expert Comment

by:ITcrow
ID: 12442563
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12442612
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
 
LVL 5

Expert Comment

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question