Difference between revisions of "Arrays perl"
From Teknologisk videncenter
m (→Overføre array til subroutine) |
m (→Multidimentionelle arrays) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | __TOC__ | ||
= Referencer til arrays = | = Referencer til arrays = | ||
== Overføre array til subroutine == | == Overføre array til subroutine == | ||
Line 87: | Line 88: | ||
} | } | ||
#----------------------------------------- | #----------------------------------------- | ||
+ | </source> | ||
+ | = Anonyme arrays = | ||
+ | Opret arrays on the fly. | ||
+ | <source lang=perl> | ||
+ | # 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"; | ||
+ | } | ||
+ | </source> | ||
+ | =Multidimentionelle arrays= | ||
+ | <source lang=perl> | ||
+ | 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"; | ||
+ | </source> | ||
+ | Prints | ||
+ | <source lang=text> | ||
+ | Size of a = 5 | ||
+ | Size of a = 2 | ||
+ | </source> | ||
+ | ==References to arrays== | ||
+ | Selvforklarende eksempel herunder | ||
+ | <source lang=perl> | ||
+ | #!/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]}); | ||
</source> | </source> | ||
[[Category:Programming]][[Category:Perl]] | [[Category:Programming]][[Category:Perl]] |
Latest revision as of 05:15, 8 July 2018
Contents
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]});