On 4/19/05, Keith Worthington <[EMAIL PROTECTED]> wrote: > 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";
[snip] Well, it looks like the immediate issue here is that there is no $2. The match variables, including $1, $2, etc. are reset /every/ time you run a regex, whether you use them or not.. 'if ($1 =~ /(?i)none/){' undef'd $1, $2, and $3 so it could reuse them, and then didn't reload them. The match variables are /very/ temporary; if you're not going to use them immediately, assign them to a temporary variable. Also don't complicate your code unecessarily: if you're never going to turn off case sensitivity (i.e. with (-?i)), just use /none/i instead of /(?i)none/. Your eyes will thank you in the long run. HTH, --jay -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>