Re: Removing duplicate IDs
- From: "Xicheng Jia" <xicheng@xxxxxxxxx>
- Date: 11 May 2006 09:53:40 -0700
John W. Krahn wrote:
macromedia wrote:
Hi,
Hello,
I added your changes to my script but now I get no output. See code below:
Syntax: perl test.pl in.srt out.srt
#!/usr/local/bin/perl
require 5.000;
my %tags = ();
my %seen;
my $input = $ARGV[0];
my $output = $ARGV[1];
open (FILE, "< $input") or die "cannot open $input: $!\n";
open (OUTPUTFILE, "> $output");
chomp(my @lines = <FILE>);
my @chars = grep !$seen{$_->[1]}++,
map {
my ($id) = m{<a id=(\w+)>};
[ $_, $id, scalar $id =~ /^\d+$/ ];
}@lines;
map $_->[0],
sort {
$b->[2] <=> $a->[2]
or
( $a->[2] ? $a->[1] <=> $b->[1] : $a->[1] cmp $b->[1] )
or
$a->[0] cmp $b->[0]
grep !$seen{$_->[1]}++,
map {
my ( $id ) = /<a id=(\w+)>/;
[ $_, $id, scalar $id =~ /^\d+$/ ];
print OUTPUTFILE ;
close OUTPUTFILE;
close FILE;
If you had made the changes I indicated then your program should look
something like:
#!/usr/local/bin/perl
require 5.000;
my %tags = ();
my %seen;
my $input = $ARGV[0];
my $output = $ARGV[1];
open (FILE, "< $input") or die "cannot open $input: $!\n";
open (OUTPUTFILE, "> $output") or die "cannot open $output: $!\n";
my @chars = grep !$seen{$_->[1]}++, map {
my ($id) = m{<a id=(\w+)>};
[ $_, $id, scalar $id =~ /^\d+$/ ];
} @lines;
my @sorted_chars = sort {
$b->[2] <=> $a->[2]
or
($a->[2] ? $a->[1] <=> $b->[1] : $a->[1] cmp $b->[1])
or
$a->[0] cmp $b->[0]
} @chars;
my @result = map { $_->[0] } @sorted_chars;
print OUTPUTFILE @result;
close OUTPUTFILE;
close FILE;
his numbers look like hexadecimals, so maybe he can just use function
hex() to sort his hash keys.
_____________________
use strict;
use warnings;
my %seen = ();
while (my $line = <DATA>) {
chomp($line);
if ($line =~ /<a id=([\da-f]+)>/) {
my $key = $1;
$seen{ $key } = $line;
}
}
print map { "$seen{$_->[0]}\n" }
sort { $a->[1] <=> $b->[1] }
map { [ $_, hex($_) ] }
keys %seen;
________________________________
XC
John
--
use Perl;
program
fulfillment
.
- References:
- Re: Removing duplicate IDs
- From: John W. Krahn
- Re: Removing duplicate IDs
- Prev by Date: Re: Confused...
- Next by Date: date & time from server problem/question
- Previous by thread: Re: Removing duplicate IDs
- Next by thread: Obtaining input from an external program. Why doesn't this work?
- Index(es):
Relevant Pages
|