Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Missing $ on loop variable at measurements2.pl line 69.

by Yoda_Oz (Sexton)
on Sep 08, 2006 at 11:50 UTC ( [id://571935]=perlquestion: print w/replies, xml ) Need Help??

Yoda_Oz has asked for the wisdom of the Perl Monks concerning the following question:

i dont't know where im going wrong. can sombody please tell me what this means?
Missing $ on loop variable at measurements2.pl line 69.
here is my code...
#!/usr/local/bin/perl use strict; use warnings; my $path='CustData.txt'; my @range = ("10-20", "20-30", "30-40", "41-50", "51-60", "61-70", "71 +-80","81-90", "91-99"); my @count; my @percent; my %HoH; open(DATA, "<$path") || die "Couldn't open $path for reading: $!\n"; my $output; open my $fd, '>', \$output or die "open: $!"; while (<DATA>) { chomp; my ($id, $title, $surname, $forename, $sex, $dob, $vision) = split +(/,/); $HoH{$id} = {'title' => $title, 'surname' => $surname, 'forename' +=> $forename, 'sex' => $sex, 'dob' => $dob, 'vision' => $vision,}; } close(DATA); my $range1=1; my $range2=10; my $range3=1; my $range4=10; my $person_count=0; sub personCount { my ($x,$y) = @_; my ($vision_total,$person_count)=0; foreach my $id (sort keys %HoH) { if( ($HoH{$id}->{'vision'}>=$x) & ($HoH{$id}->{'vision'}<=$y) +) { ++$person_count; $vision_total=$vision_total+$HoH{$id}->{'vision'}; } } return int($person_count); } sub percent { my ($x,$y) = @_; my $percent = 0; my ($total_person_count,$vision_total,$person_count)=0; foreach my $id (sort keys %HoH) { ++$total_person_count; } foreach my $id (sort keys %HoH) { if( ($HoH{$id}->{'vision'}>=$x) & ($HoH{$id}->{'vision'}<=$y) +) { ++$person_count; $vision_total=$vision_total+$HoH{$id}->{'vision'}; } } return $percent = ($person_count/$total_person_count)*100; } print $fd ("Range\t"); foreach (@range) { printf $fd ("%7s", $_); } print $fd ("\n"); for my ($counter=0;$counter<9;$counter++) { $range3=$range3+10; $range4=$range4+10; my $personCount = personCount($range3,$range4); push (@count, $personCount); } print $fd ("Count\t"); foreach (@count) { printf $fd ("%7s", $_); } print $fd ("\n"); for my ($counter=0;$counter<9;$counter++) { $range1=$range1+10; $range2=$range2+10; my $percent = sprintf("%.0f", percent($range1,$range2)); push (@percent, $percent); } print $fd ("Percent\t"); foreach (@percent) { printf $fd ("%7s", $_); } print $fd ("\n"); print $fd ("\n"); close $fd;

Replies are listed 'Best First'.
Re: Missing $ on loop variable at measurements2.pl line 69.
by gellyfish (Monsignor) on Sep 08, 2006 at 11:56 UTC

    I think you'll find it's the:

    for my ($counter=0;$counter<9;$counter++)
    construct (which you have two of.) You either want to move the move the my inside the brackets like:
    for (my $counter=0;$counter<9;$counter++)
    Or even better use the more perl-ish
    foreach my $counter (0 .. 8)

    /J\

Re: Missing $ on loop variable at measurements2.pl line 69.
by Velaki (Chaplain) on Sep 08, 2006 at 11:58 UTC

    You need to bring the my inside the for statement for each offending line in order to declare the variable.

    for my ($counter=0;$counter<9;$counter++)
    needs to be
    for (my $counter=0;$counter<9;$counter++)

    Hope this helped,
    -v.

    "Perl. There is no substitute."
Re: Missing $ on loop variable at measurements2.pl line 69.
by davorg (Chancellor) on Sep 08, 2006 at 11:59 UTC

    I expect that the line in question is this one:

    for my ($counter=0;$counter<9;$counter++)

    You're making two mistakes there. Firstly you're confusing "for" loops with "foreach" loops. The "for" loop syntax doesn't require a loop variable. And secondly, you have the "my" that is going to declare a loop variable, but you don't actually declare the variable.

    You probably meant to write:

    for ($counter=0;$counter<9;$counter++)

    But that's probably easier to understand if it's written as:

    foreach my $counter (0 .. 8)

    It's worth pointing out that the perldiag man page has complete descriptions of all Perl diagnostic messages.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      sorted thanks!
      however...
      now i get:
      Use of uninitialized value in int at measurements2.pl line 42. Use of uninitialized value in int at measurements2.pl line 42. Use of uninitialized value in int at measurements2.pl line 42. Use of uninitialized value in int at measurements2.pl line 42. Use of uninitialized value in int at measurements2.pl line 42. Use of uninitialized value in int at measurements2.pl line 42. Use of uninitialized value in int at measurements2.pl line 42. Use of uninitialized value in division (/) at measurements2.pl line 63 +. Use of uninitialized value in division (/) at measurements2.pl line 63 +. Use of uninitialized value in division (/) at measurements2.pl line 63 +. Use of uninitialized value in division (/) at measurements2.pl line 63 +. Use of uninitialized value in addition (+) at measurements2.pl line 60 +. Use of uninitialized value in division (/) at measurements2.pl line 63 +. Use of uninitialized value in division (/) at measurements2.pl line 63 +. Use of uninitialized value in division (/) at measurements2.pl line 63 +. Use of uninitialized value in addition (+) at measurements2.pl line 60 +.

        That means that in various calculations (on the indicated lines) you are trying to use a value from a variable that you haven't previously put a value in. This is often indicative of a problem in the logic of your program.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        A reply falls below the community's threshold of quality. You may see it by logging in.
        this happens because you haven't assigned any value to $person_count (only to $vision_total).
        try changing my ($vision_total,$person_count)=0; to my ($vision_total,$person_count)=(0,0);


        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        "one who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever."

        mk at rio dot pm dot org
Re: Missing $ on loop variable at measurements2.pl line 69.
by imp (Priest) on Sep 08, 2006 at 12:42 UTC
    In this particular case it was easy for us to find the error, but in the future please only post the smallest segment of relevant code that still produces the error. Please read How (not) to ask a question - Only Post Relevant Code for an explanation.

    If you aren't sure what code is relevant, start with the line number that was reported as having the error. The error message you reported said line 69, but when I run the code locally perl reports that the failure is on line 74 - which means the code provided is not the code that triggered the error.

    Line 74 is:

    for my ($counter=0;$counter<9;$counter++)
    Which you could reduce to a minimal working example by just providing an empty loop body:
    for my ($counter=0;$counter<9;$counter++) { }
    Many times you will find that when you reduce the script to the smallest segment that still produces the error, the cause of the error becomes obvious.

    Note that sometimes the reported line number is misleading, which is often the case when the issue is a missing (or extra) curly bracket, quote, or semi-colon. In those cases you need to iteratively reduce the code until the error no longer occurs, then backstep to see what block contained the error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://571935]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found