
[Fwd: How can I get the last element on each line in a text file]
Hi:
Making a temporary copy as suggested in the perlfaq5 to write/overwrite the
destination value in the
same text file did not help and hence I am trying to push the modified last
element back to the file as below, but the input text file does not get updated
with the modified information
How can I write back to the same text file(array) the modification I made to it?
use File::Basename;
$file=$ARGV[0];
open(INFO,"<$file") || die "Cannot open $file: $!";
{
#$/ = "";
next if $a =~ /^#/;
($first,$last) = split(/[|]/,$a);
$src = basename($first);
$dir = dirname($last);
$tail=$parts[$#parts];
$tail=$src;
$last = $dir . "/" . $tail;
the file.
}
close <INFO>;
exit;
Thank You
Nishi.
Quote:
> Also, for each record in the text file, after modifying the destination part
> of each record, alongwith
> printing out the modified destination value , can I also write/overwrite the
> destination value in the
> same text file?
> Thank You
> Nishi
> > > > My text file contains n number of lines like:
> > > > c:/winnt/a.txt
> > > > c:/abc/c.txt
> > > > c:/def/def.pl
> > > ...
> > > > My perl script reads the text file into an array.
> > > > How can I extract just the last element on each line and do some
> > > > processing on it? e.g. in the above example, i would like to extract
> > > > a.txt from first line, c.txt from the second line and def.pl from the
> > > next unless m|^.*/(.+)$|;
> > or:
> > next unless m|/([^/]+)$|;
> > > doYourProcessing( $1 );
> > > # or just use $1, or assign $1 to something...
> > > }
> > > prints:
> > > GOT: a.txt
> > > GOT: c.txt
> > > GOT: def.pl
> > > using your examples
> > > P
> > --
> > Michael Budash
> > Michael Budash Consulting