[comp.lang.perl] Help. Access char in a line

chuck@hplisa.HP.COM (-Chuck Bade) (08/15/90)

I am new to perl, so this may be obvious to many of you, but I need to 
manipulate some characters of some formatted output into another form.

The fields I am interested in are always in the same position in the line
with no tabs, only spaces padding between fields.  

I know how to read a line in from a file, but then how do I access 
certain characters in that line?

Chuck Bade
chuck@hpfire.lvld.hp.com

inc@tc.fluke.COM (Gary Benson) (08/22/90)

In article <1240001@hplisa.HP.COM> chuck@hplisa.HP.COM (-Chuck Bade) writes:
>I am new to perl, so this may be obvious to many of you, but I need to 
>manipulate some characters of some formatted output into another form.
>
>The fields I am interested in are always in the same position in the line
>with no tabs, only spaces padding between fields.  
>
>I know how to read a line in from a file, but then how do I access 
>certain characters in that line?
>
>Chuck Bade
>chuck@hpfire.lvld.hp.com


Chuck, this sounds exactly like my first posting here! I think that I can
answer your question, at least to some extent.

Your question is somehwat ambiguous and open to interpretation, so really there
are many answers, but they all boil down to this: You can access individual
characters on a line in several different ways, depending on what you want
to do with them.

I needed to know what was in particular character positions in a database,
and used substr a lot, like this:

     $cage = substr($_ , 0, 5);                  # cage code in left column

The left column had a CAGE code (5-digit number the US Govt. uses to
identify suppliers). This line assigns to the variable $cage whatever
appears on the current line from position 0 through 4.

I used this sort of line frequently in that program. Here is another example:

    $mfgname = substr($_, 8, 50);                # manfacturer name in fld two.

This works exactly the same, but begins at column 8 and extends for 50
or out to column 57 (perl counts from zero unless you change it).

If you don't really need to know what is on the line except to change it to
something else, you can use regular expressions. For example, one of the
lines in the records I was dealing with always started with an
identification string that did not change through the whole file -- the
Gov't contract number or some such that my perl script did not keep to be
being reminded about :-). But these were followed immediately by a sequence
number that I was very much interested in. In other words, my records were
numbered N52LNAA001, N52LNAA002, N52LNAA003, N52LNAA004, N52LNAA005...

Part of the program's job was to renumber the records, because as records
were added to the database, they were not resequenced, they just had an
alphabetic identifier tacked on to maintain the sequence, and if any were
dropped, there were just holes in the numbering. So I used a regular
expression first to strip out the sequence numbers:

    s/^(N52LNAA[0-9]{3})[A-Z]/$1 /g;     

Later, inside an incrementing loop, I assigned the new numbers using
another regular expression substitution:

    s/(^N52LNAA).{4}/$1$an/;

These are just two techniques, but there are others in perl depending on
what you want to do. You can split a line out into an array using any
character (or even NO character!) as the field delimiter. Here's how to
spilt the entire current line into an array containing one character each:

    @chararray = split (//,$_);

Yes, it is useful sometimes! Once you have that, you can shift the array
left or right, pop or push values off each end, lots of neat stuff. This is
what I used once when I needed to know how many fields were on a line. In
this case, the number of fields could vary, and they could be separated by a
bunch of different delimiters -- a pipe, caret, plus, or less than symbol.

    $fotl = (split (/[|+^<]/,$_) - 1);     #fotl= fields on this line

There are other ways, too, I'm sure, but I think that the things I have told
you here are pretty generally useable, and are things you will need to learn
at some time anyway. I have not used all of perl's many and varied functions,
but they seem to present a coherent set, so they seem to work well together
and tend to act the way you'd expect them to.

Good luck, and I hope this helps answer your question.





-- 
Gary Benson    -=[ S M I L E R ]=-   -_-_-_-inc@fluke.tc.com_-_-_-_-_-_-_-_-_-

This wallpaper's killing me. One of us has got to go. -Oscar Wilde (last words)