list vs. array

R

Robert Suyemoto

Hello.

I have recently heard the claim that in Perl,
"list" and "array" are two separate things, in that
one is of fixed length and the other isn't.

(Such that for the fixed length type, you could not
use push or pop on it.)

I have never heard of anything like this, but in
the interest of keeping an open mind, is there any
context where this would be true?


Thanks very much,
R.
 
D

David Squire

Robert said:
Hello.

I have recently heard the claim that in Perl,
"list" and "array" are two separate things, in that
one is of fixed length and the other isn't.

(Such that for the fixed length type, you could not
use push or pop on it.)

I have never heard of anything like this,

Gee. You could have tried reading the documentation that comes with
Perl. When I type "perldoc -q list vs array", the third entry is
entitled "What is the difference between a list and an array?". The
first two sentences of that entry are "An array has a changeable length.
A list does not."

That is not the only difference. I suggest that you read the documentation.


DS
 
P

Paul Lalli

Robert said:
I have recently heard the claim

you mean the fact.
that in Perl,
"list" and "array" are two separate things, in that
one is of fixed length and the other isn't.

One is a constant (or literal), one is a variable.
(Such that for the fixed length type, you could not
use push or pop on it.)

I have never heard of anything like this, but in
the interest of keeping an open mind, is there any
context where this would be true?

This is a list: ('foo', 'bar', 'baz')
This is an array: @stuff;

An array is a variable that contains a list of scalars. It is (mostly)
analogous to the difference between a scalar value, like 'Hello World',
and the variable $phrase which might contain it. You can change
$phrase by concatenating to it or using a s/// operator, but you can't
change an actual literal string:

#this works, obviously:
$string =~ s/Hello/Goodbye/;
#but this gives "Can't modify constant item in substitution (s///) "
'Hello World' =~ s/Hello/Goodbye/;

Similarly,
#this works
push @stuff, 'bam';
#but this gives "Type of arg 1 to push must be array (not constant
item)"
push ('foo', 'bar', 'baz'), 'bam';


See also: perldoc -q difference
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq4.pod
What is the difference between a list and an array?

Paul Lalli
 
U

Uri Guttman

PL> you mean the fact.

PL> One is a constant (or literal), one is a variable.

i would say a list is a value (could be generated from a func call or
expression) and an array is a variable which can hold a list.

other major points i use to explain this:

lists live on the stack and are live only during that expression.
arrays live on the heap (allocated) and can be alive as long as you want

list have no way to be referenced later
arrays can have references and be anonymous and used in data trees

lists can never have a name
arrays could have names

lists are fixed size since they are the result of an expression
arrays can have their size changed since they are in variables

lists and arrays can be indexed and sliced. this is about the only
operation common to both.

uri
 
R

robb

i would say a list is a value (could be generated from a func call or
expression) and an array is a variable which can hold a list.

Oy!

IMO, what a crazy distinction. Especially since the words 'array' and
'list' have meanings in the real world - for example, different kinds
of Abstract Data Types.
 
D

David Squire

Oy!

IMO, what a crazy distinction. Especially since the words 'array' and
'list' have meanings in the real world - for example, different kinds
of Abstract Data Types.

Oh for goodness sake! All programming languages give their reserved
words specific and restricted means compared to the usage of that word
in the "real world". Get used to it. There is a difference between a
Perl array and a Field Programmable Gate array too.

Ah you saying that the distinction between a string variable and a
string literal in almost every programming language is also "crazy"?

There is an important distinction between a variable and the values it
can represent?

'This is a string literal';
my $string; # this is a variable capable of representing a string
(amongst other things).


DS
 
R

robb

David said:
Oh for goodness sake!
:)


Ah you saying that the distinction between a string variable and a
string literal in almost every programming language is also "crazy"?

Not at all. Especially as you named them, respectively:
"string variable", and
"string literal".

But as soon as you'd call one a "string", and the other something like
"text block", then it gets suspicious.

