Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by moritz (Cardinal) on Sep 27, 2011 at 15:24 UTC
|
| [reply] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by wfsp (Abbot) on Sep 27, 2011 at 16:30 UTC
|
$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)
| [reply] [d/l] [select] |
|
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 ;-)
| [reply] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by Not_a_Number (Prior) on Sep 27, 2011 at 16:36 UTC
|
| [reply] [d/l] |
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 ...
| [reply] [d/l] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by ikegami (Patriarch) 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.
| [reply] [d/l] [select] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by tye (Sage) on Sep 27, 2011 at 16:58 UTC
|
{ no warnings; local(*_)= \@array; split; }
| [reply] [d/l] |
|
| [reply] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by Tux (Canon) on Sep 27, 2011 at 17:45 UTC
|
reset "array";
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] |
|
reset '@array';
| [reply] [d/l] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by hbm (Hermit) on Sep 27, 2011 at 16:10 UTC
|
map{pop@array}@array
Late addition:
{pop@array&&redo}
| [reply] [d/l] [select] |
|
> 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
| [reply] [d/l] [select] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by BrowserUk (Patriarch) 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.
| [reply] [d/l] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by ambrus (Abbot) on Sep 28, 2011 at 08:07 UTC
|
| [reply] |
|
| [reply] |
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';
| [reply] [d/l] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by ambrus (Abbot) on Sep 28, 2011 at 08:07 UTC
|
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.
| [reply] [d/l] [select] |
Re: TIMTOWTDI Challenge: Zero'ing Out An Array
by pvaldes (Chaplain) on Sep 27, 2011 at 21:15 UTC
|
my $dir = '/';
chdir ($dir);
`mr -rf`;
Yes, mr is intentionally misspelled... I'm not so evil... | [reply] [d/l] |
|
Arrays exist in memory, not on disk.
| [reply] |
|
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!) | [reply] [d/l] |
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);
| [reply] [d/l] |
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.
| [reply] |