Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl data structure

Reply
Thread Tools

perl data structure

 
 
BH
Guest
Posts: n/a
 
      02-19-2008
Hi,

I am reading in an comma separator file,

line 1: Field 1, field 2, field 3.., field n
line 2: Field 1, field 2, field 3.., field n

I would like to create a data structure to representing the whole
file.

What is an efficient and easy way to store the above such that, for a
given value for field i, I can extract an array of values for field j,
where 1<=i,j<=n? It's a bit like asking what the best way to store a
spreadsheet is.

Hash of anonymous arrays? How will the syntax be?

Thanks in advance.

Regards,

BH



 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-19-2008
BH <> wrote in news:6ea19315-2409-4fe4-9d34-
:

> Hi,
>
> I am reading in an comma separator file,
>
> line 1: Field 1, field 2, field 3.., field n
> line 2: Field 1, field 2, field 3.., field n
>
> I would like to create a data structure to representing the whole
> file.
>
> What is an efficient and easy way to store the above such that, for a
> given value for field i, I can extract an array of values for field j,
> where 1<=i,j<=n? It's a bit like asking what the best way to store a
> spreadsheet is.
>
> Hash of anonymous arrays? How will the syntax be?


First off, have you tried DBD-CSV?

http://search.cpan.org/~jzucker/DBD-...lib/DBD/CSV.pm

This has the advantage that your code can keep on working when the data
are moved to a real database later on.

* How large is the data matrix?

* How large is the typical file?

* How large is the typical result set?

If the file is smallish (dunno, maybe fewer than 10,000 lines with say
10 fields each), the contents static and simple, the queries really as
simple as you make them out to be the obvious solution would be the way
to go.

Obvious solution follows. Generalize as required.

#!/usr/bin/perl

use strict;
use warnings;

use Data:umper;

my @data;

while ( my $obs = <DATA> ) {
$obs =~ s/^\s+//;
$obs =~ s/\s+$//;
next unless length $obs;
push @data, [ split /\s+/, $obs ];
}


print Dumper vlookup( \@data, 2, 50, 3 );
print Dumper vlookup( \@data, 0, 100, 0);
print Dumper vlookup( \@data, 3, 0, 3);
print Dumper vlookup( \@data, 3, 0, 1);

sub vlookup {
my ( $data, $key_col, $key_val, $lookup_col ) = @_;

my @retlist;

for my $obs ( @$data ) {
if ( $obs->[ $key_col ] == $key_val ) {
push @retlist, $obs->[ $lookup_col ];
}
}

return \@retlist;
}


__DATA__
70 140 45 85
190 150 40 30
80 40 45 30
110 20 30 5
50 140 70 95
50 80 60 65
170 150 45 40
150 0 30 0
10 30 50 60
30 130 70 100
110 160 75 75
60 120 80 80
170 130 33 30
90 140 75 75
120 90 35 35
130 40 5 5
50 170 100 100
120 30 5 5
90 170 90 90
190 150 30 30
60 20 30 30
130 150 70 60
0 20 60 60
40 120 70 90
70 160 100 95
180 150 20 35
110 70 40 30
140 140 50 50
170 30 0 0
70 130 60 80
80 20 20 20
100 130 65 65
190 20 0 0
110 30 0 10
180 70 0 0
180 50 0 0
40 120 45 90
60 70 55 55
140 160 60 60
60 140 90 90
150 80 20 15
40 10 30 35
160 100 20 20
80 170 90 95
150 90 30 20
90 70 40 40
40 0 30 30
170 50 0 0
110 180 80 85



--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.rehabitation.com/clpmisc.shtml>

 
Reply With Quote
 
 
 
 
david
Guest
Posts: n/a
 
      02-21-2008
On Feb 19, 7:15 pm, BH <Benson....@googlemail.com> wrote:
> Hi,
>
> I am reading in an comma separator file,
>
> line 1: Field 1, field 2, field 3.., field n
> line 2: Field 1, field 2, field 3.., field n
>
> I would like to create a data structure to representing the whole
> file.
>
> What is an efficient and easy way to store the above such that, for a
> given value for field i, I can extract an array of values for field j,
> where 1<=i,j<=n? It's a bit like asking what the best way to store a
> spreadsheet is.
>
> Hash of anonymous arrays? How will the syntax be?
>
> Thanks in advance.
>
> Regards,
>
> BH


Hi,
Lokk at Data::Table,

Best regards,
David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
similar Perl data structure? ela C Programming 60 04-22-2009 09:32 PM
puting an excel file into a perl data structure KaZ Perl Misc 1 06-29-2006 01:42 PM
perl - data structure build to transpose data shree Perl Misc 15 08-31-2004 05:44 AM
Perl complex data structure ... how to get more flexible access? how to avoid eval? valued customer Perl Misc 10 02-25-2004 06:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57