Hi, i'd like to have an easy way of doing IO operation in files :
my main 2 tasks are :
1. reading line from file. for this i've created the following function :
2. writing a text in specific line : in this part i'm having some difficulties to find the optimal way of implementation. i can do that by reading the whole file into list (as in the previous function), adding the requested line to that list, and overriding the original file with the revised list (see below).
my question is whether there's a better way of doing these simple commands ?
thanks
my main 2 tasks are :
1. reading line from file. for this i've created the following function :
Code:
sub read_line { my $filename = shift; my $index = shift; open FILE, $filename or die "can't open $filename"; my @text = <FILE>; return $text[$index]; }
Code:
sub write_line { my $filename = shift; my $index = shift; my $text = shift; open FILE, $filename or die "can't open $filename"; my @file = <FILE>; @file = (@file[0..($index-1)],"$text\n",@file[$index..$#file]); print @file; }
thanks
Comment