in reply to Read In A File, Manipulate It, Spit It Back Out

I tried essentially what the same code that you posted, and in looking at it, I saw that you are trying to use $1 in the substitution regex. As far as I can see, using a regex resets the $n($1,$2,etc.) variables all to undefined regardless of whether they are reused or not. Try assigning your $1 variable to a $tmp variable or something like this:
while (<FH>) { while ( $_ =~ m/(?:\s|\W)*([A-Za-z0-9_\.\:]+)(?:\s|\W)*/g ) { if ( exists $hash{$1} ) { $tmp = $1; $_ =~ s/$tmp/$hash{$tmp}/; } } $data .= $_; }
Hope that helps.

*NOTE: I'd normally be using 'strict' btw. ;)

Amel - f.k.a. - kel

Replies are listed 'Best First'.
Re: Re: Read In A File, Manipulate It, Spit It Back Out
by dvergin (Monsignor) on Jul 07, 2001 at 10:31 UTC
    using a regex resets the $n($1,$2,etc.) variables all to undefined regardless...

    As it happens, this is not always the case. It is true that if the second regex succeeds, unused numbered variables from a previous regex do become undefined -- even if the new regex contains no capturing paren's.

    But in the case given in the question, the $1 will not be undefined until after the second regex is evaluated. So the $1 in the second regex will reflect the value from the first regex if it succeeded. Example:

    my $str = "a2b c34d"; $str =~ /(\D\d\D)/; print "$1\n"; # prints: 'a2b' $str =~ s/$1/NEW/; print "$str\n"; # prints: 'NEW c34d'

    As different hazzard, (distinct from the example in the stated question) if the second regex fails, the previous values are retained (not undefined). This can cause intermittent problems that can be hard to trace. Consider:

    my $str = "a2b c34d"; $str =~ /(\D\d\D)/; print "$1\n"; # prints 'a2b' $str =~ /(xyz)/; print "$1\n"; # prints 'a2b' again
Re: Re: Read In A File, Manipulate It, Spit It Back Out
by JojoLinkyBob (Scribe) on Jul 07, 2001 at 01:53 UTC
    Thanx, I'll have to remember that. Desert coder