Re: getting duplicates in a list by item id
Re: getting duplicates in a list by item id
- Subject: Re: getting duplicates in a list by item id
- From: "Mark J. Reed" <email@hidden>
- Date: Sun, 26 Aug 2007 11:36:38 -0400
On 8/26/07, Mark J. Reed <email@hidden> wrote:
> Here's the complete Ruby program.
Well, that's the one I ran. To be somewhat more useful, this version
sorts the output by line number of first occurrence, and calls the
first line 1 instead of 0:
pathList = File.readlines('paths.txt')
pathPositions = {}
pathList.each_with_index do |path, position|
path.chomp!
(pathPositions[path] ||= []).push(position + 1)
end
pathPositions.sort { |a, b| a[1][0] <=> b[1][0] }.each do |path, positions|
puts "#{path} - #{positions.join(',')}"
end
And here's the equivalent Perl:
open(my $fileHandle, "<paths.txt");
chomp(my @pathList = <$fileHandle>);
close($fileHandle);
my %pathPositions;
for (my $position = 0; $position < @pathList; ++$position)
{
my $path = $pathList[$position];
push (@{$pathPositions{$path} ||= []}, $position + 1);
}
foreach my $path
(sort { $pathPositions{$a}[0] <=> $pathPositions{$b}[0] } keys %pathPositions)
{
my $positions = $pathPositions{$path};
print "$path - ", join(',', @$positions), "\n";
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden