Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Regex renaming

by ArifS (Beadle)
on Oct 09, 2014 at 18:23 UTC ( [id://1103327]=perlquestion: print w/replies, xml ) Need Help??

ArifS has asked for the wisdom of the Perl Monks concerning the following question:

Here is the code I am working with-
while (my $file = <DATA>) { $file =~ /(\d+)(lc|pp)/i or next; my $posA = uc "$1$2-"; # $file =~ /(sh|lic)/i or next; # my $posB = uc $1; $file =~ /(800|2621|3524|6509|7206)(s-|r-)/i or next; my $dv = uc "$1$2"; $file =~ /([a-z]+)(\d+)/i or next; my $loc = uc "$1$2"; print "$posA" or die; # print "$posB" or die; print "$dv" or die; print "$loc" or die; print "\n"; } __DATA__ 1lc-2621s-RT02-9FL 2pp-3524-7FL PP_800c_-RT1-TX 10lc-7206-3FL-SW02 LC-6509r-SW1
I am trying to get the following-

01LC-2621S-RT02-09FL-STATE
02PP-3524#-DEVICE#-07FL-STATE
#PP-800C-RT01-#FL-TX
10LC-7206#-SW02-03FL-NY
#LC-6509R-DEVICE#-#FL-GA

Device# if missing RT#, FW#, or SW# etc. STATE if missing 2 digits state info like, NY, FL etc. #FL, if missing floor#.

Please let me know

Replies are listed 'Best First'.
Re: Regex renaming
by GotToBTru (Prior) on Oct 09, 2014 at 19:51 UTC

    It's unclear what you are trying to do. In what case should STATE be appended? What are the rules for handling the initial digit? When should # be inserted?

    1 Peter 4:10
      Thank you for your suggestion. I have updated what I am looking for in the original post.
Re: Regex renaming
by AnomalousMonk (Archbishop) on Oct 09, 2014 at 20:34 UTC

    Further to GotToBTru's comments on the obscurity of the string transformations you're trying to achieve, one more thing puzzles me. There are several statements of the form
        print "$part_of_final_string" or next;
    that will output an incompletely transformed string and then go on to process the next input string if the print I/O operation fails. In this unlikely (and probably catastrophic) event, don't you really want to die with an appropriate error message rather than trying to press on? Just curious...

      That's a good suggestion. I have updated as you suggested.
Re: Regex renaming
by Athanasius (Archbishop) on Oct 11, 2014 at 07:30 UTC

    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:

    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,

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1103327]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-23 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found