Read File into multi-dimensional array ignore whitespace

V

vorticitywolfe

Hello, a newbie question...

I have a file that looks like this:
########test.txt#########
28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
0454
30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
0455
31 0739 1719 0556 1843 0428
1959 0457
#######################

and I read it with this code:

###########test.pl###########
while(my $line = <INPUT>){
push @AoA, [ split ];
}
###########################

I am trying to read it into a multi dimensional array, much like the
perldoc perllol; however, I'm running into a couple of problems.

1.) What does this mean in the documentation, i.e. I don't get what
function the line should be calling , and it is not explained? I see
that it is trying to index $AoA with row $x and column $y, but the
function is throwing me off.

for $x (1 .. 10) {
for $y (1 .. 10) {
$AoA[$x][$y] = func($x, $y);
}
}

2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
combines the array into 8 elements (for the last line ) as opposed to
13 for the first).

3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
know this is because I am not printing a scalar. But the question here
is: Is there a way to print an array without for looping through each
element?

Thanks for all your help in advance!

Jonathan
 
I

it_says_BALLS_on_your forehead

Hello, a newbie question...

I have a file that looks like this:
########test.txt#########
28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
0454
30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
0455
31 0739 1719 0556 1843 0428
1959 0457
#######################

and I read it with this code:

###########test.pl###########
while(my $line = <INPUT>){
push @AoA, [ split ];}

###########################

I am trying to read it into a multi dimensional array, much like the
perldoc perllol; however, I'm running into a couple of problems.

1.) What does this mean in the documentation, i.e. I don't get what
function the line should be calling , and it is not explained? I see
that it is trying to index $AoA with row $x and column $y, but the
function is throwing me off.

for $x (1 .. 10) {
for $y (1 .. 10) {
$AoA[$x][$y] = func($x, $y);
}
}

Don't worry about the function call. That's just an example to show
what's possible.
2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
combines the array into 8 elements (for the last line ) as opposed to
13 for the first).

right, you're creating jagged arrays because the split function
doesn't care what whitespace character to split on or how many if
called with no explicit delimiter. there's no way for perl to know how
many columns there SHOULD be. you need to be more precise in your
description of your data. is it only one space character between each
value? is it a tab?
3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
know this is because I am not printing a scalar. But the question here
is: Is there a way to print an array without for looping through each
element?

you can always use Data::Dumper.
e.g.
print Dumper( \@AoA ), "\n";
 
V

vorticitywolfe

Hello, a newbie question...
I have a file that looks like this:
########test.txt#########
28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
0454
30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
0455
31 0739 1719 0556 1843 0428
1959 0457
#######################
and I read it with this code:
###########test.pl###########
while(my $line = <INPUT>){
push @AoA, [ split ];}
###########################

I am trying to read it into a multi dimensional array, much like the
perldoc perllol; however, I'm running into a couple of problems.
1.) What does this mean in the documentation, i.e. I don't get what
function the line should be calling , and it is not explained? I see
that it is trying to index $AoA with row $x and column $y, but the
function is throwing me off.
for $x (1 .. 10) {
for $y (1 .. 10) {
$AoA[$x][$y] = func($x, $y);
}
}

Don't worry about the function call. That's just an example to show
what's possible.
2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
combines the array into 8 elements (for the last line ) as opposed to
13 for the first).

right, you're creating jagged arrays because the split function
doesn't care what whitespace character to split on or how many if
called with no explicit delimiter. there's no way for perl to know how
many columns there SHOULD be. you need to be more precise in your
description of your data. is it only one space character between each
value? is it a tab?
3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
know this is because I am not printing a scalar. But the question here
is: Is there a way to print an array without for looping through each
element?

you can always use Data::Dumper.
e.g.
print Dumper( \@AoA ), "\n";
Thanks for all your help in advance!

Thanks for your help, I'm kind of stuck with the jagged arrays,
because the files I'm reading are created elsewhere, and I just want
to chunk through them getting each value. I was trying to parse it
with split since the format is XX--XXXX-XXXX--XXXX-XXXX--
where the X are numbers and the - are whitespace. So I used a split(/
\s?\s?/) command, but even then I can't get around the single then
double space syntax. what a mess.

The function thing makes sense now, I thought it may have been some
other syntax that I was unaware of. So thanks! I wish it were a csv
file :) I have much more experience with those, though in IDL... oh
well.
 
T

Tad McClellan

Hello, a newbie question...

I have a file that looks like this:
########test.txt#########
28 0742 1715 0656 1800 0602 1839 0506 1920 0430 1956 0427 2011
29 0741 1716 0600 1840 0505 1922 0429 1957 0427 2011
0454
30 0740 1718 0558 1842 0503 1923 0429 1958 0428 2011
0455
31 0739 1719 0556 1843 0428
1959 0457
#######################

and I read it with this code:

###########test.pl###########
while(my $line = <INPUT>){
push @AoA, [ split ];


Errr, you are splitting the string that is in $_ .

Where is it that you put a string into $_ ?

3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
know this is because I am not printing a scalar.


No, it is because you _are_ printing a scalar (a reference).

But the question here
is: Is there a way to print an array without for looping through each
element?


No.

local $" = ',';
print map "@$_\n", @AoA;

but map() is just a foreach() in disguise, so you are still looping.
 
M

Michele Dondi

1.) What does this mean in the documentation, i.e. I don't get what
function the line should be calling , and it is not explained? I see
that it is trying to index $AoA with row $x and column $y, but the
function is throwing me off.

for $x (1 .. 10) {
for $y (1 .. 10) {
$AoA[$x][$y] = func($x, $y);
}
}

It is a *generic* subroutine or builtin function. It's an example
after all. Try any expression involving $x and $y e.g. $x*($y-$x).
2) the push@AoA, [ split ]; thinks the 3rd column is blank space and
combines the array into 8 elements (for the last line ) as opposed to
13 for the first).

Well, that's because the first line contains 13 whitespace separated
numbers and the last one 8. Did you expect something different? From
your sample it is not entirely possible to understand whether your
data is supposed to be whitespace-separated or has fixed width fields
(with possibly blank ones). In the latter case, you have to follow a
radically different approach.
3.) If I print @AoA; I get the: ARRAY(BLAH BLAH) ARRAY(BLAH BLAH). I
know this is because I am not printing a scalar. But the question here
is: Is there a way to print an array without for looping through each
element?

perldoc Data::Dumper


Michele
 
J

Jan Pluntke

Thanks for your help, I'm kind of stuck with the jagged arrays,
because the files I'm reading are created elsewhere, and I just want
to chunk through them getting each value. I was trying to parse it
with split since the format is XX--XXXX-XXXX--XXXX-XXXX--
where the X are numbers and the - are whitespace. So I used a split(/
\s?\s?/) command,

That will also split between digits (as the empty string also matches
\s?\s?). If you want to split at (and forget about) any whitespace,
use /\s+/.
but even then I can't get around the single then double space
syntax. what a mess.

Best,
Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top