Now applying (your!) naming logic (which I agree with), two names such
as:
"list variable", and
"list literal"...

would generate a guaranteed 10% reduction in pointless questions to
c.l.p.m.
 
T

Tad McClellan

[ Please provide an attribution when you quote someone. ]

Oy!

IMO, what a crazy distinction.


But one that must be understood if you hope to understand Perl.

It can be crazy, and you can argue it if you like, but you still
need to make the distinction if you need to make sense of Perl.

Especially since the words 'array' and
'list' have meanings in the real world -


And they have meanings in the Perl world.

for example, different kinds
of Abstract Data Types.


for example, data and a variable that can hold that kind of data.
 
U

Uri Guttman

ro> Now applying (your!) naming logic (which I agree with), two names such
ro> as:
ro> "list variable", and
ro> "list literal"...

ro> would generate a guaranteed 10% reduction in pointless questions to
ro> c.l.p.m.

balderdash. people reading the docs would lower the pointless questions
to almost 0. computer stuff is about being exact in what you code and
that means exact in the meanings of terms. perl like all other projects
chose its names and terms and the key is to be consistant with them, not
to make them cater to those who don't care. if you want to hack perl you
talk perl. you don't talk cobol or python here. lists and arrays are
well defined in perl and they are not the same. did you even read the
differences i listed? did you understand how they are actually more
different than they are the same? do you understand the concept of a
value vs a variable (types don't matter)? string variable vs string
literal. the difference is variable vs literal and string doesn't
matter. list value and array variable. notice the differences there?
both terms are different so they both mean something. lists are not
variables nor arrays. arrays are not values (but they contain a value
and can supply that value to an expression).

learn perl and stop asking for perl to learn you.

uri
 
R

robb

Uri said:
did you even read the differences i listed?

Of course. No reason to get rude. I'm approaching the language like I
approach all languages - as a computer scientist. By using academic-
and industry-standard language, it gives us a common way to
communicate. I've read all the perldocs and many posts. I haven't
looked at the language specification, though. Maybe that'd be helpful.

I couldn't really understand what you're saying in your last post;

Are you talking about the mutability of the data structure?

Or, are you talking about the fact that a "list" is immutable simply
because it's literally specified?

Are you saying that the list notation causes an immutable data
structure to be instantiated, whereas array notation causes a mutable
one to be?

It doesn't sound like it, since a list can be assiged to a named
variable, and it's then mutable.

How would a "list variable" differ from an array?

How would an anonymous array differ from a list?
 
U

Uri Guttman

ro> Of course. No reason to get rude. I'm approaching the language
ro> like I approach all languages - as a computer scientist. By using
ro> academic- and industry-standard language, it gives us a common way
ro> to communicate. I've read all the perldocs and many posts. I
ro> haven't looked at the language specification, though. Maybe
ro> that'd be helpful.

you were rude in bitching about lists and arrays in your terminology. i
was responding in kind. there is no language spec for perl5 (perl6 has
one). and if you really are a computer scientist (and not just playing
one on tv) you should know that langs use terms in very specific ways and
they won't bend to your tastes. and also there is no academic and
industry standard language for any of this. have you seen how many
different overloadings of common terms are out there?

ro> I couldn't really understand what you're saying in your last post;

ro> Are you talking about the mutability of the data structure?

ro> Or, are you talking about the fact that a "list" is immutable simply
ro> because it's literally specified?

????

what i said was very clear and any computer scientist should be able to
understand them.

ro> Are you saying that the list notation causes an immutable data
ro> structure to be instantiated, whereas array notation causes a mutable
ro> one to be?

there is no list notation. where do you get ideas like that? there is no
special syntax for lists in perl. none. nada. nil. bupkis. a list is
made by having a list context and some data that is in that context. no
syntax anywhere.

ro> It doesn't sound like it, since a list can be assiged to a named
ro> variable, and it's then mutable.

some CS you are. you don't seem to get the difference between a variable
and a value.

ro> How would a "list variable" differ from an array?

who ever said they are different or the same? perl has arrays and
lists. i was making a point about terminology and what is important in
naming things. variables are not values. lists are values. simple.

ro> How would an anonymous array differ from a list?

as i said, the anon part has nothing to do with a list. anon arrays are
really array refs. you can't take a ref of a list as the list is on the
stack and refs refer to heap allocations. so anon and lists are apples
and oranges.

uri
 
T

Tad McClellan

How would an anonymous array differ from a list?


An array is on the heap, and a list is on the stack. (that sounds familiar...)


That doesn't help those students who don't have much computer science
though, so I try to put it into layman's terms:

The list is gone once you get the semicolon that ends the
statement it appears in. An array (named or not) can live on.
 
R

robb

Tad said:
An array is on the heap, and a list is on the stack. (that sounds familiar...)

Thanks - I hadn't seen it expressed this concisely up till now.

Is it advisable to think about these issues and consider them when
actually doing programming?

For example, when I work with lists and arrays, I usually focus on
issues such as do I want to be manipulating arrays or arrayrefs...
Which do I want to be using in my APIs, etc.

Now that I think about it, I think that part of my confusion is because
I'm used to programming in languages where all variables are references
- Python, Java, etc.
 
U

Uri Guttman

ro> Thanks - I hadn't seen it expressed this concisely up till now.

did you read my post? did you see (that sounds familiar...) there? tad
was refering to my post which said the exact same thing. bah.

ro> Is it advisable to think about these issues and consider them when
ro> actually doing programming?

ro> For example, when I work with lists and arrays, I usually focus on
ro> issues such as do I want to be manipulating arrays or arrayrefs...
ro> Which do I want to be using in my APIs, etc.

that makes no sense whatsoever.

ro> Now that I think about it, I think that part of my confusion is because
ro> I'm used to programming in languages where all variables are references
ro> - Python, Java, etc.

that is your problem. you are stuck in other langs world view. this is
perl. learn perl to code in perl. drop all concepts of what you think
perl should be like. perl isn't going to change for you. so you have to
change for perl.

uri
 
K

Keith Keller

Thanks - I hadn't seen it expressed this concisely up till now.

Uri said it in the exact post to which you posted a followup saying the
distinction was crazy (you snipped the part about the heap and the
stack). Perhaps that's why Tad believes it sounds familiar.

--keith
 
T

Tad McClellan

Is it advisable to think about these issues and consider them when
actually doing programming?


Yes. Much of the time perl's DWIMer will shield you from needing
to know the difference.

But having to qualify it with "Much" implies that some times the
distinction is important.

A rather contrived example:

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

my $numList = nums_list();
print "list: $numList\n";

my $numArray = nums_array();
print "array: $numArray\n";

sub nums_list { return 4, 5, 6 }
sub nums_array {my @nums=(4, 5, 6); return @nums}
 
U

Uri Guttman

d> Oh there's every reason to be rude. This is Uri "Motherfucker"
d> Guttman. He is the supreme asshole of this group. To be fair to
d> the bastard, I have to admit he has every reason to be bitter - I

how kind and sweet of you to reply. anonymous posts are so honest and
heartwarming. and your perl skills are renowned throughout the land
too. and such poetry and prose you have composed over the centuries. you
are so famous and yet you deign to write to me personally. i am honored.

and there are cures for vaginal infections. you don't have to be a
recluse anymore. just see a doctor, preferably a headshrinker.

uri
 
C

Ch Lamprecht

Tad said:
Yes. Much of the time perl's DWIMer will shield you from needing
to know the difference.

But having to qualify it with "Much" implies that some times the
distinction is important.

A rather contrived example:

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

my $numList = nums_list();
print "list: $numList\n";

my $numArray = nums_array();
print "array: $numArray\n";

sub nums_list { return 4, 5, 6 }
sub nums_array {my @nums=(4, 5, 6); return @nums}

This example illustrates context propagation to the return statement and
behaviour of the 'comma' operator in scalar context. There is no list at all.

Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top