473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

obtaining lengths of slices

@array1 = (1,2,3);

$length = @array1[1,2]; # assigning a slice to $length

print "length = $length"; # prints 3 (ie length of array not length of slice)

@slice = @array1[1,2];

print "\n";

$length = @slice; # assigning slice-converted-into-array to $length

print "length = $length"; # prints 2

----------------------------------------

Why the difference ?

Advice welcomed.

Thanks - Griff
Jul 19 '05 #1
6 3286
Griff wrote:
@array1 = (1,2,3);

$length = @array1[1,2]; # assigning a slice to $length

print "length = $length"; # prints 3 (ie length of array not length of slice)

@slice = @array1[1,2];

print "\n";

$length = @slice; # assigning slice-converted-into-array to $length

print "length = $length"; # prints 2

----------------------------------------

Why the difference ?


Because an array slice is a LIST, not an array. A LIST returns the
last element when evaluated in scalar context, as opposed to an array
which returns the number of elements when evaluated in scalar context.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<Oj********************@newsc.telia.net>...
Because an array slice is a LIST, not an array. A LIST returns the
last element when evaluated in scalar context, as opposed to an array
which returns the number of elements when evaluated in scalar context.


There is no such thing in Perl as a LIST in a scalar context!

A slice in a scalar context returns the last element.

IMNSHO an array slice in an obviously scalar context should throw a
warning. This would be much more use than the downright dumb "Scalar
value @foo[1] better written as $foo[1]" warning we have now. I quite
often want to use a slice that happens _now_ to be a single element
slice but which earlier or later in the evolution of the code may have
more elements. On the other hand can see no reason to ever
intensionally use an array (or hash) slice in a context that is known
at compile time to be scalar.

(It can make sense to use a list slice in a scalar context. It also
can make sense to use an array slice in a subroutine return where the
context is not known until runtime.)

This newsgroup does not exist (see FAQ). Please do not follow-up here
without pointing this out.
Jul 19 '05 #3
Griff wrote:
@array1 = (1,2,3);
$length = @array1[1,2]; # assigning a slice to $length
print "length = $length"; # prints 3 (ie length of array not length of slice)


The number 3 is not what you think it is.

@array1 = (10,20,30);
$lastitem = @array1[1,2];
print "Last item returned by the comma operator is: $lastitem\n";

$length_of_slice = () = @array1[1,2];
print "Using a dummy list returns the length of $length_of_slice\n";

-Joe
Jul 19 '05 #4
no****@mail.com wrote:
Gunnar Hjalmarsson wrote:
Because an array slice is a LIST, not an array. A LIST returns
the last element when evaluated in scalar context, as opposed to
an array which returns the number of elements when evaluated in
scalar context.
There is no such thing in Perl as a LIST in a scalar context!


In that case, how would you describe this statement:

my $scalar = (10, 20, 30);

(It generates "Useless use ... in void context" warnings, but $scalar
is assigned he value of the last element.)
A slice in a scalar context returns the last element.
What's the difference compared to a list literal (besides the warnings)?
IMNSHO an array slice in an obviously scalar context should throw a
warning.


Aha, so you want them to behave identically. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #5
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<jG******************@newsb.telia.net>...
no****@mail.com wrote:
Gunnar Hjalmarsson wrote:
Because an array slice is a LIST, not an array. A LIST returns
the last element when evaluated in scalar context, as opposed to
an array which returns the number of elements when evaluated in
scalar context.


There is no such thing in Perl as a LIST in a scalar context!


In that case, how would you describe this statement:

my $scalar = (10, 20, 30);


An obfucated way of saying:

10; 20; my $scalar = 30;

No, seriously, I might describe it as a list but I would know I was
using sloppy terminology in the same way as a might sloppily refer to
an "array of arrays" in Perl when really I mean "array of refernces to
arrays". As I've said numerous times sloppy short-hand terminology is
fine as long as everyone knows that's what it is. Things start to go
wrong as soon as people start trying to draw inferences from a literal
interpretation of the short-hand terminology.

I certainly would not describe the RHS of the above statement as a
LIST (in capitals) because it is not.

