homepage Welcome to WebmasterWorld Guest from 23.22.179.210
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
littleman




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

Well I came up with this:

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

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:

a1¦a2¦a3...
b1¦b2¦b3..
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.

 

littleman




msg:431365
 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.

sugarkane




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

I did it this way:

open (FP, "flat.txt");
chomp(@lines=<FP>);
close (FP);

$tmp1=0;
$tmp2=0;

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

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...

littleman




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

Thanks SK,

You thought me something,. It looks like my scriptlet could be weeded down.
This:
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