Arrays perl

From Teknologisk videncenter
Revision as of 05:15, 8 July 2018 by Heth (talk | contribs) (Multidimentionelle arrays)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Referencer til arrays

Overføre array til subroutine

Overfør en reference til arrayet. (Subroutinen får adressen på arrayet og arbejder på det originale Overfør reference til subroutine. Eksempel.

#Opret et array
my @array = qw(A B C D E F G H I J);
#--------------------------------------------
print "Det originale array\n";
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}
#--------------------------------------------

mercantec(\@array);

#--------------------------------------------
print "\nEfter kald til subroutine mercantec\n";
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}
#--------------------------------------------


sub mercantec() {
my $a = shift;
print "\nsub mercantec har modtaget\n";
  for ( my $i=0; $i <= $#$a ;$i++ ) {
	print $i,":",$a->[$i]," ";
  }
$a->[2] = Z;  # Ændre element 2 (-> betyder reference til)
}

Indsætte og fjerne elementer i et array

Eksmepler på pop,push,shift,unshift og splice herunder

my @array = qw(A B C D E F G H I J);

#--------------------------------------------
print "Det originale array\n";
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}

#--------------------------------------------
print "\n\nVi ønsker at tilføje et element til sidst i arrayet.\n";
push(@array,"K");
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}

#--------------------------------------------
print "\n\nVi ønsker at tilføje et element i starten af arrayet.\n";
unshift(@array,"=");
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}

#--------------------------------------------
print "\n\nVi ønsker at fjerne det første element.\n";
shift(@array);
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}

#--------------------------------------------
print "\n\nVi ønsker at fjerne det sidste element.\n";
pop(@array);
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}

#--------------------------------------------
print "\n\nVi ønsker at tilføje et element mellem 3 og 4.\n";
splice(@array,3,0,"*");
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}

#--------------------------------------------
print "\n\nVi ønsker at fjerne element 3 og 4.\n";
splice(@array,3,2);
for ( my $i=0; $i <= $#array ;$i++ ) {
	print $i,":",$array[$i]," ";
}
#-----------------------------------------

Anonyme arrays

Opret arrays on the fly.

# Anonyme arrays

my @array;

$array[0] = ["Abe","Menneske","Ko"];
$array[1] = ["banan","agurk","kartoffel"];
$array[2] = ["Flue","insekt","bi"];

printf("Array rows = %i and coloums = %i\n", scalar @array, scalar @{$array[0]} );
foreach (@{$array[1]}) {
	print "$_\n";
}

Multidimentionelle arrays

my @a = (  [1, 3, 5, 7, 9] ,
           [2, 4, 6, 8],
           [2, 4, 6, 8],
        [22, 24],
        [26,78,89],
           [28, 30, 32]  );

print "Size of a = ", $#a,"\n";
my $cou = $#a;
print "Size of a = ", $#{$a [4]},"\n";

Prints

Size of a = 5
Size of a = 2

References to arrays

Selvforklarende eksempel herunder

#!/usr/bin/perl
use Data::Dumper qw(Dumper);



sub j {
 my $a = shift;
 $a->[1][0] = "Hasselnød";
}

#MWAIN

my @arr= (  ["Appelsin","Banan"],
            ["Kokosnød","Agurk"],
            ["Kokosnød","Agurk"],
            ["Æble","Peberfrugt"] );
print Dumper \@arr;
 $arr[1][1] = "Aubergine";
j(\@arr);
print Dumper \@arr;
printf(" %s  %s \n",$arr[0][1],$arr[0][1]);
printf("Size = %i\n",$#arr);
printf("Rows: %i, col: %i\n", scalar @arr, scalar @{$arr[0]});