Thread arrays vergleichen (23 answers)
Opened by blaise4714 at 2007-04-24 16:21

bloonix
 2007-04-25 12:57
#76192 #76192
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Okay, da es im IRC noch Unklarheiten gab...

Vodoo.pm
Code: (dl )
1
2
3
package Vodoo;
$[ = 20;
1;


test1.pl
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use strict;
use warnings;
use lib '.';
use Vodoo;

$[ = 0;

my @arrays1 = qw(w a f g b c j g r l);
my @arrays2 = qw(0 1 r 5 6 a l w f b g);

for my $cur (@arrays1) {
  for (my $i=0; $i<=$#arrays2; $i++) {
     print "$i - $cur kommt an Stelle $i im 2. Array vor.\n" if ($cur eq $arrays2[$i]);
  }
}


#> ./test1.pl
7 - w kommt an Stelle 7 im 2. Array vor.
5 - a kommt an Stelle 5 im 2. Array vor.
8 - f kommt an Stelle 8 im 2. Array vor.
10 - g kommt an Stelle 10 im 2. Array vor.
9 - b kommt an Stelle 9 im 2. Array vor.
10 - g kommt an Stelle 10 im 2. Array vor.
2 - r kommt an Stelle 2 im 2. Array vor.
6 - l kommt an Stelle 6 im 2. Array vor.


Das schaut also soweit ok aus, da $[ mit 0 kompiliert wird. Wenn aber
"use Vodoo;" nach "$[ = 0" eingebunden wird, passiert folgendes:

test2.pl
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use strict;
use warnings;
use lib '.';

$[ = 0;

use Vodoo;

my @arrays1 = qw(w a f g b c j g r l);
my @arrays2 = qw(0 1 r 5 6 a l w f b g);

for my $cur (@arrays1) {
  for (my $i=0; $i<=$#arrays2; $i++) {
     print "$i - $cur kommt an Stelle $i im 2. Array vor.\n" if ($cur eq $arrays2[$i]);
  }
}


#> test2.pl
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
16 - w kommt an Stelle 16 im 2. Array vor.
27 - w kommt an Stelle 27 im 2. Array vor.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.
Use of uninitialized value in string eq at ./test.pl line 15.


Und so weiter und so weiter...

Hier wurde in Vodoo.pm $[ auf 20 gesetzt und die for-Schleife beginnt
fix mit 0. Wie verhält man sich also, wenn irgendjemand in einem Modul
$[ verändert? Sollte man dann lieber die Laufvariable mit $[ initialisieren
oder auf das Modul verzichten?

Gruss,
opi\n\n

<!--EDIT|opi|1177491469-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread arrays vergleichen