in reply to (meonkeys: can't get every column)
in thread Locate char in a string
That is why my example used it in a looping construct. Otherwise you only get the first match.
BEGIN EDIT
I was asked exactly what that would look like. Here it is:
END EDIT#!/usr/bin/perl -w use strict; while (<>) { my @pos; while (m/\s+/g) { # Loop while it matches push @pos, pos(); } print join " ", @pos; print "\n"; }
Note that this idea can be used to create rather complex parsing engines, each /g match being used to locate the next expected token that you are looking for. If you want to do this then you will need to be careful in how you pass the variable around. Specifically be aware of the fact that if pos($foo) is set then $bar=$foo does not result in pos($bar) being set. (But if you pass $foo into a function then pos($_[0]) will still be set.)
This is all explained in perlop. The return of a matching operation depends on whether you are in list or scalar context, and whether or not you have /g. All 4 combinations have different behaviour, and I have found occasion to use them all. :-)
Enjoy,
Ben
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(meonkeys: lesson learned. Tusen tak!)
by meonkeys (Chaplain) on Aug 06, 2000 at 00:58 UTC |