For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > April 2004 > Re: Use of uninitialized value in concatenation (.) or string Error









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Use of uninitialized value in concatenation (.) or string Error
Sukhbir Dhillon

2004-04-04, 9:31 pm

Hi! I'm trying to split the string from some file with | (pipe) seperated.
heres one of the string lines from data file.

Sukhbir Dhillon|sdhillon@bridgeport.edu|data.txt|Thank you|student|
I'm using following code to read and split
open (READ_DB, "$dbfile");
while (<READ_DB> )
{
$_ =~ tr/A-Z/a-z/;
($name, $value, $filename, $message, $position) = split(/\|/);
close(READ_DB);

and I found that $position is getting undef value and which gives lots of error mentioned in the subject.
I'm pretty new in perl so please excuse my ignorance.
Thank you
Sukhbir

Gunnar Hjalmarsson

2004-04-04, 10:32 pm

[ Reply not sent to the defunct group comp.lang.perl. ]

Sukhbir Dhillon wrote:
> Hi! I'm trying to split the string from some file with | (pipe)
> seperated. heres one of the string lines from data file.
>
> Sukhbir Dhillon|sdhillon@bridgeport.edu|data.txt|Thank you|student|
> I'm using following code to read and split
> open (READ_DB, "$dbfile");
> while (<READ_DB> )
> {
> $_ =~ tr/A-Z/a-z/;
> ($name, $value, $filename, $message, $position) = split(/\|/);
> close(READ_DB);
>
> and I found that $position is getting undef value and which gives
> lots of error mentioned in the subject.


Besides the fact that the above code does not compile, I don't see why
it would result in that *warning*.

Please write a *short* but *complete* program that illustrates your
point, and *copy* (don't retype) it into a new message.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Joe Smith

2004-04-04, 11:30 pm

Sukhbir Dhillon wrote:

> Sukhbir Dhillon|sdhillon@bridgeport.edu|data.txt|Thank you|student|
> I'm using following code to read and split
> open (READ_DB, "$dbfile");
> while (<READ_DB> )
> {
> $_ =~ tr/A-Z/a-z/;
> ($name, $value, $filename, $message, $position) = split(/\|/);
> close(READ_DB);
>
> and I found that $position is getting undef value


Of course it is. Your program should expect that, and do something like
$message = "" unless defined $message;
$position = "" unless defined $position;

-Joe
Sukhbir Dhillon

2004-04-05, 12:31 am

On Mon, 05 Apr 2004 03:35:23 +0200, Gunnar Hjalmarsson wrote:

> [ Reply not sent to the defunct group comp.lang.perl. ]
>
> Sukhbir Dhillon wrote:
>
> Besides the fact that the above code does not compile, I don't see why
> it would result in that *warning*.
>
> Please write a *short* but *complete* program that illustrates your
> point, and *copy* (don't retype) it into a new message.



I'm attaching the data file and my module.
data.txt

Sukhbir Dhillon|sdhillon@somemail.com|data.txt|Thank you|manager|
sony Dhillon|sdhillon@bridgeport.edu|data.txt|Thank you|engineer|

perl module.

#!/usr/bin/perl -w
$dbfile="data.txt";
#system "clear";
open (READ_DB, "$dbfile");
while (<READ_DB> )
{
$_ =~ tr/A-Z/a-z/;
($name) = split(/\|/, $_);
print "$name\n";
}
close(READ_DB);
print "\n\nwhich name you want to look up in database : ";
$findname=<STDIN>;
chomp($findname);

open (READ_DB, "$dbfile");
$found = 'false';
while (<READ_DB> )
{
#chomp;
$_ =~ tr/A-Z/a-z/;
($name, $value, $filename, $message, $position) = split(/\|/);
($first,$last) = split(/ /, $name);
if (($name eq $findname)||($first eq $findname)||($last eq $findname)){
print "\nFirstname = $first\n"."Lastname = $last\n";
print "Email = $value\n"."Filename = $filename\n";
print "Message = $message\n";
print "Position = $positon\n";
$found = $findname;
}
}
if ($found ne $findname) {
print "$findname not found in Database\n"; }
close(READ_DB);

###### EOF perl file

Thanks
Sukhbir Dhillon
user@domain.invalid

2004-04-05, 12:31 am

Gunnar Hjalmarsson wrote:
> [ Reply not sent to the defunct group comp.lang.perl. ]
>
> Sukhbir Dhillon wrote:
>
>
>
> Besides the fact that the above code does not compile, I don't see why
> it would result in that *warning*.
>
> Please write a *short* but *complete* program that illustrates your
> point, and *copy* (don't retype) it into a new message.
>

I'm attaching my program module and sample data file.
Thanks

Gunnar Hjalmarsson

2004-04-05, 12:31 am

Sukhbir Dhillon wrote:
> Gunnar Hjalmarsson wrote:
>
> I'm attaching the data file and my module.


Okay. It's not a module, it's a program.

I can tell you that the failure is caused by a typo, which my version
of Perl lets me know about via warning messages.

Whether your Perl version displays those messages or not, I suggest
that you rewrite your program so that you add

use strict;

as the second line, and declare all the variables using my(). That way
you will
1) learn something that you should always practice when writing
Perl programs, and
2) find the typo. :)

Good luck!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com