Hi
the piece of code below is taken directly from the O'Reilly Perl
Cookbook (4.11. Processing Multiple Elements of an Array) with the
exception of the print statements which I have in to see what's going on.
However, it doesn't work! I'm starting to wonder if something's changed
in Perl or if this is a misprint in the book. Apparently it's 1st Edition
August 1998.
gavin at fiachra rho_0.51> ./test.pl
Can't use string ("Peter") as an ARRAY ref while "strict refs" in use at
./test.pl line 22.
If I comment out the use strict (just to see) I get.
gavin at fiachra rho_0.51> ./test.pl
|
Peter Paul Mary Jim Tim
Modification of non-creatable array value attempted, subscript -2 at
./test.pl line 26.
Here's the code:
#!/usr/bin/perl
use strict;
my @friends = qw(Peter Paul Mary Jim Tim);
(my $this,my $that) = shift2(@friends);
# $this contains Peter, $that has Paul, and
# @friends has Mary, Jim, and Tim
print "$this | $that\n";
print "@friends\n";
my @beverages = qw(Dew Jolt Cola Sprite Fresca);
my @pair = pop2(@beverages);
# $pair[0] contains Sprite, $pair[1] has Fresca,
# and @beverages has (Dew, Jolt, Cola)
print "@pair\n";
print "@beverages\n";
sub shift2 (\@) {
return splice(@{$_[0]}, 0, 2);
}
sub pop2 (\@) {
return splice(@{$_[0]}, -2);
}
Can anyone help?
Gavin
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!