Thread Subroutines (4 answers)
Opened by mikey_b at 2010-03-28 15:56

Gast wer
 2010-03-28 17:31
#135398 #135398
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/local/bin/perl
use strict;
use warnings;

my $file_an=shift(@ARGV);
my @files_in=@ARGV;

my %hash = read_an($file_an);
wort_zu_zahl($_,%hash) for(@files_in);



sub read_an{
  my $file=shift;
  my %hash=();

  open(my $anfh, '<', $file) or die ("ERROR open $file ($!)\n");
  while (my $line = <$anfh>){
    chomp $line;
    my ($k, $v) = split /\s+/, $line;
    $hash{$k} = $v;
  }
  close($anfh);

  return %hash;
}


sub wort_zu_zahl{
  my $file=shift;
  print "FILE: $file\n";
  my %hash=@_;

  open(my $infh, '<', $file ) or die("ERROR open $file ($!)\n");
  while(my $line= <$infh> ){
    my %local = ();
    chomp $line;
    my @words = split /\s+/, $line;
    $local{$_} += $hash{$_} for @words;
    print join(",", map{$local{$_} || 0}keys(%hash) )."\n";
  }
  close($infh);
}

Last edited: 2010-03-28 17:41:21 +0200 (CEST)

View full thread Subroutines