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: Perl Programming Help: Beginner:
slices

 



sttring
Deleted

Jan 17, 2001, 6:37 AM

Post #1 of 8 (763 views)
slices Can't Post

In my perl book it says the following.

@who = (qw(fred barney betty wilma))[2,3];
# like @x = qw(fred barney betty wilma); @who = @x[2,3];

According to the book these two examples do the same thing, and the first one is a shorter form but from what I can see they aren't comparable. In the first array the [2,3] at the end of it is actually changing the value of the array. If you print @who after that the only possible choices to choose from will be betty and wilma.

In the second array though @x ''s value remains the same and @who is assigned to 2 of its values.

Can someone explain to me how these two examples are the same as they seem completely different to me.

Thanks,
sttring



japhy
Enthusiast

Jan 17, 2001, 6:59 AM

Post #2 of 8 (761 views)
Re: slices [In reply to] Can't Post

The first example is:


Code
@who = (qw( fred barney betty wilma ))[2,3];

Let's break this down. qw( fred barney betty wilma ) is creating a list at run-time, separated on whitespace. We put parentheses around it because it's the same as split(' ', ' fred barney betty wilma '), and you can't take a subscript on a function. So now we have something that looks like:


Code
@who = ('fred', 'barney', 'betty', 'wilma')[2,3];

The right-hand side, ('fred', 'barney', 'betty', 'wilma')[2,3], says to extract elements 2 ('betty') and 3 ('wilma') from the list. So now we have:


Code
@who = ('betty', 'wilma');

And thus, we assign two values to @who.

The second example is:


Code
@x = qw( fred barney betty wilma ); 
@who = @x[2,3];

Using our knowledge from before, we can write this as:


Code
@x = ('fred', 'barney', 'betty', 'wilma'); 
@who = @x[2,3];

Which sets @who to elements 2 ('betty') and 3 ('wilma') of @x.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


sttring
Deleted

Jan 17, 2001, 7:45 AM

Post #3 of 8 (760 views)
Re: slices [In reply to] Can't Post

I understand all of that. But my question was, how are these two the same? In the book the the first array example is supposed to be a shorter version of the second one, but they don't seem to be the same at all.

In the second array example the array doesn't get changed, but in the first one, take @who = ('fred', 'barney', 'betty', 'wilma')[2,3];. This is actually changing the value of @who. In the second example no array is changed.

What is the point of having fred and barney in it if the value of the array @who is immediately changed to @who =('betty' wilma'); Once the [2,3] is put at the end fred and barney no longer exist so what is the point of them in the first place?

Thanks,
sttring



japhy
Enthusiast

Jan 17, 2001, 7:49 AM

Post #4 of 8 (759 views)
Re: slices [In reply to] Can't Post

You said "In the second example no array is changed". What do you think this line does?


Code
@who = @x[2,3];

That clearly changes @who -- it sets it equal to a list of two elements.

The point of the example is to show how you can take arbitrary slices of lists:


Code
($day,$month,$year) = (localtime)[3,4,5]; 
# is the same as
@tmp = localtime;
($day,$month,$year) = @day[3,4,5];

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


sttring
Deleted

Jan 17, 2001, 8:22 AM

Post #5 of 8 (758 views)
Re: slices [In reply to] Can't Post

The line @who = @x[2,3]; doesn't change any array. Keep in mind that both examples are completely seperate from one another. In the second example the only array that exists is @x and it is not changed. @who is created from 2 values in @x. It did not exist previous to this point.

I understand the point of the examples, but don't understand how they are similar to one another. One is changing the existing array and the other one is creating a new array with slices of a different array. Is that correct?

Also still wondering by the first example why there is any reason to have fred and barney in the @who array if they are removed immediately with the [2,3]?

Thanks,
sttring



japhy
Enthusiast

Jan 17, 2001, 8:30 AM

Post #6 of 8 (757 views)
Re: slices [In reply to] Can't Post

Both examples, when regarded separately, CREATE the array @who, and fill it with two elements: 'betty' and 'wilma'. The ONLY difference is that the FIRST example uses a temporary list, while the SECOND example uses an array.

The example is used to show how taking a list slice is synonymous to an array slice.

"Also still wondering by the first example why there is any reason to have fred and barney in the @who array if they are removed immediately with the [2,3]?"

I ALREADY answered that -- I said that it was used to show how you can do slices on any arbitrary list. Perhaps they should have showed this instead:


Code
@who = (qw( fred barney betty wilma ))[$x,$y]; 

# is synonymous to

@x = qw( fred barney betty wilma );
@who = @x[$x,$y];

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


sttring
Deleted

Jan 17, 2001, 8:36 AM

Post #7 of 8 (756 views)
Re: slices [In reply to] Can't Post

Thank you. I was able to understand your second explanation alot better than your first and yes you are correct, I would have understood it better if it had been written @who = (qw( fred barney betty wilma ))[$x,$y];. Appreciate all the help.

Thanks,
sttring



rGeoffrey
User / Moderator

Jan 18, 2001, 7:41 PM

Post #8 of 8 (744 views)
synthetic variable [In reply to] Can't Post

At http://www.perl.com there is a series of articles about "red flags" in programs. They show things that should jump out at you as things to be improved.

One of the flags is the "synthetic variable". If @x is only used on these two lines then @x is a synthetic variable that exists only to connect the qw and @who...

@x = qw(fred barney betty wilma);
@who = @x[2,3];

And the recommended improvement would probably be to rewrite the two lines as...

@who = (qw(fred barney betty wilma))[2,3];


---
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


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

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