Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

giving same name to two files

by Anonymous Monk
on Jul 16, 2013 at 11:57 UTC ( #1044570=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey monks

I have a script in which I am converting an excel file to .txt file . What I have done is that I ask the name of excel file from user assign it to a file handler extract the info close the file . I edit it and ask the name of the .txt file and print on it . but i want that without asking the user can I assign the same name that of excel file to the .txt file

Note that I have to close the file handle . before starting to write on .txt file . if yes then how

Comment on giving same name to two files
Re: giving same name to two files
by daxim (Chaplain) on Jul 16, 2013 at 12:13 UTC
    File::Basename

    use File::Basename qw(fileparse); my $basename = fileparse 'foo/table.xls', qr/\.[^.]*/; __END__ table
Re: giving same name to two files
by Anonymous Monk on Jul 16, 2013 at 12:16 UTC

    You can give the same name as long as the extension is different i believe , if this is what you mean.

      ya extension is different , but since i took an input from user and assigned it to a file handler which is closed so how can I give that name to the text file
Re: giving same name to two files
by mtmcc (Hermit) on Jul 16, 2013 at 12:33 UTC
    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
      ya I think this will work out , Thanks . I will try it and if I en counter any issue , I will post it
      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
Node Status?
node history
Node Type: perlquestion [id://1044570]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-07-24 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls