Perl Tutorial : Perl data types
Perl Tutorial : Perl Operators
Perl Tutorial : Perl loops
Perl Tutorial : Perl Conditionals
Perl Tutorial : Perl Subroutines
Perl Tutorial : Perl regex
Perl Tutorial : Perl Files
Perl Tutorial : Perl & Databases
Perl Tutorial : Perl OOPs
Perl Tutorial : Perl signals
Perl Tutorial : Perl threads
Perl Tutorial : Perl Debugging

Introcution to Perl CGI

Introduction to modperl
Home >> perldatatypes >>

Assigning values to hash (or associative arrays/lookup tables)


  • hash tables consist of key/value pairs
  • every key is followed by a value values can be assigned to hash tables as
    %us_states=
    ("California","Sacramento","Wisconsin","Madison","New York","Albany");
    
    We can also use => operator to identify the key to the left, and the value to the right; if the => operator encounters bare words in key positions, they will be automatically quoted (note "New York", however, which consists of two words and MUST be quoted
    %us_states=
    (California=>"Sacramento",Wisconsin=>"Madison","New York"=>"Albany");
    

    In above example California is key and Sacromento is value.
    Similarily Wisconsin is key and Madison is value.

    Printing:
    print "Capital of California is " . $us_states{"California"} . "\n\n";
    
    printing all values(using for loop):
    #!/usr/bin/perl
    %states=
    (California=>"Sacramento",Wisconsin=>"Madison","New York"=>"Albany");
    foreach my $keys(keys %us_states)
    {
      print "KEY:$keys VALUE:$us_states{$keys}\n";
    }
     
    output
    KEY:Wisconsin VALUE:Madison
    KEY:New York VALUE:Albany
    KEY:California VALUE:Sacramento
    
    Note
  • Hashes have no internal order

    Hashes of hashes

    example...
    %companies_products = (
            google => {
            email   => "gmail",
        },
        microsoft => {
            email   => "hotmail",
            database      => "MSsql server",
            "wordprocessing" => "ms office",  
        },
        yahoo => {
            email   => "yahoo mail",
            chat      => "yahoo messenger",
        },
    );

  • Perl functions

    scalars and strings

    lc
    ord

    regex (regular expressions)

    arrays

    shift
    split

    More..

    eval
    Share |

    Perl @ARGV %ENV
    Perl Arrays
    Perl Data Types
    Perl list
    Perl references
    Perl Scalar data types
    Perl Special Variables

    Perl examples

  • How to use Data::Dumper?
  • How to parse config file?
  • Include external module in perl..
  • Download perl

    download stable version of perl
    Perl 5.10.1
    Perl interview Questions
    sed tutorial
    awk tutorial

    Join us on Google Groups

    Subscribe to perl
    Email:
    Visit this group
    Perl Jobs


    privacy | sitemap | disclaim | contact us
    © 2009 perlhome.com