CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
Arrays

 



Cure
User

Jul 3, 2000, 8:54 PM

Post #1 of 9 (37170 views)
Arrays Can't Post

@cure = ("one", "two", "three", "four");
@cure = ("one", "two", ("three", "four"));

ARE THE ARRAYS EQUIVALENT? EXPLAIN WHY THEY ARE EQUIVALENT OR EXPLAIN WHY THEY ARE NOT EQUIVALENT.

Cure


perlkid
stranger

Jul 3, 2000, 11:11 PM

Post #2 of 9 (37169 views)
Re: Arrays [In reply to] Can't Post

 
I think they are because the output is the same for both of them.

perlkid


reaper5
Deleted

Jul 4, 2000, 9:05 AM

Post #3 of 9 (37169 views)
Re: Arrays [In reply to] Can't Post

@cure = ("one", "two", ("three", "four"));
foreach $n (@cure) {
print "$n\n";
}

This program outputs:
one
two
three
four

The first array will obviously give that output, so this program prooves they are the same

reaper5 (DumbFish)


rGeoffrey
User

Jul 4, 2000, 8:18 PM

Post #4 of 9 (37169 views)
Re: Arrays [In reply to] Can't Post

If I add a third array to this discussion...

@rGeoffrey = ("one", "two", ["three", "four"]);

WILL IT ALSO BE EQUIVALENT? WHY?

PS. The foreach loop used to print is a good place for map, one of my favorite features of perl:

print map { "$_\n" } @cure;


errr
Deleted

Jul 5, 2000, 7:22 PM

Post #5 of 9 (37169 views)
Re: Arrays [In reply to] Can't Post

As rGeoffrey mentions, there is a difference between the ( ) and the [ ] . Both are often thought of as "list" operators.
[ ] returns a reference. References are of course simply scalars, so in rGeoffrey's example, the last element of the list is a scalar which also happens to be a pointer to a list. $a[2] itself would be a memory location. When we follow the reference, $a[2]->[0] would be "three".
( ) though is not a list operator. It simply sets precedence. In Cure's example precedence doesn't matter. The lists are simply combined. So what, then, does ( ) have to do with list context? Why doesn't:
@a = 1,2,3; work as expected without the ( )? It would seem to imply that ( )'s make a "list" but really that is the work of the comma. A comma, in list context is simply a list delimiter. In scalar context, it evaluates the left side, evaluates the right hand side then returns the result from the right. @a = 1,2,3 is really saying (@a = 1), 2, 3. First it does the assignment, throws away the results, eventually returning a "3" if you cared to check its return.
In summary, the ( ) does not make a list. It simply prevents the = from being executed first. The order of operation is irrelevant in Cure's example; the outer ( )'s protect it from the =. The inner ( )s simply make it a combination of lists. It is expanded just as (@a, @b) would be.


deus
Deleted

Jul 5, 2000, 7:42 PM

Post #6 of 9 (37169 views)
Re: Arrays [In reply to] Can't Post

The two arrays are obviously the same since presendence has nothing to do with what we're looking at, this is also a learning perl example if I remember correctly, so they are the same - accept_it Wink


Cure
User

Jul 5, 2000, 8:20 PM

Post #7 of 9 (37169 views)
Re: Arrays [In reply to] Can't Post

@cure = ("one", "two", "three", "four");
@cure = ("one", "two", ("three", "four"));


ANSWER TO THE QUIZ--> YES THEY ARE EQUIVALENT.

Errr you are exactly RIGHT. Nice job.

( ) is NOT a list operator. It simply sets precedence. The outer ( )'s protect it from the =. The inner ( )s simply make it a combination of lists.


Cure


durose78
Novice

Jul 13, 2000, 10:30 PM

Post #8 of 9 (37169 views)
Re: Arrays [In reply to] Can't Post

Of course they are equivalent!


hydpm
User

Jul 12, 2007, 9:16 AM

Post #9 of 9 (34932 views)
Re: [durose78] Arrays [In reply to] Can't Post

 
@test = ("one", "two", ["three", "four"]);
print map {"$_\n"} @test;
print @test[2]->[0];

output:
--------------------------------------------------------------
one
two
ARRAY(0x9cbdc20)


when the array as above then , how can we print the values for the references along with the scalars?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives