Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

read text file into array for matching

Posted on 2002-05-09
4
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
bebonham earned 600 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

636 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