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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
my $jobFile = $_[0];
my $key =$_[1];
binmode STDOUT, ':utf8';
print "searching for key $key\n";
if (-e $jobFile){
my $propertyHashRef = &readFileIntoHash($jobFile,":");
my @keys = keys %{$propertyHashRef};
print "@keys\n";
my @values = values %{$propertyHashRef};
print "@values\n";
if (defined $propertyHashRef){
if (!exists $propertyHashRef{$key}){
print "key $key exists in file $jobFile with value ". $propertyHashRef->{$key} ."\n";
return $propertyHashRef->{$key};
}
else{
print "key: $key does not exist in file $jobFile\n";
}
}
else{
&log_this($customer,$ordername,"hash is not defined");
print "hash is not defined\n";
}
}
else{
&log_this($customer,$ordername,"cannot find file $jobFile");
print "cannot find file $jobFile\n";
}
print "no form id found in file $jobFile\n";
return undef;
sub readFileIntoHash{
my $inputFile = $_[0];
my $seperatorChar = $_[1];
if (! $seperatorChar){ $seperatorChar = "="}
&log_this($customer,$ordername,":using input file $inputFile") ;
my %exportHash;
if (-e $inputFile){
open (HASHFILE,$inputFile) || die "file $inputFile konnte nicht geoeffnet werden\n";
binmode(HASHFILE, ":encoding(UTF-8)");
%exportHash = map { chomp; split /\s*$seperatorChar\s*/, $_, 2; } <HASHFILE>;
#while (<HASHFILE>){
# print "$_\n";
#}
#
close(HASHFILE);
return \%exportHash;
}
else{
&log_this($customer,$ordername,":file $inputFile does not exist, returning undef");
return;
}
}
1 2 3 4 5 6 7 8 9
open (my $fh, '<:encoding(UTF-8)',$inputFile) || die "file $inputFile konnte nicht geoeffnet werden\n"; my $bom=1; %exportHash = map { chomp; s/^\xEF\xBB\xBF// if($bom); $bom=0; split /\s*$seperatorChar\s*/, $_, 2; } <$fh>; close($fh);
s/^\x{feff}// if $bom;
2011-06-10T08:05:20 crojay
Code: (dl )1
2
3
4
5
6
7
8if (!exists $propertyHashRef{$key}){
print "key $key exists in file $jobFile with value ". $propertyHashRef->{$key} ."\n";
return $propertyHashRef->{$key};
}
else{
print "key: $key does not exist in file $jobFile\n";
}
2011-06-10T08:48:50 pq2011-06-10T08:05:20 crojay
Code: (dl )1
2
3
4
5
6
7
8if (!exists $propertyHashRef{$key}){
print "key $key exists in file $jobFile with value ". $propertyHashRef->{$key} ."\n";
return $propertyHashRef->{$key};
}
else{
print "key: $key does not exist in file $jobFile\n";
}
kannst du den teil des codes mal erklären?
erstens verwendest du $propertyHashRef{$key} und $propertyHashRef->{$key}. das sind zwei verschiedene dinge, das weisst du schon, oder?
use strict <-- lesen!
zweitens:
if ! exists führt zu einem print "key $key exists"
wundert mich eigentlich nicht, dass der code nicht so funktioniert wie erwartet.
$propertyHashRef{$key}
$propertyHashRef->{$key}
if (!exists $propertyHashRef{$key})
2011-06-10T09:32:26 crojay
1 2 3 4 5 6 7
my %hash = ( a => 23 ); # zugriff auf die 23: print "$hash{a}\n"; my $hashref = { a => 23 }; # zugriff auf die 23: print "$hashref->{a}\n";