Re: Removing duplicate IDs



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

.



Relevant Pages

  • Re: Removing duplicate IDs
    ... my %tags =; ... my @sorted_chars = sort { ... print OUTPUTFILE @result; ...
    (perl.beginners)
  • OT: Birthday, London, Friday
    ... You can see how the two discoveries were sort of simultaneous. ... underground map, and one of the allegedly realist ones it replaced; ... was the motto of Michelin and is the nickname of the ... The building has three large stained glass panels depicting ...
    (rec.arts.mystery)
  • Re: i got trapped in Wales because
    ... I've never caught her holding the map upside ... To be honest, I've never actually used a 'carbide lamp', but the ones ... meant for vehicles that I've seen lacked any sort of 'lighter' of their ... Paraffin lamps almost always need a match or something. ...
    (uk.people.support.depression)
  • Re: Spoilered for talk of religion
    ... Beats following someone else's map if you ask me - if you do that, ... around me', get on with things, complete a task, that sort of thing. ... That sort of gamble is a `near 100% gamble' as far ...
    (uk.people.support.depression)
  • Re: Finding, sharing and starting jam sessions
    ... offer which sort of music, let alone whether I can join in or not. ... Additionally, each post lists ... folkjam.org with a private map and forum. ... the domestic jams on the site in anything less than a year. ...
    (rec.music.folk)