Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Sep 2000 22:05:03 +0100
From:      Mark Ovens <marko@freebsd.org>
To:        Willem Brown <willem@brwn.org>
Cc:        ben@freebsd.org, questions@freebsd.org
Subject:   Re: perl(1) question
Message-ID:  <20000903220503.A254@parish>
In-Reply-To: <20000903210401.A42226@snoopy.brwn.org>; from willem@brwn.org on Sun, Sep 03, 2000 at 09:04:01PM %2B0200
References:  <20000903190409.B255@parish> <20000903210401.A42226@snoopy.brwn.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Sep 03, 2000 at 09:04:01PM +0200, Willem Brown wrote:
> Hi,
> 
> It would seem that the string index searches for is not a regular
> expression. 
> 

That was it (thanks to ben who also spotted it). My main source of
reference is Learning Perl (Schwartz & Christiansen) which says (p.153) about
index():

	"Notice that both the string being searched and the string
	 being searched for can be a literal string, a scalar variable
	 containing a string, or even an expression that has a string
	 value."

Obviously "expression" does not include "regular expression", which I
assumed it did.

Thanks for the help guys.

> The following changes seems to work when I tried it.
> 
>      $i = index(uc $line, "FOO");
> 
> Basically, convert the word to all uppercase and then check the line.
> 
> line=Foobar
> i = 0
> line= Foobar
> i = 1
> line=  foobar
> i = 2
> line=   FOOBAR
> i = 3
> line=        foOBar
> i = 8
> 
> On Sun, Sep 03, 2000 at 07:04:09PM +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';
> > 
> > Running this on a file containing:
> > 
> > Foobar
> >  Foobar
> >   foobar
> >    FOOBAR
> > 	foOBar
> > 
> > I get:
> > 
> > # ./scrap.pl scrap
> > Use of uninitialized value at ./scrap.pl line 7, <CONTENTS> chunk 1.
> > line=Foobar
> > i = 0
> > Use of uninitialized value at ./scrap.pl line 7, <CONTENTS> chunk 2.
> > line= Foobar
> > i = 0
> > Use of uninitialized value at ./scrap.pl line 7, <CONTENTS> chunk 3.
> > line=  foobar
> > i = 0
> > Use of uninitialized value at ./scrap.pl line 7, <CONTENTS> chunk 4.
> > line=   FOOBAR
> > i = 0
> > Use of uninitialized value at ./scrap.pl line 7, <CONTENTS> chunk 5.
> > line=   foOBar
> > i = 0
> > #
> > 
> > So index() appears to be ignoring the leading whitespace because it
> > always returns 0.
> > 
> > Also, what causes the "Use of uninitialized value...." warning? I
> > cannot see anything wrong with the script. I've checked Learning Perl
> > by Schwartx and Christiansen and the perl manpages; also the code is
> > taken from the original script which doesn't give this warning.
> > 
> > Any pointers would be greatly appreciated.
> > 
> > TIA
> > 
> > -- 
> > 		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@freebsd.org                http://www.radan.com
> > 
> > 
> > 
> > To Unsubscribe: send mail to majordomo@FreeBSD.org
> > with "unsubscribe freebsd-questions" in the body of the message
> > 
> 
> Best Regards
> Willem Brown
> -- 
>  /* =============================================================== */
>  /*      Linux, FreeBSD, NetBSD, OpenBSD. The choice is yours.      */
>  /* =============================================================== */
> 
> Speak softly and own a big, mean doberman.

-- 
		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@freebsd.org                http://www.radan.com



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000903220503.A254>