preface 
 
  Look_up Engine  
 
  email how to  
 
  counter  
 
  regex  
 
  search engine  
 
  home  
 
  ask us  
 

 

Perl scripting


Applications


OO Perl


Perl & Databases


Search a word:
   



© The scientific sentence. 2010

Perl Associative arrays hash
Associative arrays

1. Associative arrays

We access the element of an array by indexes. For the array: @books = ("Physics", "Mathematics", "Chemistry", "H2o", "Computer Science", 1010101); The first element of this array is $books[0]. The second element is $books[1], and so on. With Perl, we can create arrays which the elements are accessed by string. These arrays are called associative arrays. Example: ------- %books = ("Physics", "Mechanics","Mathematics", 123,"Chemistry", "H2o", "Computer Science", 1010101); @discipline = %books; for ($i=0; $i<8;++$i) { print "$discipline[$i]\n"; } for ($i=0; $i<8;++$i) { print "$disciplines[$i]\n"; } outputs: Chemistry H2o Computer Science 1010101 Physics Mechanics Mathematics 123 Now, with the associative arrays $books{"Physics"}; print $books{"Physics"}; $books{"Mathematics"}; print "$books{'Mathematics'}\n"; $books{"Chemistry"}; print $books{"Chemistry"}; $books{"Computer Science"}; print "$books{'Computer Science'}\n"; outputs: Mechanics123 H2o1010101 That is for each key, we have its value Now with a list array @disciplines = %books; for ($i=0; $i<8;++$i) { print "$disciplines[$i]\n"; } ouputs: Chemistry H2o Computer Science 1010101 Physics Mechanics Mathematics 123 foreach $book (keys %books) { print "the key is $book\n"; } foreach $book (values %books) { print "the value is $book\n"; } ouputs: the key is Chemistry the key is Computer Science the key is Physics the key is Mathematics the value is H2o the value is 1010101 the value is Mechanics the value is 123

2. Arrays related functions:

2.1. scalar

To get how many elements are in an array, use scalar my @books = ("Physics","Mathematics","Chemistry", "Geography"); my $elements = scalar(@books); will return 4

2.2. shift(@array)

To remove one element from the beginning, we use the "shift ()" function Example: my @letters = qw ( a b c d ); shift(@letters); will give: b c d

2.3. unshift( @array, LIST)

To add elements to the BEGINNING of an array. Example: my @letters = qw ( a b c d ); unshift(@letters, 'x'); will give: x a b c d

2.4. sort(@array)

The function sort() sorts an array alphabetically my @digits = ( 45, 56, 12, 3, 235, 567 ); my @sorted_digits = sort(@digits); print @sorted_digits; will sort them by considering them as characters. Use instead: my @digits = ( 45, 56, 12, 3, 235, 567 ); my @sorted_digits = sort {$a<=>$b} (@digits); print @sorted_digits; will sort the array .

2.5. reverse(@array)

The reverse() function returns an array in reverse order.

2.6. splice(@array)

We use splice() to add or remove elements into or out of an array. The syntax is: splice ( ARRAY , OFFSET , LENGTH , LIST ); my @digits = ( 45, 56, 12, 3, 235, 567 ); splice(@digits, 1, 0, '777'); warn join(" ", @digits); outputs: 45 777 56 12 3 235 567 at C:\www\PERL\test.pl line 4.
  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2007. All rights reserved.