Thread smartmatch (11 answers)
Opened by torsten at 2011-05-16 13:44

topeg
 2011-05-16 14:40
#148685 #148685
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Man muss schon vorsichtig sein:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

my $keys=[0..100];

my %hash1=hash($keys);
my %hash2=hash($keys);

print @{[values %hash1]} ~~ @{[values %hash2]}?'OK':'FAIL',"\n";
print @{[map{$hash1{$_}}sort(keys(%hash1))]} ~~ @{[map{$hash2{$_}}sort(keys(%hash2))]}?'OK':'FAIL',"\n";

sub hash
{
  my $ref=shift;

  for ( reverse(0..$#$ref) )
  {
    my $pos=int(rand(@$ref));
    @$ref[$pos,$_]=@$ref[$_,$pos];
  }

  my %l;
  map{$l{$_}= sprintf('%02u',ord($_))}@$ref;
  return %l;
}

Kommt raus:
Code: (dl )
1
2
Fail
OK


Edit es hat auch so:
Code (perl): (dl )
@hash1{sort(keys(%hash1))} ~~ @hash2{sort(keys(%hash2))}

Last edited: 2011-05-16 14:55:54 +0200 (CEST)

View full thread smartmatch