On Mon, 18 Apr 2005 15:30:39 -0400, Jay Savage wrote
> On 4/18/05, Keith Worthington <[EMAIL PROTECTED]> wrote:
> > 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 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.
> >
> > Kind Regards,
> > Keith
> >
>
> Is this the data you expect to get out of the database...or data from
> somewhere else that you're going to insert into the
> database...or...? Becuase at the moment it doesn't really look like
> a database problem. We need some more here, including some sample
> code. What are you doing, and where is it going wrong?
>
> As far as quickstarts go, for database work, the two sources Chris
> mentioned are really the place to go. For a general perl
> intoductions, the first place to go is perldoc perlfaq, man perl, and
> perldoc perlintro, and the best printed resourse is by far _Learning
> Perl, 3rd Ed._ from O'Reilly. It's short and shout get you up and
> running in about a day, depending on your general programming ability.
>
> As for your specific question, you'll want to use some kind of
> regex. The following should give you some ideas and show a fairly perlish
> appraoch to variable declarations, but this is just an example. The
> perl motto is "There's more than one way to do it." How you end up
> storing the data into variables will hopefully be deterined by what
> you ultimately want to do with it.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $data = "815 HTPP Black 2in sq Border: RMFP025BK Size: 7'10\" x
> 16' Tag: None" ;
>
> $data =~ /.*Border: (.*) Size: (.*) Tag: (.*)$/;
>
> my ($border, $size, $tag) = ($1, $2, $3) ;
>
> my ($length, $width) = split / x /, $size ;
>
> my (%length, %width);
>
> ($length{feet}, $length{inches}) = split /(?:'|")/, $length ;
>
> ($width{feet}, $width{inches}) = split /(?:'|")/, $width ;
>
> print
$border\n$length{feet}\n$length{inches}\n$width{feet}\n$width{inches}\n$tag\n";
>
Jay,
Thanks very much for your post.
I have been banging my head against the wall and based on what I have been
able to read in the last hour or so I have come up with the following code.
#!/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_tag = $_;
# Echo out the input line.
print "Just read in this line: $_";
# Print out the border.
if (/(?i)border:.*size.*tag:.*/){
print "properly formatted\n";
$v_border_id =~ s/.*(?i)border:[ ]*//;
$v_border_id =~ s/[ ]*(?i)size:.*//;
print "Border ID: $v_border_id";
$v_size =~ s/.*(?i)size:[ ]*//;
$v_size =~ s/[ ]*(?i)tag:.*//;
print "Size string: $v_size";
$v_tag =~ s/.*(?i)tag:[ ]*//;
At the moment I am just processing a text file to get the hang of how to munge
data with perl. Eventually I will be creating a function inside a postgresql
database. It sort of works like this:
CREATE OR REPLACE FUNCTION interface.func_parse_description("varchar")
RETURNS varchar AS
$BODY$
< perl statements>
$BODY$
LANGUAGE 'plperl' STABLE STRICT;
If your curious this page will give you more details about what I am trying to
do. http://www.postgresql.org/docs/8.0/interactive/plperl.html
I can see from your post that there are lots better ways to accomplish what I
need. Although at the moment the example you sent is largely beyond me I will
try to implement some of your suggestions and see what happens.
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>