Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-24-2003, 01:55 PM   #1 (permalink)
tin_man
Registered User
 
tin_man's Avatar
 
Join Date: May 2002
Posts: 4
tin_man is on a distinguished road
Send a message via AIM to tin_man Send a message via Yahoo to tin_man
Diffrent storage types in perl

A listing of the various varaible types in perl

One of the best(or worst, depending on your viewpoint)things about perl is the lack of typing as far as variables go, lets look at an example:

Say you need to store a floating point number such as 1.09

Code:
$number = 1.09;
that's it, no typecasting at all, just give it a name starting with $, and it's stored.
this is called a scalar variable, and can store one thing of any type, numbers, text, whatever.

Code:
$number = 1.09;


$whole_number = 15;


$text = "Hello World";
perl dosn't care what you put into a scalar, it just stores it and moves on.

Another storage type in perl is the array

If you are at all familar with anyother programming languages, arrays are old stuff, but lets review(or look at for the first time, depending on your skill level)

arrays in perl all start with the @ symbol

Code:
@stuff = ("book", "table", "lamp");
elements in an array are accesed like this:


Code:
print stuff[0];
this would output "book"
remember that arrays always start from 0 and go up, it is common to forget that leading to off-by-one errors.
arrays in perl are not static, if you need to add something to an array, go ahead, perl won't mind one bit
lets add something to our sample array

Code:
push @stuff, "chair";


print @stuff;
this would output "book table lamp chair" just like you would expect
another thing to remember is that perl dosn't care about if what you are storing is a string or numbers or whatever.

Hashes
the last major storage type in perl is the hash

Hashes are unordered arrays indexed by keywords, in other words a hash is like a mixed bag of things, no real order, this is a hashes strength though, because unlike arrays, hashes stay fast no matter how many variables are added to it.
here is an example of a hash

Code:
%days=(


    "Sun" => "Sunday,


    "Mon" => "Monday",


    "Tue" => "Tuesday",


    "Wed" => "Wednesday",


    "Thu" => "Thursday",


    "Fri" => "Friday",


    "Sat" = > "Saturday"


       );
to access a element in a hash, you do this

Code:
print $days("Mon");
This will print Monday, just like you would expect
note that when you access an individual element in a hash, you use a $ instead of a %, this is easy to forget, but will cause an error.
tin_man is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how come i dont hear much about perl Apodysophilia All Other Coding Languages 2 12-16-2003 10:05 AM


All times are GMT -8. The time now is 04:45 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting