flebber wrote:
Hi

Hello,

I can get the standard list slicing to work. But what about getting
the result of a list slice to an interpolated string or a range
interpolated.

In beginning perl Simon uses this example

#!/usr/bin/perl
# multilist.plx
use warnings;
use strict;
my $mone; my $mtwo;
($mone, $mtwo) = (1, 3);
print (("heads ", "shoulders ", "knees ", "toes ")[$mone, $mtwo]);
print "\n";

which is fine but say I wanted the print result to be: " I am touching
my shoulders and toes" rather than (shoulders toes)

printf " I am touching my %s and %s", ( "heads ", "shoulders ", "knees ", "toes " )[ $mone, $mtwo ];


I tried to create a list variable

There is no such thing as a "list variable".


and then return the output of the
list interpolated but not getting expected result.

For example using months

#!/usr/bin/perl
use strict;
use warnings;

#my $summer = (0,1,2);
#my $winter = (5,6,7);

qw|
        January         February        March
        April           May                     June
        July            August          September
        October         November        December
        |[(my @summer = (0,1,2)), (my @winter = (5,6,7))];

That should be:

my @summer = ( qw|
        January         February        March
        April           May                     June
        July            August          September
        October         November        December
        | )[ 0, 1, 2 );
my @winter = ( qw|
        January         February        March
        April           May                     June
        July            August          September
        October         November        December
        | )[ 5, 6, 7 );

But there you have two copies of the same list. Better to store the list in an array:

my @months = qw|
        January         February        March
        April           May                     June
        July            August          September
        October         November        December
        |;

And then use an array slice for @summer and @winter:

my @summer = @months[ 0, 1, 2 );
my @winter = @months[ 5, 6, 7 );


print "My Summer months @summer and my winter months @winter \n";

This returns the array 0,1,2 not the slice from the list.


John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

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


Reply via email to