removing deplicate entries based on first column and keeping the latest entry, using Perl -
i need perl code following problem. in advance efforts.
my input in file in format: 'name' 'version number'
tech-sgla-zustand-ts.ini 1.1 tech-sgla-zustand-ts-feld.ini 1.1 tech-sgla-stamm-cds-feld.ini 1.1 tech-sgla-zustand-ts-feld.ini 1.2 tech-sgla-zustand-ts-feld.ini 1.4 tech-sgla-zustand-ts-feld.ini 1.3
i need in format (without blank lines in between): 'name' should unique maximum 'version number'
tech-sgla-zustand-ts.ini 1.1 tech-sgla-zustand-ts-feld.ini 1.4 tech-sgla-stamm-cds-feld.ini 1.1
you use :
my %inifiles = (); while (<>) { ($ini, $vers) = split / +/, $_; if (exists $inifiles{$ini}) { $inifiles{$ini} = $vers if ($inifiles{$ini} < $vers); } else { $inifiles{$ini} = $vers } } while (my ($k,$v) = each %inifiles) { print "$k $v\n" }
or if input order important :
my @inis = (); %inifiles = (); while (<>) { ($ini, $vers) = split / +/, $_; if (exists $inifiles{$ini}) { $inifiles{$ini} = $vers if ($inifiles{$ini} < $vers); } else { push @inis, $ini; $inifiles{$ini} = $vers } } foreach (@inis) { print "$_ $inifiles{$_}\n" }
Comments
Post a Comment