PDA

View Full Version : Uppercasing file contents


spaze
09-17-2002, 02:54 AM
Hello!

I am writing a script that loads the contents from a file and shows all entries that are within the given search criteria.

The problem is, that it doesn't show the entries if the casing is not entered correctly.

For example, if the search keyword is 'Airplane', it will not pick up entries that are 'airplane'

Is there a way to ignore the casing? here is the code bit that searches through the file...

foreach $line (@array){
if ($line =~ /$search/){
($last,$first,$airplane,$sign,$dep,$arr,$timehrs,$timemin,$comp,$type,$ref,$desc,$weather,$entrysave d,$url)=split(/\|/,$line);

Is there a way that I could change all these entries in the memory to uppercase as well as the search keyword so that it would find all entries no matter what casing, but still would show the correct casing on the page?

Thanks for your help!

iDxMan
09-17-2002, 10:11 AM
if ($line =~ /$search/i){


include an `i` regex modifier.

kmj
09-17-2002, 10:11 AM
if ($line =~ /$search/i) { # notice the i flag after the search. I believe that's all you need.


damn, iDxMan si teh winn4r.

iDxMan
09-17-2002, 10:16 AM
Its also worthy to note, that the `i` modifier doesn't change the case of the line, it only performs a case insensitive match.

If you wanted to convert the line to uppercase:


$line =~ tr/a-z/A-Z/;





damn, iDxMan si teh winn4r.


lol.. quick on the draw

kmj
09-17-2002, 11:42 AM
yeah; and I even waited until after I had posted to go verify that I was right! :) (my perl knowledge atrophies as my python knowledge grows. :) )

Btw, spaze, please wrap your code in a [ code ] / [ /code ] block when posting; that will preserve indentation and make your code readable. (Help us to help you!) :)

like so:

foreach $line (@array){
if ($line =~ /$search/){
($last, $first, $airplane, $sign, $dep, $arr, $timehrs, $timemin,
$comp, $type, $ref, $desc, $weather, $entrysaved,
$url)=split(/\|/,$line);