[rochester-pm-list] foreach question...

Justin C. Sherrill webmaster at rochester.rr.com
Wed Feb 9 14:02:31 CST 2000


> 	If I use a "foreach" type of array, is it possible to open
> a text file,
> separate each line into separate variables and have a script edit
> or delete
> just that line?  Or is the only way to do it to just open the file, and
> append the differences?  (which I've already got done, I'm just trying to
> make this easier :)  )  Thanks in advance!

There's a recipe in the Perl Cookbook that details treating lines of a text
files as array items...  Recipe 14.7

use DB_File;

tie(@array, "DB_File", "/tmp/textfile", O_RDWR|O_CREAT, 0666, $DB_RECNO)
	or die "cannot open file 'text' : $!";

$array[4] = "A new line written in.";

untie @array;

You can push, pop, shift, etc. in an object style ( $x->push($list1,
$list2) )if you capture the object returned from the tie.  $DB_RECNO is the
magic trick.  There's a much longer example in the Perl Cookbook, which is
just about the mostest valuablest book ever for Perl stuff - buy it if at
all possible.

Justin C. Sherrill
Rochester Road Runner Webmaster
http://www.rochester.rr.com/
"Think slow, type fats"




More information about the Rochester-pm mailing list