Welcome to the Monastery PerlMonks

### TIMTOWTDI Challenge: Zero'ing Out An Array

by Limbic~Region (Chancellor)
 on Sep 27, 2011 at 15:17 UTC Need Help??
Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
I was just asked by a co-worker what the best way to zero out an array was. After showing him 3 ways to do it, he walked off satisfied. Then I thought - how many ways can you do it? Here are the straight forward ones I came up with after a moment's thought.
```@array = ();

@array = undef;   # Doesn't actually zero out the array

undef @array;

\$#array = -1;

splice(@array,0);

pop @array while @array;

shift @array while @array;

Can you think of any others - bonus virtual points for creativity.

Cheers - L~R

Replies are listed 'Best First'.
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by moritz (Cardinal) on Sep 27, 2011 at 15:24 UTC
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by wfsp (Abbot) on Sep 27, 2011 at 16:30 UTC
For a one element array
```\$array[0] = 0;
For a two element array
```\$array[0] = 0;
\$array[1] = 0;
For a three element array... (left as an exercise for the reader)
wfsp,
While your interpretation of "zero'ing out" is valid, based on the examples provided is not a correct one. Thanks for the laugh though ;-)

Cheers - L~R

Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by Not_a_Number (Prior) on Sep 27, 2011 at 16:36 UTC
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by johngg (Canon) on Sep 27, 2011 at 16:38 UTC
```knoppix@Microknoppix:~\$ perl -MData::Dumper -E '
> @arr = qw{ a b c };
> say Data::Dumper->Dumpxs( [ \ @arr ], [ qw{ *arr } ] );
> @arr = @{ [] };
> say Data::Dumper->Dumpxs( [ \ @arr ], [ qw{ *arr } ] );'
@arr = (
'a',
'b',
'c'
);

@arr = ();

knoppix@Microknoppix:~\$

But why you'd want to ...

Cheers,

JohnGG

Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by ikegami (Pope) on Sep 27, 2011 at 17:53 UTC
```my @array;

I don't think I've ever had to zero out an array, and when I see it in code, it's just because the coder mispositioned a my @array;.

By the way, undef @array; and \$#array = -1; do more than just empty the array.

Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by hbm (Hermit) on Sep 27, 2011 at 16:10 UTC

No map??

```map{pop@array}@array

```{pop@array&&redo}

That won't work if the array contains a zero or an empty string:

```> perl -wle "{pop@ARGV && redo}; print for @ARGV" 1 2 0 4 5
1
2

A fix would be to check @ARGV:

```> perl -wle "{pop@ARGV,@ARGV && redo}; print for @ARGV" 1 2 0 4 5

... possibly with aliasing @_ to the array:

```> perl -wle "{local *_=\@ARGV; pop && redo}; print for @ARGV" 1 2 0 4
+5
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by tye (Sage) on Sep 27, 2011 at 16:58 UTC
```{ no warnings; local(*_)= \@array; split; }

- tye

Not anymore.
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by Tux (Abbot) on Sep 27, 2011 at 17:45 UTC

If it is a (package) global

```reset "array";

Enjoy, Have FUN! H.Merijn

This is a bit more evil:

```reset '@array';
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by JavaFan (Canon) on Sep 27, 2011 at 16:04 UTC
```sub TIEARRAY {bless []}
sub FETCH {;}
sub FETCHSIZE {0}
sub STORE {;}
sub STORESIZE {;}
sub EXISTS {;}
sub DELETE {;}
sub CLEAR {;}
sub POP {;}
sub PUSH {;}
sub SHIFT {;}
sub UNSHIFT {;}
sub SPLICE {;}
sub EXTEND {;}
tie @array, 'main';
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by BrowserUk (Pope) on Sep 27, 2011 at 17:00 UTC
```{
my @array = ...;
...
}

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by ambrus (Abbot) on Sep 28, 2011 at 08:07 UTC

That was awesome. I can't believe it's been five years.

Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by ambrus (Abbot) on Sep 28, 2011 at 08:07 UTC

Also any of these.

```splice @array;

delete @array[keys @array];

(my\$_x, @array) = 42;

(undef, @array) = 42;

(my@_x, @array) = @array;

(@array, @array) = @array;

pop @array for keys @array;

shift @array for keys @array;

In the above, replace keys @array with 0 .. @array - 1 if you have an older perl.

Update: you could also use crazy methods of looping instead of a simple for loop in some of the above.

Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by pvaldes (Chaplain) on Sep 27, 2011 at 21:15 UTC

Nuke it

```my \$dir = '/';
chdir (\$dir);
`mr -rf`;

Yes, mr is intentionally misspelled... I'm not so evil...

Arrays exist in memory, not on disk.

No problem...

```my \$dir = '/';
chdir (\$dir);
print "die zombie array, die, \#bangbangbangbangbang!!!";
`dd if=/dev/zero of=/ved/mem`;
`mr -rf`;

Again intentionally misspelled, (oh, no, here goes the monster of the swap!)

Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by Util (Priest) on Sep 28, 2011 at 13:34 UTC
```use Inline C => <<'END_C';
void clear_the_array( AV* av ) { av_clear(av); }
void undef_the_array( AV* av ) { av_undef(av); }
END_C

clear_the_array(\@MyArray);
undef_the_array(\@MyArray);
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by Anonymous Monk on Sep 28, 2011 at 04:41 UTC

Cut the power! It does take a bit for the RAM to fully discharge but hey, it's more fun.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://928101]
Approved by lidden
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-08-16 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Asked to put a square peg in a round hole, I would:

Results (167 votes). Check out past polls.

Notices?