Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Perl: Create Arrays

#!/usr/bin/perl
@tab_digit=0..9;    # digit array
print @tab_digit,"n";
print "@tab_digitn";
@tab_str="a".."z";  # String array
print @tab_str,"n";
p

Perl: Calculate the factorial of a numbe

    my @factorial = (1);
    $MAX_FACT=9;
    for my $n (1..$MAX_FACT) {
        $factorial[$n] = $n * $factorial[$n-1];
    }
    print $factorial[5

Perl: list assignments

# defining a list
@tab = qw( January February March April May June );
print "@tab";
 # Assigning the list
( $10month , $12month ) = ( "October", "Dec

Perl: iterate over two lists simultaneously

# define two arrays
@tab_a = ( -1, 52, .5, 3.5 );
@tab_b = ( 0, 10, -5, 1 );
foreach $elem (@tab_a, @tab_b)    # iterate over the two arrays
{
    $e

Perl: Returning Arrays

sub myArray
{
    return (1, 2, 3, 4, 5, 6, 7, 8, 9);
}
for $index (0..9)
{
    $array[$index] = [&myArray];
}
print "$array[4][4]";

Perl: Iterating an Array Using the for Statement

#!/usr/bin/perl
@array = (1, 2, 3, 4, 5);
foreach $elem (@array)
{
    $elem--;   
}
# print result
print "@array";

Perl: add value to array at specified index

#!/usr/bin/perl
# define an array
@tab = ("January", "March", "May");
# print array values
print "@tab";
# set value at index 1
$tab[1]="February";
#

Perl: defining an array

#!/usr/bin/perl -w
use strict;
my @tab;                            #array
# defining the array
@tab = ('A', 'B', 'C');
# Concatenate the array
@tab =

Perl: How to Put one array into another array as an element

#!/usr/bin/perl -w
my @tab1 = (1, 2, 3, 4);       # define an array of 4 elements
my @tab2;                     # define an empty array
@tab2 = (@tab