|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Perl assignment and array
Hello, Im having a mental blank...
can someone, describe what his code does in lamens terms Code:
$upper=$data[@data/2]; im having a blank the data means... the @data means. im having trouble moving on until i work out what this code does |
|
#2
|
||||
|
||||
|
Code:
#!/usr/bin/perl use strict; use warnings; my @data = qw/one two three four five six seven eight nine ten/; my $num = @data/2; print "N: $num\n"; my $upper = $data[@data/2]; print "$upper\n"; Consider the @data/2 to be a scalar value from @data: the number of elements in the list. Divide by two and get a number. That value is the element chosen (automatically converted to an integer). Why the value chosen is named 'upper', I can't say. |
|
#3
|
|||
|
|||
|
thanks, what I dont understand, is what is the $data, and how does it work with the [@data/2]
im trying to work out the algorithm ![]() i get the scalar array bit, just not what the $data at the front means, lol.. thanks Last edited by drjonesuk : April 24th, 2008 at 05:34 AM. |
|
#4
|
||||
|
||||
|
$data[$num] is an element of the array @data.
Code:
@data = ('some', 'values');
print $data[1]; # displays 'values'
print $data[4+1-5]; # displays 'some'
|
|
#5
|
|||
|
|||
|
gee im dumb, i knew it was basic... thanks...
back to reading my lovely perl book ![]() |
|
#6
|
||||
|
||||
|
It's a unusual syntax for many.
@data is the whole array. %data is the whole hash. Larry Wall thought that it would be a good idea to indicate a single element in those list using a scalar notation: $data[1]; $data{'one'}; Apparantly, it's confusing to many people, and the community must have wanted a change, because in perl 6 the notation will be consistent. @data[1] will be a single element. |
|
#7
|
|||
|
|||
|
why thank you...
thanks keith... it is a bit confusing, the book is teaching me the old method.. a $inter[0] = an array.. Ill have to upgrade my perl.. im sure a good text editor can change ever instance in a old perl program or you could write a good substitution methods/// ![]() |
|
#8
|
||||
|
||||
|
The current stable is 5.10. Perl 6 is a ways off yet.
![]() There's a lot of recent progress though, maybe by the end of the year. Sorry, I didn't want to mislead you, just wanted to let you know that you are not alone in finding the current syntax confusing. Last edited by keath : April 24th, 2008 at 06:06 AM. |
|
#9
|
|||
|
|||
|
Quote:
... a binary search?
__________________
"Plagiarize the code of others, I say!" That's about the worst you can do. It's the fast way to make a very bad coder out of you. It's important to understand what you are doing. Just asking what you should do, and blindly copying that if you would have gotten an unambigious answer is very, very bad. -- Abigail |
|
#10
|
||||
|
||||
|
Quote:
I'm not sure if you understand or not. $inter[0] is a scalar, not an array. @inter would be the array that stores the list of scalars or strings, [0] would be the first scalar/string in the list. The [n] notation indicates the position of the scalar/string in the array, but $inter[0] is just a scalar like $foo is. |
|
#11
|
|||
|
|||
|
I get you, it calls variables from an array in the scalar context..
i hope i'm right ![]() |
|
#12
|
||||
|
||||
|
Quote:
Yes, that is exactly right. There are two contexts for variables, list and scalar, and the context in which you use them is important. For example, this is list context: @inter[0] it will return the same thing as: $inter[0] but in list context you can do this: @inter[4,2,7,0] which will return those elements of the array in the order they are listed. But that would not work in scalar context. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Perl assignment and array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|