Selecting a variable - Newbie question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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.


Record 1:


Record 2:


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

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.

Re: Selecting a variable - Newbie question

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.

Re: Selecting a variable - Newbie question

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);

@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\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.

Re: Selecting a variable - Newbie question

Quoted text here. Click to load it

For that type of data access, hashes are ideal. The "indices" are
strings, and the values are also strings. Here is a hash that
encapsulates the data shown above:

my %hash = ( 'MED' => 'FAM', 'DEN' => 'FAM', 'VIS' => 'EE' );

The pattern is key => value. The value of the MED key is retrieved by

For multiple dependents, you can use an array of hash references, one
for each person. The elements of the array must be references, not the
hashes themselves, as array elements must be scalars.

See 'perldoc perldata' for simple data types
and 'perldoc perldsc' for more complex data structures.

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- The #1 Newsgroup Service in the World! >100,000
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

Re: Selecting a variable - Newbie question wrote:
Quoted text here. Click to load it

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

@cov = qw(MED DEV NIS); ## note space, not comma!

Quoted text here. Click to load it

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 = @lvl;

Quoted text here. Click to load it

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.


-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB

Re: Selecting a variable - Newbie question wrote:
Quoted text here. Click to load it
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.

Chris Green

Re: Selecting a variable - Newbie question wrote:
Quoted text here. Click to load it

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";


use Perl;

Re: Selecting a variable - Newbie question wrote:
Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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

Quoted text here. Click to load it

No, at least not to me I am afraid.


Re: Selecting a variable - Newbie question wrote:

Quoted text here. Click to load it

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.

Site Timeline