Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Perl5-tut] Perl Intro: Scalars, Arrays, Hashes and Refs
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Tue Sep 20, 2005 1:13 am   Post subject: [Perl5-tut] Perl Intro: Scalars, Arrays, Hashes and Refs

As rizzix has graced our humble site with some Perl code, I thought it would be good to have some basic intro tutorials. Smile

Variables

Unlike most popular programming languages, Perl stores data in "variables". Wink

Variables in Perl takes four basic forms: scalars, arrays, hashes, and references.

Scalars

Scalars are the most basic type of variables. Numbers and strings are both scalars.

As far as syntax goes, the names of scalar variables look much like those in other programming languages. However, unlike many languages, the names begin with the $ "sigil", which denotes them as scalars.

Some quick examples:

Perl:
$an_integer = 42;
$a_string = "hello";
$another_string = 'world';
$a_float = 32.7;


Arrays

Arrays are a convenient variable for storing several values. Unlike many other languages, Perl's arrays do not have a set size. They can automatically resize as necessary.

An array is created like so (among other techniques):

Perl:
@names = ("Bob", "John", "Steve");


Here you can see the use of the @ sigil to denote an array.

Arrays are indexed from zero, and access to an element of an array looks like:

Perl:
$bobs_name = $names[0];


So, why did the sigil in front of "names" change? Well, that's a source of much controversey in the Perl community, but it is the way it is. The rationale is that each element of an array is a scalar, and scalars use a $ sigil.

Perhaps the most common operations on an array are the previously mentioned access, and "push". The "push" subroutine appends a new value onto the end of an array.

Perl:
@names = ();
push @names, "Neville";


Hashes

Arrays are indexed by numbers, but hashes are indexed by strings.

Let's say we want to store student grades by student names.

Perl:
%grades = ("Bob" => 86, "John" => 93);


To access an element in the hash, we can simply:

Perl:
$bobs_grade = $grades{"Bob"};


As with arrays, when we access an individual element, the % sigil becomes $ to indicate that all elements of a hash are scalars.

Adding entries to a hash is easy enough.

Perl:
$grades{"Eunice"} = 65;


References

We can easily gain a reference to any variable using the backslash operator.

Perl:
$an_integer = 42;
@array = (42, 37, 89)
%hash = ("foo" => "hello", "bar" => "world");

$int_ref = \$an_integer;
$array_ref = \@array;
$hash_ref = \%hash;


Where does this really become useful?

Well, let's look at that grades example again. What if we want each student to have multiple grades? Well, an array would do quite nicely.

Perl:
%grades = ("Bob" => (42, 86, 78), "John" => (93, 54));


But, that doesn't work, because as far as Perl is concerned, that looks like:

Perl:
%grades = ("Bob" => 42, 86, 78, "John" => 93, 54);


Which looks like:

Perl:
%grades = ("Bob" => 42, 86 => 78, "John" => 93, 54);


Which makes no sense.

But hashes and arrays can store scalars, and references are seen as essentially a type of scalar, so...

Perl:
%grades = ("Bob" => [42, 86, 78], "John" => [93, 54]);


The square bracket syntax is just a shorthand for creating an array reference. Similarly, curly brackets are shorthand for creating hash references.

So, how do we treat a reference like the original data type?

Perl:
@johns_grades = @{$grades{"John"}};


Accessing a particular grade could look like:

Perl:
$one_of_johns_grades = @{$grades{"John"}}[1];


But shorthand for that would be:

Perl:
$one_of_johns_grades = $grades{"John"}->[1];


Similar forms work for scalar references and hash references.
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Tue Sep 20, 2005 6:25 am   Post subject: (No subject)

Sort of a philosophical question, but what is the difference between Perl's "array" and, say, Python's "List"? I thought the distinction was that lists were resizable while arrays were not -- if Perl's array is resizable, how is it not a list?
rizzix




PostPosted: Tue Sep 20, 2005 11:27 am   Post subject: (No subject)

perl also has a list.. but u can't store the list as.. umm.. welll a list... u have to store it as an array or a hashtable.

python's lists and perl's arrays are similar except that perl does not have a list generator syntax. perl's lists are more like a really long tuple.. eg:
Perl:
@my_arr = (1,2,3,4,5,6);
here the (1,2,3,4,5,6) is actually a list and it's stored as an array.

But that's basically an array initialization syntax right? how could that possibly be a list??

well cuz u can do stuff like this as well
Perl:
@sub_arr = (1,2,3,4,5,6)[3,2..4];
ahha! so it's an array literal then? no cuz u can also do this
Perl:
($a, $b) = ($b, $a)
as well as this
Perl:
%my_hash = (abc => 'abc', numbers => 123);


so in perl we just call it a list.
rizzix




PostPosted: Tue Sep 20, 2005 11:37 am   Post subject: Re: [Perl5-tut] Perl Intro: Scalars, Arrays, Hashes and Refs

wtd wrote:
But shorthand for that would be:

Perl:
$one_of_johns_grades = $grades{"John"}->[1];


Similar forms work for scalar references and hash references.


you forgot to mention.. you can simply do this instead:
Perl:
$one_of_johns_grades = $grades{"John"}[1];
since the -> is implied
wtd




PostPosted: Tue Sep 20, 2005 11:40 am   Post subject: (No subject)

Indeed. The concept of "context" is very powerful in Perl.

Consider the difference between "list" and "scalar" context to get the length of an array.

Perl:
@grades = (54, 67, 32, 98, 87, 67, 12);
$number_of_grades = @grades;


Of course this is an implicit trasition to scalar context. We can make the transition explicit.

Perl:
@grades = (54, 67, 32, 98, 87, 67, 12);
$number_of_grades = scalar @grades;
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: