Welcome to WebmasterWorld Guest from 54.211.17.91

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

getting a three dimensional array from flat text

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

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.
8:08 pm on Mar 2, 2001 (gmt 0)

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.