Solved

read text file into array for matching

Posted on 2002-05-09
4
209 Views
Last Modified: 2010-03-05
How to read in a text file, put into an array.
What dimension of the array would the best solution for this?

Text file as :-
------------------------------------------
CON     SEV     CDA     REC

XXX     XXX         100     01
XXX     0505       801     02
201     002011  902     04
228     XXX         902     04
XXX     011600  099     05
000     XXX         801     05
------------------------------------------
XXX is any number.

For bunch of data records on CON,SEV & CDA,
find which match CON,SEV & CDA, then get the REC.

REC is what I want for each data records.

or Do you have a better solution?

Cheers;
0
Comment
Question by:pkimk
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
bebonham earned 150 total points
ID: 6999230
open IN, "dataq.txt";
@rows=<IN>;
close IN;
@hdr=split(/\s+/,shift @rows);
my $i=0;
foreach(@hdr)
{
$key{$_}=$i++
}
for $v(@rows)
{
print getData($v, $key{'REC'});
print "\n";
}


sub getData
{
my ($line, $pos)=@_;
return ${[split(/\s+/,$line)]}[$pos];
}
0
 

Author Comment

by:pkimk
ID: 7000599
what does this few line doin?

$key{$_}=$i++

for $v(@rows)

print getData($v, $key{'REC'});

my ($line, $pos)=@_;

return ${[split(/\s+/,$line)]}[$pos];
0
 
LVL 8

Expert Comment

by:bebonham
ID: 7001692
$key{$_}=$i++

this line makes a hash called key for storing the name of the fields at the top, and the index of the fields,
so $key{'CON'} is equal to 0 in this case...it just makes this script more flexable, so you can still use it if you change or expand your data.


for $v(@rows)
{
print getData($v, $key{'REC'});
}

this is a loop that just outputs all rec value for each ROW in your database.

the getData function is what you really will want to be using, because by passing it a single row ($v in this case)
and then the index of the row --- $key{'REC'}


my ($line, $pos)=@_;
this line takes the arguments passed to the getData and stores them in $line and $pos.

return ${[split(/\s+/,$line)]}[$pos];

then this is the line in the same function (or subroutine)
that returns the particular piece of data that you are asking for from that row (in this case that row is in $line)

so, it splits the row on 1 or more spaces, and then I use that as an anonymous array and access the element that is represented by $pos (which  you passed into the function through $key{'REC'}


I hope that helps,

Bob
0
 

Author Comment

by:pkimk
ID: 7010017
If I change my text file as below, the coding will be more easy.
------------------------------------------
XXX     XXX         100     01
XXX     0505       801     02
201     002011  902     04
228     XXX         902     04
XXX     011600  099     05
000     XXX         801     05
------------------------------------------


my @AoRec = [];

open (IN, "data.txt") or die "Can't open data.txt: $!";
while (<IN>) {
    push @AoRec, [split];
}
close IN or die "Can't close USC_Rec_Format.tab: $!";

#print all record
for $row (@AoRec) {
    print "@$row\n";
}

#print one record
print "$AoRec[1][4]";

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question