aboutsummaryrefslogtreecommitdiff
path: root/challenge-024/ruben-westerberg/perl5/ch-2.pl
blob: 90fdecf9270a30d7bb4b789eafac1aea1d0bbbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env perl
use strict;
use warnings;
my %index;
for my $path (@ARGV) {
	open my $h, "<",$path;
	my $number=1;
	while (my $line=<$h>) {
		my @words=split /\W+/, $line;
		for my $w (@words) {
			$index{$w}{$path}{count}++;
			push @{$index{$w}{$path}{lines}}, $number;
		}
		$number++;
	}
	close $h;
}
for (sort keys %index) {
	print "Word: $_\n";
	my $e=$index{$_};
	for (sort keys %$e) {
		printf "\tFile: %s\t Count: %s\t Line: %s\n",$_,$$e{$_}{count},join " ", @{$$e{$_}{lines}};
	}
}