Code Comments
Programming Forum and web based access to our favorite programming groups.Hi All, I am new to the list and need some quick help. I have been kicking around with vi and sed for years but never took the time to learn Perl. Now I need to use Perl and could really use a jumpstart. I am writing a function in the Postgresql database using Perl because of its text processing power. My other options are less than pretty. :-( I will be 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' Rope Color:Yellow Joint Color:Red 1250 HTPP Black Bonded 2in sq Border: RMFP025BK Size: 39" X 100' Tag: None 1250 HTPP Black Bonded 2in sq Border: RMFP025BK Size: 83" X 40' Tag: None 3000 HTPP Black 4in sq Border: WNY200BK Size: 16'6" x 21'3" Tag: None 250 HTPP Black 1in sq Border: TW84NTBK Size: 9' x 25' Tag: 200' sec 1250 HTPP Yellow 2in sq Border: TW84NYYL Size: 6'1" x 12'7" Tag: 1855mm x 3 840mm I need to parse them up into the pieces Border, Size and Tag. Furthermore I need to break up the Size piece into width and length components, feet and i nches. Using the first example I would like to obtain: 'RMFP025BK' 7 10 16 0 '' (or NULL) Any help would be appreciated. Kind Regards, Keith
Post Follow-up to this messageOn Mon, 18 Apr 2005, Keith Worthington wrote: > Any help would be appreciated. Write a program. Use the DBI module for the database programming. Get the DBD::Pg plugin to DBI for the PostgreSQL driver. Get this stuff from CPAN if it isn't on your system already. Break the problem down into simpler steps if you need to. From the *nix command line, read over `perldoc DBI` for the basics, or get a copy of a book like _Programming the Perl DBI_ for details. This list isn't a script writing service; while we are very happy to help new learners, you have to at least take a stab at reading over the documentation and writing your own code. Please show what you've tried so far and indicate what you're getting stuck on, and we can help you take it from there. -- Chris Devers
Post Follow-up to this messageKeith Worthington wrote:
> Hi All,
Hello,
> I am new to the list and need some quick help. I have been kicking around
> with vi and sed for years but never took the time to learn Perl. Now I ne
ed
> to use Perl and could really use a jumpstart.
>
> I am writing a function in the Postgresql database using Perl because of i
ts
> text processing power. My other options are less than pretty. :-(
>
> I will be 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' Rope Color:Yell
ow
> Joint Color:Red
> 1250 HTPP Black Bonded 2in sq Border: RMFP025BK Size: 39" X 100' Tag: No
ne
> 1250 HTPP Black Bonded 2in sq Border: RMFP025BK Size: 83" X 40' Tag: Non
e
> 3000 HTPP Black 4in sq Border: WNY200BK Size: 16'6" x 21'3" Tag: None
> 250 HTPP Black 1in sq Border: TW84NTBK Size: 9' x 25' Tag: 200' sec
> 1250 HTPP Yellow 2in sq Border: TW84NYYL Size: 6'1" x 12'7" Tag: 1855mm
x 3840mm
>
> I need to parse them up into the pieces Border, Size and Tag. Furthermore
I
> need to break up the Size piece into width and length components, feet and
inches.
>
> Using the first example I would like to obtain:
> 'RMFP025BK'
> 7
> 10
> 16
> 0
> '' (or NULL)
>
> Any help would be appreciated.
This appears to do what you want:
while ( <DATA> ) {
next unless /
Border: \s* (.+?) \s+
Size: \s* (?:(\d+)')?
(?:(\d+)")? \s*x\s*
(?:(\d+)')?
(?:(\d+)")? \s+
Tag: \s* (.*)
/ix;
print "Border: $1 ",
'Size: ',
$2 ? "$2 feet " : '',
$3 ? "$3 inches " : '',
'by ',
$4 ? "$4 feet " : '',
$5 ? "$5 inches " : '',
'Tag: ',
"\L$6" eq 'none' ? '' : $6,
"\n";
}
__DATA__
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' Rope Color:Yellow
Joint Color:Red
1250 HTPP Black Bonded 2in sq Border: RMFP025BK Size: 39" X 100' Tag: None
1250 HTPP Black Bonded 2in sq Border: RMFP025BK Size: 83" X 40' Tag: None
3000 HTPP Black 4in sq Border: WNY200BK Size: 16'6" x 21'3" Tag: None
250 HTPP Black 1in sq Border: TW84NTBK Size: 9' x 25' Tag: 200' sec
1250 HTPP Yellow 2in sq Border: TW84NYYL Size: 6'1" x 12'7" Tag: 1855mm x
3840mm
John
--
use Perl;
program
fulfillment
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.