Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 24th, 2008, 04:54 AM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
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

Reply With Quote
  #2  
Old April 24th, 2008, 05:29 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
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.

Reply With Quote
  #3  
Old April 24th, 2008, 05:32 AM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
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.

Reply With Quote
  #4  
Old April 24th, 2008, 05:35 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
$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'
 

Reply With Quote
  #5  
Old April 24th, 2008, 05:36 AM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
gee im dumb, i knew it was basic... thanks...

back to reading my lovely perl book

Reply With Quote
  #6  
Old April 24th, 2008, 05:42 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
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.

Reply With Quote
  #7  
Old April 24th, 2008, 05:46 AM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
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 method

s///


Reply With Quote
  #8  
Old April 24th, 2008, 06:04 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
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.

Reply With Quote
  #9  
Old April 24th, 2008, 07:32 AM
Clueless Newbie Clueless Newbie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 437 Clueless Newbie User rank is Second Lieutenant (5000 - 10000 Reputation Level)Clueless Newbie User rank is Second Lieutenant (5000 - 10000 Reputation Level)Clueless Newbie User rank is Second Lieutenant (5000 - 10000 Reputation Level)Clueless Newbie User rank is Second Lieutenant (5000 - 10000 Reputation Level)Clueless Newbie User rank is Second Lieutenant (5000 - 10000 Reputation Level)Clueless Newbie User rank is Second Lieutenant (5000 - 10000 Reputation Level)Clueless Newbie User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 1 h 4 m 59 sec
Reputation Power: 93
Quote:
Originally Posted by drjonesuk
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


... 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

Reply With Quote
  #10  
Old April 25th, 2008, 01:30 AM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 1,966 KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 3 h 4 m 48 sec
Reputation Power: 338
Quote:
Originally Posted by drjonesuk
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 method

s///



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.

Reply With Quote
  #11  
Old April 25th, 2008, 01:48 AM
drjonesuk drjonesuk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 145 drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level)drjonesuk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 37 m 23 sec
Reputation Power: 9
Send a message via ICQ to drjonesuk
I get you, it calls variables from an array in the scalar context..

i hope i'm right

Reply With Quote
  #12  
Old April 25th, 2008, 02:22 AM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 1,966 KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 3 h 4 m 48 sec
Reputation Power: 338
Quote:
Originally Posted by drjonesuk
I get you, it calls variables from an array in the scalar context..

i hope i'm right


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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Perl assignment and array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump