Mr said:
Thank-You. Please elaborate...
If THIS works:
my @a = split /regex/, @a2;
Not entirely sure what you think that's doing. It's trying to split
the string representing the *size* of @a2 by /regexp/. I'm guessing
you meant something more like:
my @a = split /regex/, $string;
which is essentially, if I understand you, saying:
my array = a list
Putting this into proper Perl code:
my @array = (1, 2, 3, 4, 5);
This is assigning an array variable to have the value of the list
containing 1, 2, 3, 4, and 5.
so why can't I do:
my array = shift (a list)
For the same reason that you can't do:
23 = "hello world";
A list is a *value*. Another name for a list is a "list literal". It
is not a variable that can be modified. The shift() function returns
*and removes* the first element of its array. That is, it directly
modifies its argument. A list is not something modifiable.
Consider this: the substr() function can produce an lvalue, if its
argument is itself an lvalue:
my $string = "Hello World";
substr($string, 1, 3) = "FOOBAR";
# $string now ==> "HFOOBARo World"
That works because $string can be assigned to, so therefore parts of
$string can be assigned to, changed, modified, deleted, etc. However,
if you were to pass substr a value that cannot be modified:
substr ("Hello World", 1, 3) = "FOOBAR";
You would get the error: Can't modify constant item in scalar
assignment
In this second example, the string "Hello World" is a literal, a
constant. It cannot be altered. The same applies to a list. An array
is a variable that can be assigned to, changed, modified, etc. A list
is a constant sequence of scalar values.
Have you read
perldoc -q "list and an array"
yet?
In other words, its OK to ASSIGN a list to an array (which would have
to be interpreted or coerced into an array), but I can't use the shift
operator on a list. Using this same rule, why can't the list be coerced
intro an array for the purpose of shifting it?
I don't understand what you mean by "coerced". An array is a Perl
variable that holds a given value at any given time. A list is a
sequence of values.
my @array = (1..5);
assigns a value to @array the same way
my $string = "Hello World";
assigns a value to $string
Its pretty obvioius what the code is TRYING to do
No, it's not obvious to me at all. What effect are you going for when
you try to shift a constant list? shift() returns *and removes* the
first element of an array. What would be the effect of removing an
element from a constant list?
If you're going for the first part of my description - the *returning*
of the first element, just do it directly:
my $first = (split /regexp/, $string)[0];
- and knowing Larry
as well as I do, he usually tries to make things work as you'd expect
them to. Except in some cases like this.
I think you're the only one expecting this to work any differently than
it does, unfortunately.
Weird. These restrictions make pipelined operations very awkward in
Perl...
No, it doesn't. You're just using the wrong function/operator for your
goal.
It would be clean syntax to use:
my @a = grep ... shift ... split, $scalar;
Hrm. I *think* what you're going for here is to have the grep operate
on everything *except* the first element of the list returned by split.
Is that correct?
Unfortunately, I can't think of any particular way to do this. But I
consider that a failure of list slices, not of the shift() function.
(Any one else able to come up with a way of doing this "in-line",
without assigning to a temp variable, or knowing in advance how many
fields are contained in $scalar?)
Paul Lalli