Hello ArifS,

It looks like you data lines are composed of fields separated by hyphens. So I would begin with this kind of structure:

while (my $file = <DATA>) { chomp $file; my @old = split /-/, $file; my @new; for my $old_field (@old) { # analyse $old_field # construct $new_field push @new, $new_field; } print join('-', @new), "\n"; }

This is conceptual only. The mechanics are complicated by the requirement that fields do not appear in a fixed order within the data. For example, in the line:

1lc-2621s-RT02-9FL

the “device” field RT02 preceeds the “floor” field 9FL, whereas in the line:

10lc-7206-3FL-SW02

the order of these two fields is reversed. To handle this, you need a more flexible implementation. Here is one such approach:

#! perl use strict; use warnings; my (@old, @new); while (my $file = <DATA>) { chomp $file; @old = split /-/, $file; @new = (); posA () or next; dv () or next; device(); floor (); state (); print join('-', @old, @new), "\n"; } sub posA { my $field = shift @old; if ($field =~ /^(\d*)(lc|pp)$/i) { my $num = (length $1 == 0) ? '#' : (length $1 == 1) ? '0' . $1 : $1; push @new, $num . uc($2); return 1; } warn "Malformed posA: $field\n"; return 0; } sub dv { my $field = shift @old; if ($field =~ /^(\d+)([a-z]?)$/i) { my $code = (length $2 == 0) ? '#' : $2; push @new, $1 . uc($code); return 1; } warn "Malformed dv: $field\n"; return 0; } sub device { for my $i (0 .. $#old) { if ($old[$i] =~ /^(?:rt|fw|sw)\d+$/i) { push @new, uc(splice @old, $i, 1); return; } } push @new, 'DEVICE#'; } sub floor { for my $i (0 .. $#old) { if ($old[$i] =~ /^(\d+)fl$/i) { splice(@old, $i, 1); push @new, (length $1 == 1 ? '0' : '') . "$1FL"; return; } } push @new, '#FL'; } sub state { for my $i (0 .. $#old) { if ($old[$i] =~ /^[a-z]{2}$/i) { push @new, uc(splice @old, $i, 1); return; } } push @new, 'STATE'; } __DATA__ 1lc-2621s-RT02-9FL 2pp-3524-7FL PP-800c-RT1-TX 10lc-7206-3FL-SW02-ny LC-6509r-SW1

Output:

17:20 >perl 1051_SoPW.pl 01LC-2621S-RT02-09FL-STATE 02PP-3524#-DEVICE#-07FL-STATE #PP-800C-RT1-#FL-TX 10LC-7206#-SW02-03FL-NY #LC-6509R-SW1-#FL-STATE 17:20 >

Note that there are mismatches between the data you show and the result you expect to get. For example, LC-6509r-SW1 is supposed to transform into #LC-6509R-DEVICE#-#FL-GA, but (1) the device SW1 should appear unchanged, and (2) the state GA appears from nowhere! In programming, nailing down and documenting the requirements is often half the battle, and failure to do so leads inevitably to frustration, wasted effort, and dissatisfied clients. :-(

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Regex renaming by Athanasius
in thread Regex renaming by ArifS

Title:
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, details, 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, summary, 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.