Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
There are two things that make this much easier. One, is to iterate over the data twice, the first time to set up your relationships, the second time to report their existance. And two, rely solely on incrementing +1, as it is the easier of the relationships to calculate because of the special magic of ++ on strings.

You still must define the characteristics of your boundary conditions though. Ideally, we want the increment and decrement relationships to be one to one. What is the increment of "Bar999"? If it is "Bar1000", then your relationship is no longer 1 to 1 as there is also "Bar0999".

Anyway, here is an implementation without the boundary conditions fully defined. I've added two data entries for lines that do not match:
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
And the output is:
>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
- Miller

In reply to Re: searching for strings by wind
in thread searching for strings by steph_bow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-24 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found