Solved

m/ and s/

Posted on 2004-10-28
224 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
Question by:komlaaa
    7 Comments
     
    LVL 84

    Expert Comment

    by:ozo
    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:
    #!/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
    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
     
    LVL 5

    Expert Comment

    by:ITcrow
    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
    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
    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
    Isn't it called Windoze, hmm... it's been a while may be I should go check M$ again.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    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…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now