in reply to searching for strings
And the output is:use Fcntl qw(SEEK_SET); use strict; # Calculate Relationships. # - Rely on increment, as it's the easier of the two to calculate. my %decrement; my %increment; # Synonymous with existance. my $start_of_DATA = tell DATA; while (<DATA>) { chomp; my $item = $_; # Note concerning parsing # - This regex requires that a prefix exist. if ($item =~ m{ (\w+) ( (?<!\d)\d+ | (?<![A-Z])[A-Z]+ ) \z}x) { my ($prefix, $suffix) = ($1, $2); # Note: this is the primary spot where there might be changes +in rules. # - What happens when the character ends in 'Z'? Currently # That would translate to 'AA'. # - What happens when number is 999? Currently that would tra +nslate # to '1000'. # Fix the rules here, and everything else will translate. (my $suffix_next = $suffix)++; my $item_next = $prefix . $suffix_next; $increment{$item} = $item_next; $decrement{$item_next} = $item; } else { die "Invalid data: $_"; } } # Reparse DATA seek(DATA, $start_of_DATA, SEEK_SET); while (<DATA>) { chomp; print; if ($increment{ $decrement{ $_ } }) { print ";$decrement{$_}"; } elsif ($increment{ $increment{ $_ } }) { print ";$increment{$_}"; } print "\n"; } __DATA__ AAA30 BBC5 SHT12H DAL33B BBC49 AAA31 BBC8 BBC3 DAL33A BBC6 SHT12G BBC50
- Miller>perl scratch.pl AAA30;AAA31 BBC5;BBC6 SHT12H;SHT12G DAL33B;DAL33A BBC49;BBC50 AAA31;AAA30 BBC8 BBC3 DAL33A;DAL33B BBC6;BBC5 SHT12G;SHT12H BBC50;BBC49
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching for strings
by wind (Priest) on Aug 10, 2007 at 08:32 UTC |