On 8/14/07, Mr. Shawn H. Corey <[EMAIL PROTECTED]> wrote:
> Chas Owens wrote:
> > On 8/14/07, DJ Gruby <[EMAIL PROTECTED]> wrote:
> >> Hello there!
> >>
> >> Have question if that is possible to empty a few arrays with one simple 
> >> command?
> >>
> >> For the moment I clear these six tables by assigning them an empty list:
> >>
> >>    @bitmap_addr_lo = ();
> >>    @bitmap_addr_hi = ();
> >>    @screen_addr_lo = ();
> >>    @screen_addr_hi = ();
> >>    @colors_addr_lo = ();
> >>    @colors_addr_hi = ();
> >>
> >> Just wondering if there is a more elegant way to achieve the same result?
> >>
> >> Many thanks in advance for your help!
> >>
> >> Regards,
> >> DJ Gruby.
> >
> > The desire to empty arrays is a bad sign.  It generally means you do
> > not have them declared with the correct scope, but, assuming you have
> > a good reason, there are many ways to reset a bunch of arrays.  Here
> > are two in addition to your straight forward way.
> >
> > @bitmap_addr_lo = @bitmap_addr_hi = @screen_addr_lo = @screen_addr_hi
> > = @colors_addr_lo = @colors_addr_hi = ();
> >
> > @$_ = () for \(@bitmap_addr_lo, @bitmap_addr_hi, @screen_addr_lo,
> > @screen_addr_hi, @colors_addr_lo, @colors_addr_hi);
> >
> > Frankly, I think the straight forward way is more readable since I
> > dislike lines longer than 78 characters.  But, given the names of the
> > arrays and the fact that they are all being reset at the same time I
> > think you are using the wrong data structure.  You should be using an
> > HoHoA (a hash of hashes of arrays): $addr{screen}{hi} instead of
> > @screen_addr_hi.
> >
> > Then you could clear them all by saying
> >
> > for my $type (qw<screen bitmap colors>) {
> >     for my $loc (qw<hi low>) {
> >         $addr{$type}{$loc} = [];
> >     }
> > }
> >
> > or (my favorite)
> >
> > @{$_}{qw<hi low>} = ([], []) for @addr{qw<screen bitmap colors>};
> >
> > or the more comprehensive (but dangerous if other keys are being used)
> >
> > for my $type (keys %addr) {
> >     @$_ for keys %$type;
> > }
> >
>
> Or simply:
>
>   %addr = ();

If you can do that safely then it would almost certainly be better
%addr if simply went out of scope.  It is a complete destruction of
the data structure (as opposed to the emptying of a part of it).
Autovivification can save you from some problems, but not all of them:

#!/usr/bin/perl

use strict;
use warnings;

my %addr = (
        screen => {
                low  => [0,1,2],
                high => [0,1,2],
        },
        bitmap => {
                low  => [0,1,2],
                high => [0,1,2],
        },
        colors => {
                low  => [0,1,2],
                high => [0,1,2],
        }
);

print "there are " . @{$addr{screen}{low}} .
        " items in the low screen address\n";

@{$_}{qw<hi low>} = ([], []) for @addr{qw<screen bitmap colors>};

print "there are " . @{$addr{screen}{low}} .
        " items in the low screen address\n";

%addr = ();

print "there are " . @{$addr{screen}{low}} .
        " items in the low screen address\n";

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to