perl(1) question

Jose Marques noway at nohow.demon.co.uk
Mon Sep 4 20:35:38 BST 2000


> On Sun, Sep 03, 2000 at 07:11:48PM +0100, Mark Ovens wrote:
> > Does index() ignore leading whitespace? A perl(1) script I have
> > appears to be doing just that. Isolating the relevant lines I have:
> > 
> > #!/usr/bin/perl -w
> > 
> > open 'CONTENTS',"/usr/mark/scrap" or die "cannot open /usr/mark/scrap";
> > 
> > until (eof 'CONTENTS') {
> >     chomp ($line = <CONTENTS>);
> >     $i = index($line, /FOO/i); 
> >     print "line\=$line\n";
> >     print "i \= $i\n";
> > };
> > 
> > close 'CONTENTS';

Try:

#!/usr/bin/perl -w

use strict;

open CONTENTS, "<scrap" or die "cannot open file 'scrap': $!";

while (<CONTENTS>) {
    chomp;
    my $i = index(uc, "FOO"); 
    print "line=$_\ni=$i\n";
} 

close CONTENTS;

The "uc" function uppercases the search string effectively making the
search case insensitive.  The "uc" function takes works on the "$_"
variable (set by the while loop) when no argument is supplied.

-- 
Jose Marques






More information about the Ukfreebsd mailing list