[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] reg exp fun



At 22:55 -0500 6/3/99, Scott Prince spoke thusly:
>I'm playing with a sub routine to parse a script and gather a list of
>variables, but there's one I can't seem to get a handle on.
>
># $q, is the string, in this case...
>#
># "#$hashref->{'hjkjg'} = "isahashreftoo";3sdfgdkjldskjd sdjkdfsklj"
>
>while ($q =~ /.*?\$(\w+)-.*?/)
>{
>$xref = $1;
>$q =~ s/\$$xref//;
>  if ((!(defined($hashes{$xref}))) && ($xref !~ /arrays|hashes|scalars/))
>  {
>  $hashes{$xref} = 1;
>  }
>}
>
>I just want to match '$hashref-' or '$hashref->'. I'm pretty sure that
>the problem is the '-'. Any ideas?

Hi Scott (and listers y'all)

I've just joined this list and so far I'm a bit boggled by the advanced
level of some of the posts I've seen, so it's nice to be able to weigh in
with a possible answer rather than a question (which is why most of us join
lists, isn't it?).

Scott, I think the problem is the fact that you have both types of quote in
this string.

Because of the {'hjkjg'} part, you can't enclose the string in '' quotes,
because Perl will think the string finishes after the {. You can, of course
use "" and escape everything that needs to be escaped, so if I change the
string to:

$q = "#\$hashref\->{'hjkjg'} = \"isahashreftoo\";3sdfgdkjldskjd sdjkdfsklj";

then I can test with the much simpler

while ($q =~ /\$\w+\->?/) {

and get a result in $& (no need for $1 here).

However, if you're parsing a script, those characters ain't gonna be
escaped (as you see, I had to escape the $ as well as the - and the "" to
make it work). If there's some way you can wrap it in 'custom quotes' (or
whatever they're called), you can make it behave that way too, eg:

$q = q[#$hashref->{'hjkjg'} = "isahashreftoo";3sdfgdkjldskjd sdjkdfsklj];

...but of course you'll need to hit on some character that's not likely
ever to occur in the string. Actually I used the ¤ (section) character, but
changed it to square brackets in case it got mangled in the e-mailing
process.

Good luck - I'll be interested to hear what you come up with.

 Cheers ... Chris
 ======================================================================
 Chris Sansom    -    Highway 57   -   Designs for the World Wide Web |
 chris@highway57.co.uk                     http://www.highway57.co.uk |
 ======================================================================



===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org