perl(1) question

Nik Clayton nik at
Mon Sep 4 10:20:56 BST 2000

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Gawd, it's been ages since I wrote any Perl.

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';

index() is not working the way you expect it to.  In particular, SUBSTR
(/FOO/i in your example) can't be a regular expression (at least, not based
on my reading of "perldoc -f index").

Attached is how I'd do it (with, perhaps, more explanatory comments than
I would normally give.

Internet connection, $19.95 a month.  Computer, $799.95.  Modem, $149.95.
Telephone line, $24.95 a month.  Software, free.  USENET transmission,
hundreds if not thousands of dollars.  Thinking before posting, priceless.
Somethings in life you can't buy.  For everything else, there's MasterCard.
  -- Graham Reed, in the Scary Devil Monastery

Content-Type: application/x-perl
Content-Disposition: attachment; filename=""

#!/usr/bin/perl -w

# Iterate over all the files given on the command line, or standard input it
# no files were listed.

while(<>) {
    chomp;				     # Lose the terminating '\n'

    # index() doesn't take a regex.  So first confirm that the thing we're
    # looking for exists in the string using m//i.  Store whatever we found
    # in $1, then look for that using index().

    # First thing, open up a new block.  This is because the lifetime of
    # variables like $1 is limited to the enclosing block.  If we didn't
    # do this then after we match once $1 will always be defined and contain
    # a value, even for lines that didn't match.
    # $1 is a readonly variable, so you can't assign back to it to replace
    # its contents.

    # To check this for yourself, run this script using the included scrap
    # file, then comment out this brace, and it's closing partner, and
    # compare the output from the two runs.
	# Do a case insensitive search.  Use parentheses to store whatever
	# we match in $1
	$_ =~ m/(FOO)/i;

	# $1 will only be defined if we matched something.  Check for this,
	# and if it is then print out the information.
	if(defined($1)) {	    
	    $i = index($_, $1);
	    print "line = '$_'\n";
	    print "i = $i\n";

Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=scrap



More information about the Ukfreebsd mailing list