|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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<br>";##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 |
|
#2
|
|||
|
|||
|
instead of fussing with a splice, why not try reading the number of the line to remove before you open the file, and then only read the line from the file into the array if the line number != the $form{'remove_item'} value.
just a thought. |
|
#3
|
|||
|
|||
|
Seems like you should be truncating the file somewhere in there. Consider that you are 1) opening a file for read/write, 2) reading all of its content 3) seeking the start of the file, and then 4) writing less content back to the file.
I don't kow if it will solve your problem but certainly you should be clearing the file after 2 and before 4 or else you will get some leftover bits in the file.
__________________
<www.perlcircus.com> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > splice problem... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|