Glenn Booth am Donnerstag, 5. April 2007 23:10:
> Hi All,

Hi Glenn

> I'm a two-week perl newbie, trying to get my head around text
> handling. I keep getting sent badly formatted text files, which
> I have to 'repair' so that I can use them to feed an Oracle
> database. They are typically a few thousand lines long.
>
> The files generally arrive in a format like this:
>
> serial_number | TITLE OF SOMETHING | free text description | price |
>
> For example:
>
> 0097138 | BOOK TITLE | A book about dogs | 4.99 |
> 0098102 | different book title | small blue book about cats | 2.99 |
>
> I need to sort out the cases of the text fields (BOOK TITLE)
> and ( a book about cats ) and render them to "Title Case" (first
> character upper case for each word). So my example would become:
>
> 0097138 | Book Title | A Book About Dogs | 4.99 |
> 0098102 | Different Book Title | Small Blue Book About Cats | 2.99 |
>
> I have an awk solution, but since I want to learn Perl...
>
> My failed approach so far:
>
> "While" loop to read file line by line
> "Split" each line using delimiter (pipe in this case)
> Put the text fields into an array
> "Shift" each element out of the array
> Run a regex to upper case the first character
> Shift element back into array
>
> I made a horrible mess, and it didn't work. I also tried using substr()
> to isolate the first character then uc() it, lc() the rest of the string
> and then concatenate the result. Even uglier, and it still didn't work.
>
> Anyone have an elegant way?

What about (tested with sample data):

$ perl -nle 'print join " ", map ucfirst, map lc, split' < in.txt > out.txt

perldoc perlrun # for -nle
perldoc -f [map|ucfirst|lc|split]


Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to