Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Parsing text to array

Reply
Thread Tools

Parsing text to array

 
 
Philipp
Guest
Posts: n/a
 
      08-11-2006
Hello
I would like to parse a piece of text to an array. Is there one of those
Perl-magical ways to do this? Or should I use split and iterate through
the lines?

Thank you for your answers
Phil


$content = "hello world\nthis should be parsed";

# do something here to parse words to columns
# and lines to lines of array

print $contentarray[0][0]; #should print "hello"
print $contentarray[1][2]; #should print "be"
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-11-2006
Philipp <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hello
> I would like to parse a piece of text to an array. Is there one of those
> Perl-magical ways to do this? Or should I use split and iterate through
> the lines?
>
> Thank you for your answers
> Phil
>
>
> $content = "hello world\nthis should be parsed";
>
> # do something here to parse words to columns
> # and lines to lines of array
>
> print $contentarray[0][0]; #should print "hello"
> print $contentarray[1][2]; #should print "be"


my @contentarray = map [ split], split /\n/, $content;

What have you tried?

Anno
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      08-11-2006
Philipp wrote:
> I would like to parse a piece of text to an array. Is there one of those
> Perl-magical ways to do this?


I don't know about 'magical,' but of course it can be easily done in
Perl. It is, after all, a parsing language.

> Or should I use split and iterate through


That would be the worst approach.

Anything else you wish to ask about?

--
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      08-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:
> Philipp <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> Hello
>> I would like to parse a piece of text to an array. Is there one of those
>> Perl-magical ways to do this? Or should I use split and iterate through
>> the lines?
>>
>> Thank you for your answers
>> Phil
>>
>>
>> $content = "hello world\nthis should be parsed";
>>
>> # do something here to parse words to columns
>> # and lines to lines of array
>>
>> print $contentarray[0][0]; #should print "hello"
>> print $contentarray[1][2]; #should print "be"

>
> my @contentarray = map [ split], split /\n/, $content;
>
> What have you tried?


Hello and thanks for your answer.
I didn't know "map". Does exactly what I want...

I would have done a "for" loop on the lines, but I thought that some
smarter perl way existed (and was obviously right).
Thanks again
Phil
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      08-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Philipp wrote:
>>Or should I use split and iterate through

>
>
> That would be the worst approach.


The worst in what sense? And compared to what?
It seems to me to be much easier to comprehend by someone else reading
the code, as compared with Anno's map() solution.

--Ala


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      08-11-2006

Quoth Ala Qumsieh <(E-Mail Removed)>:
> (E-Mail Removed) wrote:
>
> > Philipp wrote:
> >>Or should I use split and iterate through

> >
> >
> > That would be the worst approach.

>
> The worst in what sense? And compared to what?
> It seems to me to be much easier to comprehend by someone else reading
> the code,


....who doesn't know Perl at all. If you're going to code for people like
that you might as well give up.

> as compared with Anno's map() solution.


Which is entirely standard Perl. Not understanding map is nearly as bad
as not understanding hashes: it is a fundamental part of Perl.

Ben

--
Every twenty-four hours about 34k children die from the effects of poverty.
Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
that image, that ghastly, grey-billowing, double-barrelled fall, repeated
twelve times every day. Full of children. [Iain Banks] (E-Mail Removed)
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      08-13-2006
Ben Morrow wrote:
> Quoth Ala Qumsieh <(E-Mail Removed)>:
>>The worst in what sense? And compared to what?
>>It seems to me to be much easier to comprehend by someone else reading
>>the code,

>
>
> ...who doesn't know Perl at all. If you're going to code for people like
> that you might as well give up.


Theoretically, I agree. Unfortunately, that is not the case. Most people
who learn Perl don't care about learning it inside out like you do, and
this is well evident from all the posts in this newsgroup. They just
want to scratch an itch and move on. I personally work with a lot of
people that fall into this category, and sometimes resort to more
verbose coding to aid comprehension (if I can't then I make sure to
document it properly).

>>as compared with Anno's map() solution.

>
>
> Which is entirely standard Perl. Not understanding map is nearly as bad
> as not understanding hashes: it is a fundamental part of Perl.


The OP admitted he didn't know about map(). You don't need to know about
map() to code Perl, and people on this newsgroup need to accept that.

--Ala

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-14-2006
On 11 Aug 2006, (E-Mail Removed) wrote:

> Quoth Ala Qumsieh <(E-Mail Removed)>:
>> as compared with Anno's map() solution.

>
> Which is entirely standard Perl. Not understanding map is nearly as bad
> as not understanding hashes: it is a fundamental part of Perl.


I disagree. You can write very good Perl code without using map(),
for very complicated programs and modules. It's next to impossible to
write good Perl code without hashes (except in very limited situations
or short programs). I would say grep() is much more important than
map() actually, and for grep() the above statement holds true.

Ted
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-14-2006
Ted Zlatanov wrote:
> On 11 Aug 2006, (E-Mail Removed) wrote:
>
> > Quoth Ala Qumsieh <(E-Mail Removed)>:
> >> as compared with Anno's map() solution.

> >
> > Which is entirely standard Perl. Not understanding map is nearly as bad
> > as not understanding hashes: it is a fundamental part of Perl.

>
> I disagree. You can write very good Perl code without using map(),
> for very complicated programs and modules. It's next to impossible to
> write good Perl code without hashes (except in very limited situations
> or short programs). I would say grep() is much more important than
> map() actually, and for grep() the above statement holds true.


map can be written using a for loop and a push
grep can be written using a for loop, an if statement, and a push

I do not believe either is more complicated, important, or fundamental
than the other.

Paul Lalli

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-14-2006
On 14 Aug 2006, (E-Mail Removed) wrote:

Ted Zlatanov wrote: > On 11 Aug 2006, (E-Mail Removed) wrote: >
>>> Quoth Ala Qumsieh <(E-Mail Removed)>:
>>>> as compared with Anno's map() solution.
>>>
>>> Which is entirely standard Perl. Not understanding map is nearly as bad
>>> as not understanding hashes: it is a fundamental part of Perl.

>>
>> I disagree. You can write very good Perl code without using map(),
>> for very complicated programs and modules. It's next to impossible to
>> write good Perl code without hashes (except in very limited situations
>> or short programs). I would say grep() is much more important than
>> map() actually, and for grep() the above statement holds true.

>
> map can be written using a for loop and a push
> grep can be written using a for loop, an if statement, and a push


We're not arguing about basics. I also didn't mention the speed
penalty of map()/grep(), assuming everyone knows about it, or the
decrease in legibility the use of map() incurs quite often.

> I do not believe either is more complicated, important, or fundamental
> than the other.


OK.

Ted
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SAX parsing problem, when element contains text like "[text]" Kai Schlamp Java 1 03-27-2008 08:36 PM
parsing text file into array Hamanjam Perl Misc 3 04-03-2007 05:25 PM
Assistance parsing text file using Text::CSV_XS Domenico Discepola Perl Misc 6 09-02-2004 03:55 PM
Parsing a text file into an array Scott Perl Misc 5 08-30-2004 12:26 PM
Parsing a text file into an array Scott Perl 1 08-17-2004 04:21 PM



Advertisments