CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Use of uninitialized value problem in Perl script

 



digioleg54
User

Jun 9, 2017, 8:37 AM

Post #1 of 3 (1573 views)
Use of uninitialized value problem in Perl script Can't Post

I continue my last post. I have a code:

Code
foreach my $line (@records) { 
print "We are in the record $line\n";
chop;
if (/ TBA/) {
my$ln_tp=$_;


It is reading line and print it, however in the error_log I see

Code
Use of uninitialized value $_ in scalar chop at tests1.pl line 47. 
Use of uninitialized value $_ in pattern match (m//) at tests1.pl line 48.

47 - chop, 48 if

Could you please correct the code, if it is wrong?
This is one of the line

Code
CUSIP|20170504|DMG |100.6340625||1544000 We are in the record CUSIP|20170504|DMG |100.6340625||1544000


The code is not mine, I have to fix it. And I don't understand what is

Code
 if (/ TAB/

. It is trying to compare something with TAB? I don't see, what can be compare like that
Thanks


(This post was edited by digioleg54 on Jun 9, 2017, 9:14 AM)


Laurent_R
Veteran / Moderator

Jun 9, 2017, 11:14 AM

Post #2 of 3 (1564 views)
Re: [digioleg54] Use of uninitialized value problem in Perl script [In reply to] Can't Post

Hi,

this:

Code
if (/ TBA/) {

is trying to match the / TBA/ regex in the string contained in $_, which would work if you used the $_ topical variable to read your array. For example with code like this:


Code
foreach (@records) {  
print "We are in the record $_\n";
chomp;
if (/ TBA/) {
my$ln_tp=$_;
}
}


But since you are storing the records in the $line variable, you need to use it for the regex and for the assignment on the next line:


Code
foreach my $line (@records) {  
print "We are in the record $line\n";
chomp $line;
if ($line =~ / TBA/) {
my$ln_tp = $line;
}
}


Note that I used chomp instead of chop, because this is most probably what you want (remove trailing new line character). chop would remove that last character even if it isn't a new line character, so this is somewhat dangerous when all you want to do is to remove new line characters.


BillKSmith
Veteran

Jun 9, 2017, 2:48 PM

Post #3 of 3 (1560 views)
Re: [digioleg54] Use of uninitialized value problem in Perl script [In reply to] Can't Post


Quote
The code is not mine, I have to fix it. And I don't understand what is

Code


if (/ TAB/



. It is trying to compare something with TAB? I don't see, what can be compare like that


Refer to the match operator in perlop. (Emphasis mine)

Quote
m/PATTERN/msixpodualngc
/PATTERN/msixpodualngc
Searches a string for a pattern match, and in scalar context returns true if it succeeds, false if it fails. If no string is specified via the =~ or !~ operator, the $_ string is searched.


You really should have been able to find this yourself.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives