Monica y Stefan
Leonardo, Daniela

HomeFotosLeonardoDanielaKalenderPerl

Tipps und Tricks mit mehreren Arrays

Elemente finden die in beiden Arrays vorhanden sind (intersection)

Intersection%count = ();
foreach $element (@A,@B)
  {
  $count{$element}++;
  }
foreach $element (keys %count)
  {
  if ($count{$element} > 1) { push @intersection,$element; }
  }

@A = (a 1 2 3 4);
@B = (a x y z);
@intersection = (a);

Alle Elemente aus beiden Arrays (union)

Union%count = ();
foreach $element (@A,@B)
  {
  $count{$element}++;
  }
foreach $element (keys %count)
  {
  push @union,$element;
  }

@A = (a 1 2 3 4);
@B = (a x y z);
@union = (1 2 3 4 a x y z);

Elemente finden die nur in @A vorhanden sind. (difference)

Difference@seen{@B} = ();
foreach (@A)
  {
  push (@difference, $_) unless exists $seen{$_};
  }

@A = (a 1 2 3 4);
@B = (a x y z);
@difference = (1 2 3 4);

Elemente aus 2 Arrays finden die nur einmal vorhanden sind (symmetrical difference)

Symmetrical Difference%count = ();
foreach $element (@A,@B)
  {
  $count{$element}++;
  }
foreach $element (keys %count)
  {
  if ($count{$element} <= 1) { push @difference,$element; }
  }

@A = (a 1 2 3 4);
@B = (a x y z);
@difference = (1 2 3 4 x y z);

 

Tipps und Tricks mit Arrays

zum Inhalt