For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > parsing text over newline









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author parsing text over newline
cicco-bionico@web.de

2006-11-05, 6:57 pm

Hi,

I've a list of address as following formated:

ABA S.P.A.
VIA Terese, 21 43005 SALA BAGANZA PR
Tel 0125.5555000 Fax 0125.830428
info@aba.it
http://www.aba.it

ABA2 S.P.A.
VIA Terese2, 21 43005 SALA BAGANZA PR2
Tel 0125.5555000 Fax 0125.830428
info@aba2.it
http://www.aba2.it

my Intend ist to import the datas in a mysql databse, so I would like
to split the text in single variables an then process it. And hier
comes my problems and my questions:

How do I parse the text over a newline so that I can insert every
single addess in an array, I tried
with

while (<> ){

if(/\n/ ... /\n/){

push @arr, $_;
....

but does't works, it does't match anything. But I made also a test by
adding a brgin and end sign
like if(/\|begin/ ... /\|end/) and works very well, but the file
contains more than 1000 address and it take too much work...

the second question: when I've the array with one address how do I
split this in single variables?
I tried a lot of things but noneone works.

Please help, if yoou could make an example would be nice.

Cheers.

Xaver

DJ Stunks

2006-11-05, 6:57 pm

cicco-bionico@web.de wrote:
> Hi,


Hi.

> I've a list of address as following formated:
>
> ABA S.P.A.
> VIA Terese, 21 43005 SALA BAGANZA PR
> Tel 0125.5555000 Fax 0125.830428
> info@aba.it
> http://www.aba.it
>
> ABA2 S.P.A.
> VIA Terese2, 21 43005 SALA BAGANZA PR2
> Tel 0125.5555000 Fax 0125.830428
> info@aba2.it
> http://www.aba2.it
>
> my Intend ist to import the datas in a mysql databse, so I would like
> to split the text in single variables an then process it. And hier
> comes my problems and my questions:
>
> How do I parse the text over a newline so that I can insert every
> single addess in an array


this isn't a "parsing" question, this is a question about how to split
up the input file into records, rather than into lines (which is Perl's
default behavior).

the answer to this question is to use the $/ variable, known in English
as $INPUT_RECORD_SEPARATOR.

> if yoou could make an example would be nice.


Ok, here's an example, run this to see how it's splitting up your data.

#!/usr/bin/perl

use strict;
use warnings;

use English qw{ -no_match_vars };

local $INPUT_RECORD_SEPARATOR = q{};

while ( my $record = <DATA> ) {
print "[$record]\n";
}

__DATA__
ABA S.P.A.
VIA Terese, 21 43005 SALA BAGANZA PR
Tel 0125.5555000 Fax 0125.830428
i...@aba.it
http://www.aba.it

ABA2 S.P.A.
VIA Terese2, 21 43005 SALA BAGANZA PR2
Tel 0125.5555000 Fax 0125.830428
i...@aba2.it
http://www.aba2.it

> the second question: when I've the array with one address how do I
> split this in single variables?
> I tried a lot of things but noneone works.


this is much more difficult. you could split() your $record into
lines, and then work with each one on it's own, or, what I would do, is
match everything at once taking advantage of the /s regular expression
modifier (to make the . metacharacter match newlines) and the /m
modifier (to make the ^ and $ match at the beginning and end of
embedded lines)

take a crack at it and post your attempt for more help.

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com