Selecting a variable - Newbie question

D

dan.bent

I'm confused and up against a deadline. I expect it's an easy question,
but let me know if I need to provide more info.

I have a source file that contains several fields, any of which may
contain the coverage (I work in Insurance) data I need. I need to find
a way to get the right data. The actual data is in a fixed length ASCII
file, but I'll show it as comma seperated for clarity.

Example:

Record 1:

Bob,MED,DEN,VIS,MEDPLAN,DENPLAN,VISPLAN

Record 2:

Mary,DEN,VIS,MED,DENPLAN,VISPLAN,MEDPLAN

I've parsed the file, so I have field names of:
name,cov1,cov2,cov3,plan1,plan2,plan3

I only need to associate MED with MEDPLAN (of course, the actual plan
names are not so simple). I was thinking that since I can easily
identify which coverage field contains MED, I ought to be able to use
the field number (1 in Bob's case, 3 in Mary's) to get to the plan. I
just don't know the syntax to get the content of the field returned.

I've tried writing code that correctly identifies which "cov" field
contains "MED", but I'm not getting the content of the "plan" field.
I've tried:

if($MED ne '0'){
$medplan=$'plan'.$MED ;
}
Where $MED is the number of the "cov" field that contains "MED", but
that didn't give me what I wanted.

Is that clear? I'm not sure I've explained the problem well.

Anyway, I appreciate the effort or everyone who has taken the time to
read this far, and I'm eager to learn whatever information folks are
willing to share.
 
J

John W. Krahn

I'm confused and up against a deadline. I expect it's an easy question,
but let me know if I need to provide more info.

I have a source file that contains several fields, any of which may
contain the coverage (I work in Insurance) data I need. I need to find
a way to get the right data. The actual data is in a fixed length ASCII
file, but I'll show it as comma seperated for clarity.

Example:

Record 1:

Bob,MED,DEN,VIS,MEDPLAN,DENPLAN,VISPLAN

Record 2:

Mary,DEN,VIS,MED,DENPLAN,VISPLAN,MEDPLAN

I've parsed the file, so I have field names of:
name,cov1,cov2,cov3,plan1,plan2,plan3

I only need to associate MED with MEDPLAN (of course, the actual plan
names are not so simple). I was thinking that since I can easily
identify which coverage field contains MED, I ought to be able to use
the field number (1 in Bob's case, 3 in Mary's) to get to the plan. I
just don't know the syntax to get the content of the field returned.

I've tried writing code that correctly identifies which "cov" field
contains "MED", but I'm not getting the content of the "plan" field.
I've tried:

if($MED ne '0'){
$medplan=$'plan'.$MED ;
}
Where $MED is the number of the "cov" field that contains "MED", but
that didn't give me what I wanted.

Is that clear? I'm not sure I've explained the problem well.

Anyway, I appreciate the effort or everyone who has taken the time to
read this far, and I'm eager to learn whatever information folks are
willing to share.

Do you have headers that describe which fields are 'cov' and which are 'plan'
and if not how do you determine the difference?

This may work (untested):

while ( <FILE> ) {

my ( $name, @cov ) = unpack 'FORMAT?', $_;

my @plan = splice @cov, @cov / 2;

my ( $med_index ) = grep $cov[ $_ ] eq 'MED', 0 .. $#cov;

print "$name $cov[$med_index] $plan[$med_index]\n";

}




John
 
J

Jürgen Exner

I'm confused and up against a deadline. I expect it's an easy
question, but let me know if I need to provide more info.

I have a source file that contains several fields, any of which may
contain the coverage (I work in Insurance) data I need. I need to find
a way to get the right data. The actual data is in a fixed length
ASCII file, but I'll show it as comma seperated for clarity.

Example:

Record 1:

Bob,MED,DEN,VIS,MEDPLAN,DENPLAN,VISPLAN

Record 2:

Mary,DEN,VIS,MED,DENPLAN,VISPLAN,MEDPLAN

I've parsed the file, so I have field names of:

Perl does not have "fields" or "field names". What _Perl_ data structure are
you using? The answer to that question determines how you would access this
data later.
name,cov1,cov2,cov3,plan1,plan2,plan3

I only need to associate MED with MEDPLAN (of course, the actual plan
names are not so simple). I was thinking that since I can easily
identify which coverage field contains MED, I ought to be able to use
the field number (1 in Bob's case, 3 in Mary's) to get to the plan. I
just don't know the syntax to get the content of the field returned.

I've tried writing code that correctly identifies which "cov" field
contains "MED", but I'm not getting the content of the "plan" field.
I've tried:

Again, Perl does not have fields. Your explanation may be clear to you, but
for anyone not familiar with your thought process it is gibberish.
if($MED ne '0'){
$medplan=$'plan'.$MED ;
}
Where $MED is the number of the "cov" field that contains "MED", but
that didn't give me what I wanted.

