homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

getting a three dimensional array from flat text

 1:17 am on Jan 10, 2001 (gmt 0)

Well I came up with this:

my(@wlines) = <INF>;
foreach $t(@wlines) {
$d .= $t;
@each_line = split(/\n/,$d);
$cut =0;
foreach $word_group(@each_line){
@{$words[$cut]} = split(/\¦/,$word_group);

I was needed to dice up a flat text file into a three dimensional array and this is the approach I came up with. It is made to work with a pipe delimited ('¦') file.
like this:

and so on...

so $words[0][1] would be 'a2' and $words[1][0] would be 'b1'. I couldn't find anything like this on the web to do this so I hacked this code together, so if anyone has a cleaner approach, please post it here.



 8:08 pm on Mar 2, 2001 (gmt 0)

I just wanted to float this back up now that this forum is jumping. If anyone has an improvement / variation on this please post away.


 9:29 am on Mar 3, 2001 (gmt 0)

I did it this way:

open (FP, "flat.txt");
close (FP);


foreach $i (@lines) {
@temp=split(/\/, $i);
foreach $foo (@temp) {$words[$tmp1][$tmp2++]=$foo;}

I'm not sure if it's an improvement - I've not benchmarked it - but it's another way.

Your @{$words[$cut]} is an interesting construct, I must look into that...


 7:16 pm on Mar 3, 2001 (gmt 0)

Thanks SK,

You thought me something,. It looks like my scriptlet could be weeded down.
foreach $t(@wlines) {
$d .= $t;
@each_line = split(/\n/,$d);
$cut =0;
foreach $word_group(@each_line){

could be replaced with this:
$cut =0;
foreach $word_group(@wlines){

I didn't know that perl will automatically divide up an array along the newline character. That is cool.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved