SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    BLYAT! theGWS's Avatar
    Join Date
    Jun 2001
    Location
    Melbourne, AU & Seattle, WA
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What's this error? Use of uninitialized value in concatenation (.) or string...

    Can anyone tell me why I get the following error message when running this script?

    Use of uninitialized value in concatenation (.) or string at line 15.

    Code:
    #!/usr/bin/perl -w
      
      use strict;
      
      sub total
      {
      	foreach(@_)
      	{
      		my $total += $_;
      	}
      	return;
      }
      
      my $grand_total = &total(1..1000);
      print "$grand_total \n";
    ~Dorian

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the warning is refering to this line:

    print "$grand_total \n";

    $grand_total has no value (it's uninitialized) . Your code is not written correctly, as you may have noticed since it prints nothing when run.

    this line is properly coded:

    my $grand_total = &total(1..1000);

    you can leave off the & sign though for cleaner looking code:

    my $grand_total = total(1..1000);

    the problem begins in sub total(). You are using strict (a good thing!) so $total is not visible outside the sub and you never pass it back to the calling routine, you just have "return;" at the end of the sub, which returns nothing (sort of). But you also have:

    my $total += $_;

    inside the foreach loop. That is also not correct. $total can't be seen outside the foreach loop, which is a block:

    Code:
    foreach() {
       block of code here
    }
    this is a scoping problem. Variables only are scoped (visible) to the block they are in when use strict is in place (as it should be).

    so you have a scoping problem as well as a problem I'm not sure how it's called. You can't say:

    my $total += $_;

    at least not the way you have it. $total will be redefined each time the loop is run and end up with the last value of $_.

    Pass your variable to the sub routine (your list of numbers in this case), initiate $total before the foreach block so it's scope will also be outside the foreach block but not outside the sub rotuine block, and return the value back to the calling routine:

    Code:
      sub total
      {
       my $total = 0;
      	foreach(@_)
      	{
      		$total += $_;
      	}
      	return($total);
      }
      
      my $grand_total = total(1..1000);
      print "$grand_total \n";
    or more succintly:

    Code:
      sub total
      {
       my $total = 0;
       $total+=$_ for @_;
       return($total);
      }
      
      my $grand_total = total(1..1000);
      print "$grand_total \n";

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fun!

    #!/usr/local/bin/perl -l

    use warnings;
    use strict;

    sub total { eval eval join '+' => @_ }

    print total (1..5)

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is niffty code but you have to admit to the casual perl user it's probably going to be incomprehensible. If that was for my benefit I aprreciate it, if it's for the benefit of theGWS I have a feeling it's just voodo to him/her at this point, they are still battling with passing arguments and variable scoping.

  5. #5
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi there im getting a error kinda like that also

    use warnings;
    use strict;
    use CGI;

    my $script_URL = "http://zephir.seattlecentral.edu/~ajohns30";
    my $q= new CGI;
    print<<END_OF_HTML;
    Content-type:text/html
    <html>
    <head>
    <title>
    </title>
    </head>
    <body>
    END_OF_HTML
    my $name = $q->param("Name");
    my $phone = $q->param("Phone");
    my $zip = $q->param("Zip");
    print<<END_OF_HTML;
    <p>
    The following information has been submitted:
    <br/>
    The name is $name </p>
    <p>
    The phone number is $phone </p>
    <p>
    The zip code is $zip </p>
    </body>
    </html>
    END_OF_HTML

    sub showform {
    print "<form action=$script_URL method=\"get\">\n";
    print "Name: <INPUT TYPE=text name=name size=25 value=\"$name\">\n";
    print "Phone: <INPUT TYPE=text name=phone size=12 value=\"$phone\">\n";
    print "Zip: <INPUT TYPE=text name=zip size=5 value=\"$zip\">\n";
    print "<INPUT TYPE=submit value=\"SUBMIT\">\n";
    print "<INPUT TYPE=reset value=\"RESET\">\n";
    if ($name =~ /^[A-Z_]\w$/i) {
    print "All of the data has been formatted correctly.\n"
    }else {
    print "The data in the name field is not formatted correctly. \n"
    }
    if ($phone =~ /^[0-9_]\w$/i) {
    print "All of the data has been formatted correctly.\n"
    }else {
    print "The data in the phone field is not formatted correctly.\n"
    }
    if ($zip =~ /^[0-9_]\w$/i) {
    print "All of the data has been formatted correctly.\n"
    }else {
    print "The data in the zip code field is not formatted correctly..\n"
    }

    }

    im getting the error at line 22...can someone pls help me pls...thanks
    you can email me...maynard48@comcast.net

    great board

  6. #6
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for maynard49's code, you need another newline after the content type, like this:

    print<<END_OF_HTML;
    Content-type:text/html

    <html>
    <head>
    <title>
    </title>
    </head>
    <body>
    END_OF_HTML


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •