Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Deleting elements from array questions

by chuleto1 (Beadle)
on Aug 08, 2002 at 17:58 UTC ( [id://188669]=perlquestion: print w/replies, xml ) Need Help??

chuleto1 has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to have this done in an easier way?
And have the array collapse shrink?
(i.e. if the array size is 10 when this happens '@array[count] = "";' have the array size shrink to size 9)?

$count = 0; $item = "match"; foreach $element (@array) { if($element eq $item) { @array[count] = ""; } count++; }

Edit by myocom: Added code tags

Replies are listed 'Best First'.
Re: Deleting elements from array questions
by thelenm (Vicar) on Aug 08, 2002 at 18:06 UTC
    If I understand what you're trying to do, you could just use grep and assign the new array to the original one:
    $item = "match"; @array = grep {$_ ne $item} @array;

    -- Mike

    --
    just,my${.02}

Re: Deleting elements from array questions
by DamnDirtyApe (Curate) on Aug 08, 2002 at 18:08 UTC

    grep can do what you're trying to accomplish. Consider the following:

    #! /usr/bin/perl use strict ; use warnings ; $|++ ; my @array = qw( foo bar match zoot zoot match match match ) ; my $item = 'match' ; print "Original size: " . @array . "\n" ; print "Original contents: " . join( " : ", @array ) . "\n\n" ; my @new_array = grep { ! /^$item$/ } @array ; print "New size: " . @new_array . "\n" ; print "New contents: " . join( " : ", @new_array ) . "\n\n" ; __END__

    Results:

    Original size: 8 Original contents: foo : bar : match : zoot : zoot : match : match : m +atch New size: 4 New contents: foo : bar : zoot : zoot
    Update: Whoops, was doing the exact opposite of the poster's question. All better now.
    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Deleting elements from array questions
by Zaxo (Archbishop) on Aug 08, 2002 at 18:09 UTC

    If you want to remove the elements from the array entirely, grep is the way:

    @array = grep { $_ ne $item } @array;
    That eliminates fencepost errors and leaves no empty slots in the array. Your code should work, but it does not remove the array slot.

    Updade: Corrected pasteo.

    After Compline,
    Zaxo

      If you want to remove the elements from the array entirely, grep is the way [Emphasis added]

      Well, grep is one way (and probably the one that will work best for the original question.) But splice is also a perfectly good way to remove elements from an array.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Deleting elements from array questions
by robobunny (Friar) on Aug 08, 2002 at 18:36 UTC
    the subject of "deleting elements from array questions" comes up pretty often, so i decided to write a subroutine that would handle the job once and for all. note that it will only remove elements from array questions, not array statements or other types of questions.
    $ques = "how do i make an array spin around?"; $element = 4; print delete_element($element, $ques), "\n"; sub delete_element { my ($element, $question) = @_; if($question =~ /array.*\?$/i) { @elements = split(' ', $question); $elements[$element] = ''; return join(' ', @elements); } else { print "That is not an array question!\n"; return $question; } } output: how do i make array spin around?
Re: Deleting elements from array questions
by aufrank (Pilgrim) on Aug 08, 2002 at 18:31 UTC

    the problem is that there are still 10 elements if you just undef the match(es):

    my @array = 1..10; print "@array\n"; my $item = 3; foreach my $element (@array) { undef $element if ($element eq $item); } print "@array\n"; print $#array + 1;

    see, the array doesn't actually shrink. when you do a print $#array + 1 you still get a 10 (assuming there were 10 elements to start like there were here).

    to get rid of the phantom element(s), you could use splice like this:

    my @array = 1..10; print "@array\n"; my $item = 3; my @empties; my $count; foreach my $element (@array) { if ($element eq $item) { undef $element; push @empties, $count; } $count ++; } foreach (@empties) { splice (@array, $_, 1); } print "@array\n"; print $#array + 1;

    hth,
    --au

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found