Lance

I'm not sure exactly what you're trying to do. I don't have a copy of the
book, but it's clear that the file is prescibed to have standard scalars
alone on each line with no whitespace except the line terminator.

See in line.

"Lance Murray" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Hello:
>
> I'm trying to work through the "Learning Perl" Llama book, but I'm a bit
> stumped on one of the regex exercises (ch 8.6 #3).  The solution at the
back of
> the book doesn't work if you don't presume anything for input.
>
> Anyway, what the book asks is to write a regular expression that will
parse a
> file for scalar variables.  I've found its not so easy, particularly if
you
> don't want to presume anything on the input, e.g.:
>
> # Given a file, e.g.: ~/filetoparse.txt  (exclude comments)
> $0                   # "shouldn't match special variables like $0"
> $the1stScalar        # OK

Fine.

> $$notaScalar         # improper syntax

Not sure what you mean by 'improper syntax'. It's valid in a Perl program as
a dereferenced scalar reference:

    $isaScalar;
    $notaScalar = \$isaScalar;

>   $the2ndScalar .    # preceeding or trailingspaces or tabs OK

OK

> thisis$notaScalar    # improper syntax

Well, again, it depends on the context. This is valid in Perl as

    print "thisis$notaScalar";

where $notaScalar is replaced with the stringified version of its value.

> $the3rdScaler = $the4thScalar  # multiple variables OK

So it seems that you're trying to find all the scalar variables that Perl
would find in a source file.

> $thisIsNOT@scalar $the5thScaler the end.  # invalid character (@)

Again,

    print "$thisIsNOT@scalar";

would stringify both the scalar value $thisisNOT and the array value
@scalar.

>
> # The output I'm trying to get to:
> $the1stScalar
> $the2ndScalar
> $the3rdScaler
> $the4thScalar
> $the5thScaler
>
> Anyway, I've racked my brain for days, and I still can't get very close.
A key
> issue seems to be matching a variable at the beginning of the line (^
anchor?)
> OR preceded by \s character.  This is the code/regex snippets I"m using to
test
> my attempts, and few of the examples I've tried (commented out:
>
> #!/usr/usperl -w
> ### Yes its ugly, but it all works for my purposes here
> use strict;while(<>){chomp;if(
> ### Uncomment regex to test
> # /\$([\w])+/
> # /\$[a-zA-Z_]+[a-zA-Z_\d]*/
> # /\s\$[a-zA-Z_]+[a-zA-Z_\d]*/
> # /( |\t|^\w)\${1}[a-zA-Z_]+[a-zA-Z_\d]*\b/
> /^\$[A-Za-z_]\w*$/    # example from back of book doesn't work well
> ###
> ){print "$&\t\t$`$&$'\n";}}
>
>
> Maybe somebody has some ideas I haven't thought of?  I could probably
spring
> for a Carl's Jr "$6 dollar burger & Lord of the Rings "The Two Towers"
ticket
> (all via-Paypal) if anyone has a solid solution.

Overall, it seems that you're suffering from the old programmer's problem of
not having defined the problem fully. If you want to simply find all the
scalar-like identifiers separated by whitespace in a file, then the book
solution is almost correct. If, as I said, you want to find everything that
Perl would find, then you must consider, do you find:

    $scalar
    ${scalar}
    $name = 'scalar'; ${$name}
    $array[0]
    $hash{key}
    $a, $b (as 'special' as $0, $& etc.)
    "\$justsometext"

...and a few more that don't immediately spring to mind.

Cheers,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to