On Wed, 16 Jan 2002, Kuhnibert wrote: > > ok guys, > > i need a starter on this one. > have a file which looks like this > > name: @a age: @b > gender: @c bla: @d blub @e > blabla: @f > > and so on ... > > the @ signs are placeholders for a template and mark the position for later > entries. they have always 1 alphanumeric digit, followed by either one or > multiple spaces or a new line. > after scanning the file, i want to end up in having a hash with the @ sign's > char as a key and the column and row in which it occured in the file as a > value, e.g. for above: > > $pos{a}->{col}= 9; > $pos{a}->{row}= 1; > $pos{b}->{col}= 18; > $pos{b}->{row}= 1; > $pos{c}->{col}= 9; > $pos{c}->{row}= 2; ... > > probably a regex can do this, but i don't know how to find out the position > on which a regex matched within a string, furthemore the multiple occurences > of placeholders within one line are causing me a headache ... >
The position of a regex match will be the length of the "stuff" before the match plus one. In a regex, the "stuff" before the match is $` or, if uou use the English module, $PREMATCH. I would code it like this: $row=0; while($line=<INFILE>) { $row++; while( $line=/@([a-zA-Z0-9])\s+/g) { my $item=$1; $pos{$item}->{col}=length($`)+1; $pos{$item}->{row}=$row; } } Note, there is no check for duplicate identifiers. With the code as I've shown it, the row and column values for an item will be for the last seen copy of that item. **** [EMAIL PROTECTED] <Carl Jolley> **** All opinions are my own and not necessarily those of my employer **** _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users