perl(1) question
Mark Ovens
marko at freebsd.org
Mon Sep 4 21:17:26 BST 2000
On Mon, Sep 04, 2000 at 08:35:38PM +0100, Jose Marques wrote:
>
> > 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.
>
Thanks Jose, and others who replied. According to Learning Perl
(Schwartz & Christiansen) the sub-string (2nd arg) can be "an
expression that has a string value". The mistake I made was
interpreting this as including regular expressions.
I've got the thing working now :)
> --
> Jose Marques
>
>
>
> ------ FreeBSD UK Users' Group - Mailing List ------
> http://listserver.uk.freebsd.org/mailman/listinfo/freebsd-users
--
4.4 - The number of the Beastie
________________________________________________________________
51.44°N FreeBSD - The Power To Serve http://www.freebsd.org
2.057°W My Webpage http://ukug.uk.freebsd.org/~mark
mailto:marko at freebsd.org http://www.radan.com
More information about the Ukfreebsd
mailing list