SPUG: Slice of an arrary within a hash?

Daniel V. Ebert debert at dusya.osd.com
Fri May 5 14:27:54 CDT 2000


This thread has sparked a couple questions.

1. I haven't seen this type of thing before
@{ $DATA{$dsku} }
and was wondering if someone could write it out 'longhand'

If there is a bit of the Camel or Llama book that covers it I can read it  
myself, but didn't find one im my search.

2.  I often see people saying "always use strict" ... is there some  
recommended reading on that subject?

Thanks for your time!

Dan.
==============
It's poetry in motion
She turned her tender eyes to me
As deep as any ocean
As sweet as any harmony

   -Thomas Dolby

You wrote:
> Replying to myself here...
>
> I now see you were talking about slices.  If this is what you want, you
> could do one of the following 2 ways:
>
>
> #!/usr/bin/perl
>
> # You should always use strict
> use strict;
>
> my %DATA;
> my $dsku   = 'fred';
> my @prices = ('11.11','22.22','33.33','44.44');
>
>
> # EITHER THIS
> $DATA{$dsku}[0]         = '00.00';
> @{ $DATA{$dsku} }[1..4] = @prices;
> $DATA{$dsku}[5]         = '55.55';
>
> # OR THIS
> @{ $DATA{$dsku} }[0..5] = ('00.00', @prices, '55.55');
>
>
> #I also moved my $count++ so that it reflects 0-5, not 1-6
> my $count = 0;
>
> for my $element ( @{ $DATA{$dsku} } ) {
> print "\$DATA{\$sku}[$count] = '$element'\n";
> $count++;
> }
>
> __END__
>
> HTH
>
> Joel
>
>
>
> On Fri, 5 May 2000, Joel wrote:
>
> > I'm not completely clear on what you're trying to do, but it looks like
> > something like this would work for you:
> >
> >
> > #!/usr/bin/perl
> >
> > # You should always use strict :-)
> > use strict;
> >
> > my %DATA;
> > my $dsku   = 'fred';
> > my @prices = ('11.11','22.22','33.33','44.44');
> >
> > @{ $DATA{$dsku} } = ('00.00', @prices, '55.55');
> >
> > my $count;
> > for my $element ( @{ $DATA{$dsku} } ) {
> > $count++;
> > print "\$DATA{\$sku}[$count] = '$element'\n";
> > }
> >
> > __END__
> >
> > Joel
> >
> >
> >
> > > I'd appreciate a little help understanding the behavior of the
> > > following script.
> > >
> > > I've included 3 versions of the same script which are identical except
> > > for the subset of lines which have been commented out.
> > >
> > > The first example is producing the results I want but I was trying to
> > > streamline the logic.
> > >
> > > - - - - - - - - - - - - - - - Version 1
> > >
> > > #!/usr/bin/perl
> > > %DATA;
> > > $dsku = 'fred';
> > > @prices = ('11.11','22.22','33.33','44.44');
> > > $DATA{$dsku}[0] = '00.00';
> > > #$DATA{$dsku}[1 .. 4] = @prices[0 .. 3];
> > > #$DATA{$dsku}[1,2,3,4] = @prices[0,1,2,3];
> > > $DATA{$dsku}[1] = @prices[0];
> > > $DATA{$dsku}[2] = @prices[1];
> > > $DATA{$dsku}[3] = @prices[2];
> > > $DATA{$dsku}[4] = @prices[3];
> > > $DATA{$dsku}[5] = '55.55';
> > >
> > > for ( my $i = 0; $i < 6; $i++ )
> > > {
> > > print "\$DATA{\$sku}[$i] = '$DATA{$dsku}[$i]'\n";
> > > }
> > > ====>
> > > $DATA{$sku}[0] = '00.00'
> > > $DATA{$sku}[1] = '11.11'
> > > $DATA{$sku}[2] = '22.22'
> > > $DATA{$sku}[3] = '33.33'
> > > $DATA{$sku}[4] = '44.44'
> > > $DATA{$sku}[5] = '55.55'
> > >
> > > - - - - - - - - - - - - - - - Version 2
> > >
> > > #!/usr/bin/perl
> > > %DATA;
> > > $dsku = 'fred';
> > > @prices = ('11.11','22.22','33.33','44.44');
> > > $DATA{$dsku}[0] = '00.00';
> > > $DATA{$dsku}[1 .. 4] = @prices[0 .. 3];
> > > #$DATA{$dsku}[1,2,3,4] = @prices[0,1,2,3];
> > > #$DATA{$dsku}[1] = @prices[0];
> > > #$DATA{$dsku}[2] = @prices[1];
> > > #$DATA{$dsku}[3] = @prices[2];
> > > #$DATA{$dsku}[4] = @prices[3];
> > > $DATA{$dsku}[5] = '55.55';
> > >
> > > for ( my $i = 0; $i < 6; $i++ )
> > > {
> > > print "\$DATA{\$sku}[$i] = '$DATA{$dsku}[$i]'\n";
> > > }
> > > ====>
> > > $DATA{$sku}[0] = '44.44'
> > > $DATA{$sku}[1] = ''
> > > $DATA{$sku}[2] = ''
> > > $DATA{$sku}[3] = ''
> > > $DATA{$sku}[4] = ''
> > > $DATA{$sku}[5] = '55.55'
> > >
> > > - - - - - - - - - - - - - - - Version 3
> > >
> > > #!/usr/bin/perl
> > > %DATA;
> > > $dsku = 'fred';
> > > @prices = ('11.11','22.22','33.33','44.44');
> > > $DATA{$dsku}[0] = '00.00';
> > > #$DATA{$dsku}[1 .. 4] = @prices[0 .. 3];
> > > $DATA{$dsku}[1,2,3,4] = @prices[0,1,2,3];
> > > #$DATA{$dsku}[1] = @prices[0];
> > > #$DATA{$dsku}[2] = @prices[1];
> > > #$DATA{$dsku}[3] = @prices[2];
> > > #$DATA{$dsku}[4] = @prices[3];
> > > $DATA{$dsku}[5] = '55.55';
> > >
> > > for ( my $i = 0; $i < 6; $i++ )
> > > {
> > > print "\$DATA{\$sku}[$i] = '$DATA{$dsku}[$i]'\n";
> > > }
> > > ====>
> > > $DATA{$sku}[0] = '00.00'
> > > $DATA{$sku}[1] = ''
> > > $DATA{$sku}[2] = ''
> > > $DATA{$sku}[3] = ''
> > > $DATA{$sku}[4] = '44.44'
> > > $DATA{$sku}[5] = '55.55'
> >
> >
>
> ---------------------------------------------------
> Procedure Computers_Move(var CPU_Turn :boolean);
> var a:integer;
> begin
> writeln('Computer decides to skip this round. ');
> CPU_Turn := False;
> end;
>
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
> Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
> SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
> Email to majordomo at pm.org: "action" spug-list your_address
>
>



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
           Email to majordomo at pm.org: "action" spug-list your_address





More information about the spug-list mailing list