in reply to Regex renaming

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,

Replies are listed 'Best First'.
Re^2: Regex renaming
by ArifS (Beadle) on Oct 14, 2014 at 12:46 UTC
    Sorry for the late reply, because of the weekend. Thank you for correcting me on the final output. I just gave it a try and it works great. Awesome! Thank you very much.