Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Perl has three basic datatypes: scalars, arrays, and hashes.

Scalars

Scalars are the most basic type of data in Perl they can represent a number(int, float, whatever) or a string of text(we won't tell you about scalars as references just yet). Here are some examples of scalar assignments:
$a=5; $b="five"; $c=5.0

One important thing to take note of is that since scalars can be either numbers or strings you need to test for equality in different ways. For numbers use == and != to test for equality and nonequality. For strings you use eq and ne to test for the same things. Here's an example: (Note scalar variables begin with an $)
$a="5.0"; # set up our variables $b="5"; # # to the end of a line is a comment in perl print "Are these variables equal as numbers? ",$a==$b,"\n"; print "Are the variables equal as strings? ",$a eq $b,"\n"; print "These variables are equal as strings\n" if($a eq $b); print "These variables are equal numerically\n" if($a==$b);


If you ran this you would see a 1 is placed in the place of $a==$b. When we compare them with a numerical comparison they are processed as numbers and therefore 5.0 and 5 are equal and it returns a 1 which means true in Perl. $a eq $b is replaced with nothing in the print statement because as strings the two variables are not equivalent. This is equivalent to false in Perl along with 0 and a few other things. For information about these type of things check What is true and false in Perl?

For more information on each of these types of scalars check:
Integer Literals in Perl
Float Literals in Perl
Strings in Perl

Functions for SCALARs or strings
chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack, q/STRING/, qq/STRING/, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///

Arrays

Arrays are basically a collection of scalars stored next to each other and accessed by indices from zero to the number of elements minus one. Here are examples of some arrays in action. Note: when we're referring to an entire array we use the @ at the beginning of its name. If we're referring to only one of its elements(which is a scalar) we use a $.
@a=(1,2,3); @simpsonsfamily=("homer","marge","lisa","maggie","bart");

Arrays can store either strings or numbers or both. Now lets see how we can get at an individual element of an array.
$a[0]; #This returns the first element in @a which is 1
$simpsonsfamily[4]; #This returns the fifth element which is bart
$a[3]=4; #This sets the 4th element in @a to 4.

One nice thing about arrays in Perl is that you don't have to set the size of them when you start so they will grow for you when you need them to. To find the size of an array you can you can do scalar(@a) which would return 3 originally and 4 after $a[4] was set to 4; You can get at the highest index by using the variable $# with the arrayname attached to the end. For example $#simpsonsfamily would be equal to 4.

Some other functions I will quickly mention are push and reverse. push is followed by the array you want to add to, and then a value or list of values that you want added to the end. It would look something like:
push @array, $value; #puts $value onto the end of @array.

The reverse function simply takes a list or array and reverses it. For example, to reverse the order of an array permanently you would just type something like:
@array=reverse @array;


Functions for real @ARRAYs
pop, push, shift, splice, unshift

Functions for list data
grep, join, map, qw/STRING/, reverse, sort, unpack

Hashes

Hashes are collections of scalars like arrays only they have indices or keys which are strings instead of integers. Hash variables are named with a % followed by at least one letter and then maybe a mix of some more numbers, letters, and underscores. Key and value are two important hash terms. The key is what you use to look up a value. The key is like the index in an array and the value is like the data stored there. Hashes can also be thought of as an array filled with key value pairs as you will soon see. Now we'll show you how you can initialize and access elements of an array.
@array=("key1","value1","key2","value2"); #an array filled with key +value pairs %hash1=("key1"=>"value1","key2"=>"value2"); #one way to initialize a h +ash (key=>value,key2=>value2,..) %hash2=@array; #making %hash2 out of a co +llection of key value pairs in an array $hash3{"key1"}="value1"; #creates key of "key1" and + value of "value1" $hash3{"key2"}="value2";

Now %hash1, %hash2, %hash3 all contain the very same keys and values.

Functions for real %HASHes
delete, each, exists, keys, values

Some other things you might want to check out are:
Control statements and looping
Some things that will make your life easier as a Perl coder

In reply to the basic datatypes, three by root

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (14)
    As of 2014-04-18 21:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (472 votes), past polls