Fields have numbers? Is cov an array and $MED ) the index?
BTW: it is customary to use all-upper case variable names for file handles
only.
Is that clear? I'm not sure I've explained the problem well.

No, at least not to me I am afraid.

jue
 
B

Brian McCauley

if($MED ne '0'){
$medplan=$'plan'.$MED ;
}

You appear to be trying to use symbolic references. You've almost got
the syntax right but since you really do _not_ want to use symbolic
references I won't show you haw to fix it.

If you really want to shoot yourself in the foot then read the
documentation on references.

Always use the natural representation for things. The natural
reprentation of several scalar variables grouped together and accessed
by a numeric index is an array.
 
D

dan.bent

Ok, I got some excellent responses, and obviously, I need to clarify my
question. I have to admit that I do not fully understand John Krahn's
response, but perhaps I will after a bit more experience. I suspect
that Jurgen Exner, John Krahn and Brian McCauley may all be expressing
the same thing. So, let me clarify a couple of points:

I'm parsing an ASCII fixed length file. I'm trying to reformat the
source file, do some data mapping, and arrive at an ASCII fixed length
file that I can use as a source file for an import program, which will
ultimately put the data into an application that our organization uses.

I think Brian is right on. I believe I'll be better off putting the
data into an array. I've got the 3rd edition of the camel book, so I'm
optimistic that I can figure it out, but I have not had much success
understanding arrays in the past. Perhaps my failure to understand has
been a function of not having a concrete, meaningful-to-me, example to
work with. This may be the case that produces a breakthrough for me.
If folks have additional comments or suggestions, I'm open to them.
 
D

dan.bent

The array construct seems to be exactly what I need (for this, and many
future projects, so big thanks to all respondents, especially Brian),
but I'm still not quite getting it.

Using a single record as an example, I now have:
@cov = (MED,DEN,VIS);

and
@lvl = (FAM,FAM,EE);

Indicating that this employee has family coverage for Medical and
Dental, but only the employee is covered for Vision.

I'm not understanding how to use the arrays to create a dependent's
record, showing Medical and Dental, but not Vision coverage.

My guess would be:
for $coverage (@cov){
if($lvl[subscript] eq "FAM"){
print "Dependent has ${coverage} coverage\n"
}

Of course, I wouldn't print it, I'd just set an appropriate variable.
The thing I don't understand is what to put where "subscript" is, or,
more accurately, how to keep the two arrays in sync, so that I'm
comparing elements with the same subscript from each array.

Am I making more sense now?
Again, thanks for all assistance.
 
X

xhoster

The array construct seems to be exactly what I need (for this, and many
future projects, so big thanks to all respondents, especially Brian),
but I'm still not quite getting it.

Using a single record as an example, I now have:
@cov = (MED,DEN,VIS);

and
@lvl = (FAM,FAM,EE);

There should be quotes around those. Or you could use the qw construct:

@cov = qw(MED DEV NIS); ## note space, not comma!
Indicating that this employee has family coverage for Medical and
Dental, but only the employee is covered for Vision.

You have two arrays that are positionally correlated. It would probably
be best to turn them into the corresponding hash. It is easy to do that
with a hash slice:

my %hash;
@hash{@cov} = @lvl;
I'm not understanding how to use the arrays to create a dependent's
record, showing Medical and Dental, but not Vision coverage.

My guess would be:
for $coverage (@cov){
if($lvl[subscript] eq "FAM"){
print "Dependent has ${coverage} coverage\n"
}

Of course, I wouldn't print it, I'd just set an appropriate variable.
The thing I don't understand is what to put where "subscript" is, or,
more accurately, how to keep the two arrays in sync, so that I'm
comparing elements with the same subscript from each array.

If you want to keep it with the correlated arrays, rather than then using
a hash, then it would be something like:

foreach my $cov_indx (0..$#cov) {
if ($lvl[$cov_indx] eq 'FAM') {
print "Dependent has $cov[$cov_indx] coverage\n"
};
};

But I'd strongly recommend the hash method instead.

Xho
 
U

usenet

The array construct seems to be exactly what I need (for this, and many
future projects, so big thanks to all respondents, especially Brian),
but I'm still not quite getting it.

Using a single record as an example, I now have:
@cov = (MED,DEN,VIS);

and
@lvl = (FAM,FAM,EE);

Indicating that this employee has family coverage for Medical and
Dental, but only the employee is covered for Vision.

I'm not understanding how to use the arrays to create a dependent's
record, showing Medical and Dental, but not Vision coverage.

My guess would be:
for $coverage (@cov){
if($lvl[subscript] eq "FAM"){
print "Dependent has ${coverage} coverage\n"
}

Of course, I wouldn't print it, I'd just set an appropriate variable.
The thing I don't understand is what to put where "subscript" is, or,
more accurately, how to keep the two arrays in sync, so that I'm
comparing elements with the same subscript from each array.
It sound to me as if you should really be using a relational database
for this work, the sort of things you are doing fit almost exactly
into the way a relational database works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top