Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

is this correct

by snowrider (Pilgrim)
on Feb 02, 2001 at 22:03 UTC ( [id://56044]=perlquestion: print w/replies, xml ) Need Help??

snowrider has asked for the wisdom of the Perl Monks concerning the following question:

fellow monks i think i just found a little mistake i am currently doing the exercise #1 in the tutorials under string matching & regular expressions the original code looks like this
1: #!/usr/bin/perl 2: #this programs prompts a user for a filename and then 3: #replaces all tabs in that file with 3 spaces. 4: print "What file do you want to \"detabify?\" "; 5: $file=<>; 6: chomp $file; 7: $/=undef; #changes the input record separator so it'll +read the entire file in at once 8: open FILE, "<$file"; 9: $text=<FILE>; #reads the entire file in 10: close FILE; 11: $text=~s/\t/ /g; #replaces tabs 12: open FILE, ">$file"; 13: print $text; #write changes to file 14: close FILE;
shouldnt line 12 open $text instead of $file? snowrider

Replies are listed 'Best First'.
Re: is this correct
by kschwab (Vicar) on Feb 02, 2001 at 22:28 UTC
    It is broken, but not at line 12.

    Line 13 should read:

    print FILE $text;
    As it stands now, it reads in the file, prints the untabbified text to STDOUT, and truncates the supplied user-specified file.
      And assuming you actually run the program, you'll see that problem firsthand and quickly fix that line so it acts correctly.

      (Note: this is simply a friendly suggestion that some attempt be made to understand the program/problem when you post.)

      ALL HAIL BRAK!!!

        PsychoSpunk this is my second day tring to program in perl since the program wasnt running correctly i did try to fix the problem but since i dont have any experience in perl thats alittle hard without the help of you guys(fellow monks). thanks for your help snowrider
      You could also do this...
      select(FILE); print $text; print "blah blah blah\n";
      And so on... all prints would go to FILE

      Update:
      Snowrider - This is assuming you have multiple files open and are printing to them as you go... and as I know the basic tasks you will have to perform as you come up to speed with Perl... this might be useful to you.

      i.e.,

      open DRILL_FILE, ">$drill_file"; open ROUT_FILE, ">$rout_file"; select(DRILL_FILE); print "stuff...\n"; select(ROUT_FILE); print "other stuff...\n";

      Prost,
      Moe

Re: is this correct
by davorg (Chancellor) on Feb 02, 2001 at 22:10 UTC

    No, it looks right to me. $file contains the name of the file that you are processing and $text contains the contents of that file.</p. --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: is this correct
by Gloom (Monk) on Feb 03, 2001 at 00:10 UTC
    What's append if the file take 150mb of disk space ?

    Maybe you may use something like this :

    # perl -e 'while(<>){s/\t/ /g;print}' < file > file.tmp;\ > mv file.tmp file;

    Update : Thank's arhuman for this comment. This option is really usefull.

    Hope this helps

      In case like this (a while loop around one or more files) the perl -ne is usually a good candidate...

      Something like :
      perl -ne 's/\t/ /g;print' file > file.tmp; mv file.tmp file;
        To abstract it a little more

        perl -i.back -pe 's/\t/    /g' file

        The -i means copy original file to file.back and put the output of program in file. The -p is like -n but does while(<>){YOUR_CODE; print;} instead.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found