Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Deleting lines from an array

by Anonymous Monk
on Jun 10, 2002 at 14:30 UTC ( [id://173144]=note: print w/replies, xml ) Need Help??


in reply to Code explanation needed (was: What does this mean)

Im trying to delete a $array[value] out of @array, but wehn I print to my text file it like
--------- 1|cool.gif|aaa|yes|yes 3|biggrin.gif|:D|biggie|yes 5|tongue.gif|:p|tongue|yes ---------
See the space, thats what it does when it does, I want that gone.
sub set_del_yes { $storeline = $q->param('id'); open(FILE, "info/smileset.txt"); @file = <FILE>; close(FILE); chomp @file; my @matches = grep { $file[$_] =~ /^\Q$storeline|\E/ } 0..$#file; no_match($storeline), return unless @matches; duped_ids($storeline), return if @matches > 1; undef($file[$matches[0]]); delete($file[$matches[0]]); print "Smiley set has been deleted!<br><br> <a href=\"admin.cgi?pass=$pas&do=settings_set\">Smiley Set</a>"; open FLE, '>', 'info/smileset.txt' or die $!; print FLE $_, $/ for @file; close FLE or die $!; }

Edit kudra, 2002-06-10 Fixed literal [ and friend

Title edit by tye

Replies are listed 'Best First'.
Re: Deleting lines from an array
by Ovid (Cardinal) on Jun 10, 2002 at 14:39 UTC

    Change this:

    print FLE $_, $/ for @file;

    To this:

    for ( @file ) { print FLE "$_\n" if /\S/; # must have at least one non-whitespace }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Deleting lines from an array
by Abigail-II (Bishop) on Jun 10, 2002 at 14:59 UTC
    That's because a delete $array [$indx] only changes the size of the array if you are deleting from the end. If you really want it gone, you would use splice, but splicing away lots of individual elements from a large array is costly (as each splice in general take time linear in the size of the array).

    You could change your print to:

    defined ($_) && print FLE $_, $/ for @file;
    unless you have undeleted, undefined elements in @file which you want to print. In that case, you could do something like:
    foreach (0 .. $#file) { print FLE $file [$_], $/ if exists $file [$_] }

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://173144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-16 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found