Solved

read text file into array for matching

Posted on 2002-05-09
4
176 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
Comment Utility
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
Comment Utility
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
Comment Utility
$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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now