Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

flat file database 3

Status
Not open for further replies.

ailse

Programmer
Jan 20, 2004
79
GB
Hi,

I have a text database in the following format:

Code:
id uid short_name desc org

where all five are tab-delimited. I am trying to read this into a hash of arrays, so that "id" is the key and an array consisting of fields 1 to 4 is the value for each row in my flat file. What I basically need to do, is given an id, is return the array items associated to it, so I can print out something like:

Code:
Protein ID is "id"
UID: "uid"
Short name: "short"
Description: "desc"
Organism: "org"

where the things in quotes would be actual values pulled from the flat file.

Is this possible? I have tried doing something like this:

Code:
open (DESCS, "ids_descriptions.txt") || die ("Could not open input file!");
while( <DESCS> ) {
	chomp;
	($id, $uid, $short, $desc, $org) = split('\t');
	$HoA{$id}=($uid, $short, $desc, $org);
	}

which doesn't work... or perhaps a hash of hashes would be more suitable?

Any help gratefully received :)
 
This line:

$HoA{$id}=($uid, $short, $desc, $org);

should be like this:

$HoA{$id}=[$uid, $short, $desc, $org];

The brackets tell perl to create an anonymous array associated with $id.

Raklet
 
$HoA{$id}=[$uid, $short, $desc, $org];


I think is possible to do that. (I have not tested it)
push(@{$AoH{$id}},($uid, $short, $desc, $org));

foreach $id (sort {$a <=> $b} keys %AoH) {
print "$id => @{$AoH{$id}}";
}


dmazzini
GSM System and Telecomm Consultant

 
open (DESCS, "test.txt") || die ("Could not open input file!");
while( <DESCS> ) {
chomp;
@temp = split('\t');
$HoA{$temp[0]} = [@temp];
}
foreach (keys %HoA) {
print 'Protein ID is ' . $HoA{$_}[0] . "\n";
print 'UID: ' . $HoA{$_}[1] . "\n";
print 'Short name: ' . $HoA{$_}[2] . "\n";
print 'Description: ' . $HoA{$_}[3] . "\n";
print 'Organism: ' . $HoA{$_}[4] . "\n";
}
 
Using complex structures....It might be interesting..

[blue]

open (DESCS, "test.txt") || die ("Could not open input file!");
while( <DESCS> ) {
chomp;
($id, $uid, $short, $desc, $org) = split('\t');
$MYDATA{$uid}{USERID}{$short}{SHORT}{$desc}DESCRIPTION}{$org}{ORG}=$id;
}



foreach $i (sort {$a <=> $b} keys %MYDATA ) {
foreach $j (sort {$a <=> $b} keys %{$MYDATA{$i}{USERID}}) {
foreach $k (sort {$a <=> $b} keys %{$MYDATA{$i}{USERID}{$j}{SHORT}}) {
foreach $l (sort {$a <=> $b} keys %{$MYDATA{$i}{USERID}{$j}{SHORT}{$k}{DESCRIPTION}}) {
print " ID:$MYDATA{$i}{USERID}{$j}{SHORT}{$k}{DESCRIPTION}{$l}{ORG} => USERID:$i,SHORT:$j,DESCRIPTION:$k,ORG:$l";
}
}
}
}

[/blue]


dmazzini
GSM System and Telecomm Consultant

 
thanks everyone, for all your useful posts, have got it working great now :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top