For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2007 > Loading csv type file into an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Loading csv type file into an array
Rich Bradshaw

2007-02-20, 6:58 pm

Hi,

I want to load a file into an array so that I can export the median,
standard error etc. As I am getting slightly more confident with Perl,
I am attempting to do this from scratch... not a good idea, as I don't
know much Perl at all. To get me started, I would like to load a three
column file into an array. I can load the data, but I don't think it
is an array with 3 columns. I would like to know how to specify which
column of data to look at as well if possible.. Thanks!

So far I have:

#!/usr/bin/perl

use strict;
use warnings;

my $file = "information05-1.txt"; # Name of file
open(INFO, $file) || die("Could not open file!"); # Open the file or
exit.
@data = <INFO>; # Read it into an array
close(INFO); # Close the file

#print @data; # Print the array in a boring way!

# print the array in a way that enables me to do exciting things to it
at some point!

$end = $#data;

for ($i = 0; $i < $end; ++$i)
{
print $data[$i];
}

Oh, if there is time, I would like to know how to tell this script
which file to load,

i.e. ./readdata.pl < information05-1.txt

Thank you!

Rich Bradshaw

2007-02-20, 6:58 pm

On Feb 20, 8:20 pm, "Rich Bradshaw" <Rich.Brads...@gmail.com> wrote:
> Hi,
>
> I want to load a file into an array so that I can export the median,
> standard error etc. As I am getting slightly more confident with Perl,
> I am attempting to do this from scratch... not a good idea, as I don't
> know much Perl at all. To get me started, I would like to load a three
> column file into an array. I can load the data, but I don't think it
> is an array with 3 columns. I would like to know how to specify which
> column of data to look at as well if possible.. Thanks!
>
> So far I have:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $file = "information05-1.txt"; # Name of file
> open(INFO, $file) || die("Could not open file!"); # Open the file or
> exit.
> @data = <INFO>; # Read it into an array
> close(INFO); # Close the file
>
> #print @data; # Print the array in a boring way!
>
> # print the array in a way that enables me to do exciting things to it
> at some point!
>
> $end = $#data;
>
> for ($i = 0; $i < $end; ++$i)
> {
> print $data[$i];
>
> }
>
> Oh, if there is time, I would like to know how to tell this script
> which file to load,
>
> i.e. ./readdata.pl < information05-1.txt
>
> Thank you!


OK, I have gotten somewhere - decided to start writing again from
scratch.

#!/usr/bin/perl

use strict;
use warnings;
my @list;
$/=",";

while (<> ) {
push (@list, $_);
}

foreach (@list) {
chop $_; # This gets rid of the commas.
print "$_\n";
}

print $list[100];

This almost works, but I am still getting my csv file into one column
where every element is in one column, that is:

1, 2
2, 4
3, 9

ends up as
1
2
2
4
3
9

, so it still isn't really working. Found a really good tutorial in
case there isn't enough floating around, at
http://www.sthomas.net/oldpages/rob...l.htm#109-Files ,
which has helped my general concepts of Perl a lot.

Does anyone know how I can continue? Do I need to use a hash table to
get a two column array, or can I do that just using arrays?

Thanks,

Rich

Sethsdad

2007-02-20, 6:58 pm

On Feb 20, 4:04 pm, "Rich Bradshaw" <Rich.Brads...@gmail.com> wrote:
> On Feb 20, 8:20 pm, "Rich Bradshaw" <Rich.Brads...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> OK, I have gotten somewhere - decided to start writing again from
> scratch.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> my @list;
> $/=",";
>
> while (<> ) {
> push (@list, $_);
>
> }
>
> foreach (@list) {
> chop $_; # This gets rid of the commas.
> print "$_\n";
>
> }
>
> print $list[100];
>
> This almost works, but I am still getting my csv file into one column
> where every element is in one column, that is:
>
> 1, 2
> 2, 4
> 3, 9
>
> ends up as
> 1
> 2
> 2
> 4
> 3
> 9
>
> , so it still isn't really working. Found a really good tutorial in
> case there isn't enough floating around, athttp://www.sthomas.net/oldpages/roberts-perl-tutorial.htm#109-Files,
> which has helped my general concepts of Perl a lot.
>
> Does anyone know how I can continue? Do I need to use a hash table to
> get a two column array, or can I do that just using arrays?
>
> Thanks,
>
> Rich- Hide quoted text -
>
> - Show quoted text -


good form say to use a hash with 3 elements and make each hash an
element of the array; a quick and dirty way to make a single array
appear to be 3 columns would be to process it with a for loop.

Rich Bradshaw

2007-02-20, 6:58 pm

On Feb 20, 10:20 pm, "Sethsdad" <d...@debsinc.com> wrote:
> On Feb 20, 4:04 pm, "Rich Bradshaw" <Rich.Brads...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> good form say to use a hash with 3 elements and make each hash an
> element of the array; a quick and dirty way to make a single array
> appear to be 3 columns would be to process it with a for loop.


How would you do that? I haven't worked out how to deal with hashes
yet...

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com