CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
splice problem

 



randor
User

Apr 23, 2001, 5:50 AM

Post #1 of 2 (372 views)
splice problem Can't Post

hello,

i am writing a program and i have come to a point where i need to be able to remove 1 item from an array, hence the splice function, but it is not working like it should, so i am hoping someone can explain to me what i could be doing wrong..

the problem i am having is this:

&no_cart unless (open CART_ID, "+<$svrloc/treasures_cart/$from/$me_here.dat"); ##this is the file to be opened
&oops('FLOCK1') unless (flock(CART_ID, LOCK_EX));
@all_of_it = <CART_ID>;##this is where i get all of the data
if (@all_of_it != '') {##as long as there is infact info in the file
$form{'remove_item'}--;##this is a number, it is used to determine which line of info is to be pulled, example :3 means i want the 3rd line pulled from the file (i am aware that it starts at 0, and have accounted for that
chomp $form{'remove_item'};
splice(@all_of_it, $form{'remove_item'}, 1);##remove the selected number
seek(CART_ID, 0, 0);##return to the top of the file
foreach $gr (@all_of_it) {
chomp $gr;
print CART_ID "$gr\n"; ##print to the file each of the remaining lines
print "$gr
";##this is printing to the screen for testing purposes


ok, when i run this, it prints to the screen correct, but when i look at the file it doesnt remove the spliced item, and infact adds half of the line it was suppose to remove to the list.

this is what is in the file originally:

item1%%item2|this stuff|here%%my lione
item3%%item2|here is the stuff%% here
test2%%item2|here|not%%i am


is there anything i am doing wrong with the splice??

thank you for any help you can give:
Randor




BigRich
Novice

Apr 25, 2001, 3:18 AM

Post #2 of 2 (349 views)
Re: splice problem [In reply to] Can't Post

There are a number of problems. First, I wouldn't use splice(). Next, you open the file and lock it before testing if there is anything in it. Then you slurp all of the contents in at once.

Try this:


Code
my $file = "$svrloc/treasures_cart/$from/$me_here.dat"; 

my @updated; # file content array

if (-e $file && -s $file) { # if it exists and contains data

chomp($form{'remove_item'}); #is there a newline after the number?

open(FILE, "+<$file") || &no_cart;
flock(FILE, LOCK_EX) || &oops('FLOCK1');
while (<FILE>) {
chomp;
# unless input line number equals $form{'remove_item'}
# put the line into the @updated array
unless ($. == $form{'remove_item'}) {
push @updated, $_;
}
}

seek(FILE,0,0);
truncate(FILE,0);

foreach (@updated) {
print FILE $_."\n";
}
close(FILE);
} #end if
else {

# the file does not exist or it's empty
print "file does not exist or is empty."

}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives