in reply to searching for strings
Definitely one of those things that appears on the surface to be trivial, but turns out to be much harder:
Here's one way
#! perl -slw use strict; use Data::Dump qw[ pp ]; my %hash; while( <DATA> ) { chomp; my( $pre, $temp ) = m[(^.+?)(\d+|\D)$]; $temp = $pre . ++$temp; if( exists $hash{ $_ } ) { push @{ $hash{ $_ } }, $_; } elsif( exists $hash{ $temp } ) { push @{ $hash { $temp } }, $_; } else { $hash{ $temp } = [ $_ ]; $hash{ $_ } = [ $_ ]; } } print join ';', @{ $_ } for grep{ @{ $_ } == 2 } values %hash; __DATA__ AAA30 BBC5 SHT12H DAL33B BBC49 AAA31 DAL33A BBC6 SHT12G BBC50
Output:
C:\test>junk DAL33B;DAL33A AAA30;AAA31 BBC49;BBC50 BBC5;BBC6 SHT12H;SHT12G
If you need the doubled-up output, just print them twice with the elements reversed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching for strings
by Limbic~Region (Chancellor) on Aug 06, 2007 at 12:48 UTC | |
by BrowserUk (Patriarch) on Aug 06, 2007 at 17:51 UTC | |
by shmem (Chancellor) on Aug 06, 2007 at 18:08 UTC |