[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] working with strings



On Fri, Jan 22, 1999 at 09:42:06AM -0800, Adam Bridge wrote:
} I have an odd problem and I can't quite grok how to get Perl to do what I want.
} 
} I'm reading in a file whose data is written in fixed length records.  Fields are
} defined within each record.
} 
} Lets say there is a field called full_name which is 40 characters long and I've
} read it into a variable $full_name.
} 
} It looks like "ATKINSON, TINA                     "
} 
} What I need to do is:
} 
} 1) reverse the name parts and
} 2) have only the first character in upper case
} 
} The way I'd think to do it in a traditional language is to find the last
} character that isn't whitespace to get the real length of the name, find the
} comma, take the characters up to the comma as the last name and the rest, save
} the leading space, as the first name.
} 
} But there doesn't seem to be that sort of addressability inside strings.
} 
} Probably there's a better way to do it.  Can someone suggest it?  Broad hints
} are okay.

Depending on what kind of control you want, use split or a regular
expression.  For example,

($last,$first) = split(/,/,$full_name);

Then you use ucfirst (and maybe lc) to print them back out.

print ucfirst($first), " ",ucfirst($lsat),"\n";

A regular expression will allow you to pattern match and extract what
you want out of any string.  However, it isn't always the most
efficient way to do the job.


} 
} Thanks
} 
} Adam
} 
} 
} 
} ***** Want to unsubscribe from this list?
} ***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch
} 
} 

-- 
Paul Schinder
schinder@pobox.com

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch