Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: giving same name to two files

by mtmcc (Hermit)
on Jul 16, 2013 at 12:33 UTC ( [id://1044578]=note: print w/replies, xml ) Need Help??


in reply to giving same name to two files

are you looking for something like this?

#!/usr/bin/perl use strict; use warnings; print STDERR "Enter filename: "; my $fileName = <STDIN>; chomp $fileName; die "$fileName not found.\n\n" unless -e $fileName; my @name = split ('\.', $fileName); $name[$#name] = "txt"; my $newFileName = join(".", @name); print STDERR "\nOLD: $fileName\nNEW: $newFileName\n\n"; open (IN, "<", $fileName); open (OUT, ">", $newFileName); while (<IN>) { #do stuff... #print OUT "output"; }

-Michael

Replies are listed 'Best First'.
Re^2: giving same name to two files
by Anonymous Monk on Jul 16, 2013 at 12:39 UTC
    ya I think this will work out , Thanks . I will try it and if I en counter any issue , I will post it
Re^2: giving same name to two files
by Anonymous Monk on Jul 17, 2013 at 10:07 UTC
    hey

    your code is working when the user inputs the name correctly the first time , but if he gives a wrong name the first time , so i am doing this

    while (!-e $filename) { print "-*-*--*-*--*-*--*-*--*-*--*-*--*-*--*-*--*-*--*-*--*- +*--*-*--*-*--*-*--*-*--*-*--*-*--*-*-\n"; print STDERR "\n$filename not found. \n Ensure that you have + given the correct file name\n and the file exists in the working dir +ectory \n."; print "Re-enter filename, or q to quit: "; $filename = <STDIN>; chomp $filename; # my @filenam = split ('\.', $filename); exit() if $filename eq "q"; }

    the line commented out by me is what I did so everytime user inputs the file name it should get updated but its not happening , can you tell me why

      Is this what you mean?

      #!/usr/bin/perl use strict; use warnings; my $ending = "txt"; print STDERR "Enter filename: "; my $fileName = <STDIN>; chomp $fileName; while (!-e "$fileName") { print STDERR "$fileName not found. Try again, or quit (q): "; $fileName = <STDIN>; chomp $fileName; exit () if $fileName eq "q"; } my @name = split ('\.', $fileName); $name[$#name] = $ending if @name >= 2; push(@name, $ending) if @name == 1; my $newFileName = join(".", @name); print STDERR "\nOLD: $fileName\nNEW: $newFileName\n\n"; open (IN, "<", $fileName); open (OUT, ">", $newFileName); while (<IN>) { #do stuff... #print OUT "output"; }

      -Michael

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1044578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-24 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found