Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here's an approach based on the observation of certain similarities (common prefix characters) in the data fields of interest in the three different types of data files. No discrimination between the three data file types is needed in the code.

Some notes of caution:

  • The code shown assumes the data being fed to it is valid. It is intended only as an example of a regex-based approach.
  • The code is critically dependent on the definition of the  $rx_oct regex. (I gave it this name because it superficially suggests an IP octet.) The OP shows only limited examples of this sub-field in the range (10 .. 13). You (brad_nov) will have to change this regex to reflect the real data – or else maybe reveal an actual spec!

>perl -wMstrict -le "my @records = ( '1|1212|34353|56575|||||4|~some~~pi=[],uid=[11]}~', '1|1212|34353|56575|||||4|~som~~390=,391=222,394~', '1|1212|34353|56575|||||4|~somedata~', ); ;; my $rx_oct = qr{ \d{1,3} }xms; my $rx_quint = qr{ $rx_oct (?: \. $rx_oct){4} }xms; ;; my $rx_dotted = qr{ (?<! \d) $rx_quint (?! \d) }xms; my $rx_int = qr{ \d+ }xms; ;; for my $record (@records) { print qq{'$record'}; my ($const, $var) = $record =~ m{ ( \A .+) \| ( .* \z) }xms; my (undef, $dotted, $int) = $var =~ m{ (\D) ($rx_dotted) .*? \1 ($rx_int) }xms; my $new_record = join '|', $const, $dotted, $int; print qq{'$new_record' \n}; } " '1|1212|34353|56575|||||4|~some~~pi=[],uid=[11]}~' '1|1212|34353|56575|||||4||11' '1|1212|34353|56575|||||4|~som~~390=,391=222,394~' '1|1212|34353|56575|||||4||222' '1|1212|34353|56575|||||4|~somedata~' '1|1212|34353|56575|||||4||3333'

Update: After playing around with this a bit and doing a little, um, testing, I think I would change the definition of  $rx_dotted as follows (change to final look-ahead):
    my $rx_dotted = qr{ (?<! \d) $rx_quint (?! [.\d]) }xms;
This change does not affect behavior for valid records.

In reply to Re: Split file using perl and regexp by AnomalousMonk
in thread Split file using perl and regexp by brad_nov

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [moritz]: ... "so there is no need to do anything"
    [LanX]: and the code is a bit of a mess
    [LanX]: moritz, no ... jsut copy one line of my code
    [moritz]: I'm still using the CB, because ambrus's #cbstream makes it bearable
    [LanX]: ... the rest is loaded from a node
    [holli]: the best course of action would be to transfer the data to something well tested. even if it isnt perl based
    [moritz]: but even if I had all the time in the world, I wouldn't spend much on perlmonks
    [LanX]: holli: LOL
    [LanX]: moritz: why ? family?
    [LanX]: or other language?

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (12)
    As of 2017-11-20 19:21 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (291 votes). Check out past polls.