Hi All,

Many thanks to Jay for his examples I have now written a perl script that is
munging a text file.  Eventually I will move the perl code into a function
inside a postgresql database.

I am processing inputs like the following:

815 HTPP Black 2in sq Border: RMFP025BK Size: 7'10" x 16' Tag:  None
3000 HTPP Black 4in sq Border:  WNY200BK Size:  17' x 50' Tag:  None
3000 HTPP Black 4in sq Border:  WNY200BK Size:  12' x 12'2" Tag:  None
Netform Lily Pad Net Size: 5' X 32' W & L Body Length:24'
250 HTPP Black 1in sq Border:  TW84NTBK Size:  9' x 25' Tag:  200' sec
1250 HTPP Yellow 2in sq Border: None Size: 6'1" x 12'7" Tag:  1855mm x 3840mm
Here is the code that I have written so far.

#!/usr/bin/env perl
use strict;
use warnings;

open(INFILE,  "input.txt")   or die "Can't open input.txt: $!";

while (<INFILE>) {     # assigns each line in turn to $_
   my $v_border_id = "";
   my $v_size = "";
   my $v_length = "";
   my $v_width = "";
   my $v_tag = "";

#  Echo out the input line.
   print "\nInput line:\n   $_";
#  Perform a case insensitive check for the proper data format.
   #if (/(?i)border:.*size.*tag:.*/){
#  Perform a case insensitive check for the proper data format.
#  Capture the desired parts of the data using parentheses. 
   if (/(?i).*border:[  ]*(.*)[  ]*size:[  ]*(.*)[  ]*tag:[  ]*(.*)[  ]*/){
      print "properly formatted\n";
#     Check for no border.
      if ($1 =~ /(?i)none/){
         $v_border_id = "";
      } else {
         $v_border_id = $1;
      }
#     Parse up the size string.
      my ($v_length, $v_width) = split(/x/, $2);
      print split(/(?i)[  ]*x[  ]*/, $2);
      print "\n";
#     Check for no tag.
      #if ($v_tag =~ /(?i)tag:[  ]*none/){
      if ($3 =~ /(?i)none/){
         $v_tag = "";
      } else {
         $v_tag = $3;
         #$v_tag =~ s/.*(?i)tag:[  ]*//;
      }
   } else {
      print "bad format\n";
      $v_border_id = "";
      $v_size = "";
      $v_tag = "";
   }
   print "Border ID:  $v_border_id\n";
   print "Size string:  $2\n";
   print "Length string:  $v_length\n";
   print "Width string:  $v_width\n";
   print "Tag string:  $v_tag\n\n";
}

close INFILE;

Most of the code seems to be working as expected.  I seem to be having a
problem with the split command/assignment as the length and width strings are
blank.  The command is based on Jay's example shown here.

> my ($length, $width) = split / x /, $size ;

What I really wanted to do was this.
my ($v_length, $v_width) = split(/(?i)[  ]*x[  ]*/, $2);

So I put the following in the code to try and understand what was going wrong.
      print split(/(?i)[  ]*x[  ]*/, $2);
      print "\n";

When I ran the program this is one of the outputs
Input line:
    1250 HTPP Yellow 2in sq Border: TW84NYYL Size: 4'11" x 6'1" Tag:  1500mm x
1855mm
properly formatted
4'11"6'1" 
Border ID:  TW84NYYL 
Size string:  4'11" x 6'1" 
Length string:  
Width string:  
Tag string:  1500mm x 1855mm

What am I doing wrong?

Kind Regards,
Keith

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to