Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

read text file into array for matching

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
pkimk
Asked:
pkimk
  • 2
  • 2
1 Solution
 
bebonhamCommented:
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
 
pkimkAuthor Commented:
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
 
bebonhamCommented:
$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
 
pkimkAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now