Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with a regex erroring on $1 variable

Status
Not open for further replies.

Kathy1

Programmer
Dec 21, 2000
39
0
0
US
I'm having problem with the regex in the following bit of code:

#capitalize the first character of the first name
$varFirstName = ("First_Name" . $nameSub);
$in{$varFirstName} =~ s/\b(\w)/\U$1/g;

It is giving me this error:
[Fri Jun 15 07:04:08 2001] C:\prod\nameUpdate.pl: Use of uninitialized value at C:/perl/lib/name.pm line 463.

Through use of prints, I've gotten the error narrowed down to the $1 variable. I've done this by doing a 'print' on that variable prior to the problem line of code, which then gives me 2 errors like the one listed directly above this paragraph. If I do a print on the $in{$varFirstName} prior to the problem line of code, I don't get the extra error, but do get the value in that variable. And that particular variable is initialized directly above the problem line of code, as you can see.

Since the $1 variable isn't a variable I can alter or initialize (I tried that), is there some way I can get rid of the error message I'm receiving? It is filling up my log files.

Any help would be appreciated, as we are using this capitalization regex in several places, and it is affecting several log files.

Thanks!

Kathy :cool:
 
I'm probably just not understanding your problem, but it doesn't make sense to me. You are:

1. assigning the value of first name to $varFirstName
2. binding a substitution to the $in hash with
key $varFirstName - this doesn't make sense since
you haven't defined the $in hash - what you have
defined is the $varFirstName variable.

I'm not sure what the purpose of the $in hash is, but I assume you want the result to be stored in $in{$varFirstName} - try this:

$in{FirstName} = ("First_Name" . $nameSub);
$in{FirstName} =~ s/\b(\w)/\U$1/g;

or

$varFirstName = ("First_Name" . $nameSub);
$varFirstName =~ s/\b(\w)/\U$1/g
$in{$varFirstName} = $varFirstName;

The "=~" binds the preceeding variable to the substitution - so the preceeding variable needs to contain a value that the substitution will act upon. In your example, the substitution is bound to the hash element $in{$varFirstName} - in the previous statement you gave $varFirstName a value, but not $in{$varFirstName}, so the error is telling you that it can't find the $1 in $in{$varFirstName} since $in{$varFirstName} has *NO* value.

You need to understand the difference between $varFirstName and $in{$varFirstName}. If you don't, read up on perl data structures by doing

perldoc perldata

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
HTH:

Thank you for your suggestion...I will try changing things a little.

Let me explain more clearly what I'm doing. In the variable $varFirstName, I'm assigning a name field which can occur multiple times, coming in from my HTML page. The field is kept in a hash named %in, which is updated with the HTML field names and their corresponding values, by a subroutine. This %in hash is created in a subroutine called &readParse, which comes in the normal "use CGI" set of subroutines. That subroutine has been performed, and the hash has its values already set up prior to this point in my code.

The name of the First_Name field changes on the hash. The hash might look like this: %in = ("First_Name1", "Joe", "First_Name2", "Fred", "First_Name3", "Julie", "First_Name4", "Sarah");

This is because of the multiple occurances of names. Rather than hardcoding each First_NameX value, which would make my code longer, I loop through a routine that increments the $nameSub subscript, and then accesses the corresponding value from the %in hash.

It is kinda complicated, but my system is doing this with TONS of fields. We have drivers, vehicles, loss information, etc... all set up this way. The %in array itself is not the problem...its just that the regex doesn't seem to like it or something.

I'm going to try to move my hash value to a work field, then do the capitalization and move it back to the hash to see if that doesn't get rid of the error.

Thanks for your suggestion. I'll keep you posted on results.

Kathy :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top