I've previously suggested we could introduce the term "lexical list"
to describe a fragment of Perl source code that has a list-like
appearance but is not a LIST. The trouble with this is that people
might confuse people even more.

In perl there are two ways a comma can be parsed. In an argument-list
(of a protyped function) list it can be parsed as an argument
separator.

In other contexts it's an left associative operator.

In a LIST context comma is the LIST concatenation operator. It
evaluates it's LHS and RHS operands in LIST contexts then returns the
concatenation of the LISTs.

In a SCALAR context comma evaluates it's LHS operand in a VOID
context, then evaluates it's RHS operand in a SCALAR context then
returns value of the RHS.

In a VOID context comma evaluates it's LHS operand in a VOID context,
then evaluates it's RHS operand in a VOID context then doesn't return
anything (because it's in a VOID context - like duh!).

Note unlike most operators the order of the evaluation of the operands
by a comma operator in a SCALAR or VOID context is assured.
Technically it's not assured in a LIST context but I suspect that it
would break so much code it it ever changed that it's unlikely ever to
do so.
A slice in a scalar context returns the last element.


What's the difference compared to a list literal (besides the warnings)?


There is really no such thing as a list literal in Perl although it
is a convenient short-hand to describe an expression consisting
entirely of literals and comma operators evaluated in a LIST context.
There certainly is no such thing as a list literal in a scalar
context. There is such a thing as a slice in a scalar context.

Let us consider the two facts in question here.

A) scalar(1,2,3) same as scalar((1,2,3)[-1])
B) scalar(@foo[EXPR]) same as scalar((@foo[EXPR])[-1])

I'm not denying that there is a superficial similarity. However you
appeared to claim a _causual_ relationship (A=>B). There is no such
relationship. There is no general rule.

It should also be noted that

scalar(foo(),bar()) != scalar((foo(),bar())[-1])
IMNSHO an array slice in an obviously scalar context should throw a
warning.


Aha, so you want them to behave identically. ;-)


That has no meaning because the situations are not comparible.

my $bar = @foo[3]; # Currently: warn, I want: warn
my ($bar) = @foo[3]; # Currently: warn, I want: no warn
my $bar = @foo[1,2]; # Currently: no warn, I want: warn
my $bar = @foo[@q]; # Currently: no warn, I want: warn
Jul 19 '05 #6
no****@mail.com wrote:
Gunnar Hjalmarsson wrote:
no****@mail.com wrote:
There is no such thing in Perl as a LIST in a scalar context!


In that case, how would you describe this statement:

my $scalar = (10, 20, 30);


An obfucated way of saying:

10; 20; my $scalar = 30;


<interesting discussion snipped>

Thanks for the lecture! :)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: David Abrahams | last post by:
Can anyone explain the logic behind the behavior of list slicing with negative strides? For example: >>> print range(10) I found this result very surprising, and would just like to see the...
29
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I...
1
by: Antoon Pardon | last post by:
I'm for the moment writing two classes. A table, which is like a list, but can start at any integer. A tree which is like a dictionary, but will iterate over the keys in sorted order. The...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
2
by: brianlum | last post by:
Hi, I have been looking for a good way to convert python code into a control flow graph. I know of Python functions that will convert an expression into an abstract syntax tree (i.e. ast =...
1
by: meridian | last post by:
If, like me, you're always forgetting which way around your list/seq slices need to go then worry no more. Just put my handy "slice lookupper" (TM) ) on a (large!) PostIt beside your screen and,...
3
slightlybefuddled
by: slightlybefuddled | last post by:
(Exporting ImageReady slices as CSS rather than tables) apparently means it'll work just fine in Firefox, but do wacky stuff in IE? Can anyone help me figure out why on earth the slices are not...
31
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the...
5
by: NuberSteve | last post by:
I'm very new to using CSS and also the concept of slices for mouse-overs, and have made my first attempt at using ImageReady to generate slices of a world map. I basically wanted a map that would...
3
by: David | last post by:
That xpairs() generator is nice, but it's not the best possible code What do you mean by best possible? Most efficient? Most readable? And why don't you use islice? eg: def xpairs(seq):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.