in reply to read from a file and write into the same file

Hi Anonymous monk

In my view you want to read all the file and afterwards append to a file then you can do is create a two file handleing pointers.

First thing is to read the whole content of a file to a memory or simply display it out.

Afterwards do the appending work.

Here is the example for the sorting file contents that might help

#!/usr/bin/perl -w if (@ARGV < 1 ) { print "$0 <file>\n"; exit 1 } $tmp_file=$ARGV[0]; open(TMP_INPUT_FILE, "<$tmp_file"); # open for input my(@lines) = <TMP_INPUT_FILE>; # read file into list close(TMP_INPUT_FILE); open(TMP_OUTPUT_FILE, ">$tmp_file"); @lines = sort(@lines); # sort the list my($line); foreach $line (@lines) # loop thru list { print TMP_OUTPUT_FILE "$line"; # print in sort order } close(TMP_OUTPUT_FILE);


In the above example filename is supplied at the command line.

Replies are listed 'Best First'.
Re^2: read from a file and write into the same file
by CountZero (Bishop) on Mar 03, 2008 at 07:09 UTC
    It is considered better to use <code> ... </code> tags rather than <pre> ... </pre> around code. This will allow easy downloading of the code and assures sensible wrapping of overly long line of the code. Also I have a code colorizer which needs <code> ... </code> tags to know what to colorize.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks for your comment and updated it