Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Update: problem with scalar

by sargg55 (Novice)
on Apr 23, 2016 at 23:08 UTC ( [id://1161346]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, I seem to be having a problem with scalar. "Not enough arguments for scalar at a18.pl line 25, near "scalar;" Execution of a18.pl aborted due to compilation errors."
#!/usr/bin/perl use Modern::Perl; use strict; system 'clear'; my@array; my $counter=0; my $total=0; open FILE,'testFile.txt'; while (<FILE>) { chomp; $array[$counter]=$_; $total +=$_; $counter++ } close (FILE); foreach my $scalar(@array) { $total+=scalar; } printf("\nThe total of the numbers in testfile.txt is %d.\n\n",$total) +; my($add,$sub,$div,$mul,$mod)=0; $add=$array[0] +$array[1]; $sub=$array[0] -$array[3]; $div=$array[5]/$array[1]; $mul=$array[0]*$array[1]*$array[2]*$array[3]*$array[5]*$array[4]; $mod=$array[4]%$array[2]; print "ADD:$add\n"; print "SUB:$sub\n"; print "DIV:$div\n"; print "MUL:$mul\n"; print "MOD:$mod\n"; print ("The answer to 3%5+\(12/4\)-10/5*-3 is".(($array[0] %$array[1]) + +($array[2]/$array[4])-$array[5]/$array[1]*($array[3]))."\n"); open FILE2,'output.txt'; print FILE2 "ADD:$add\n"; print FILE2 "SUB:$sub\n"; print FILE2 "DIV:$div\n"; print FILE2 "MUL:$mul\n"; print FILE2 "MOD:$mod\n"; print FILE2 ("The answer to 3%5+\(12/4\)-10/5*-3 is".(($array[0]%$arra +y[1]) + ($array[2]/$array[4])-$array[5]/$array[1]*($array[3]))."\n"); close (FILE2); print"\nThe Program has terminated. data has been saved to output.txt\ +n\n"; ~

Replies are listed 'Best First'.
Re: Update: problem with scalar
by Athanasius (Archbishop) on Apr 24, 2016 at 03:09 UTC

    Hello sargg55,

    foreach my $scalar(@array) { $total+=scalar; }

    You’ve named the foreach variable $scalar, but in the loop body you’ve left off the $ sigil. Now, it so happens that scalar is a built-in function in Perl (it puts its argument into scalar context), so the interpreter thinks you’re calling the function — without an argument. Add the sigil:

    $total += $scalar;

    and it should compile cleanly. See scalar.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks !, fixed that, and now it spawned another problem
      Use of uninitialized value in addition (+) at a18.pl line 27. Use of uninitialized value in addition (+) at a18.pl line 27. Use of uninitialized value in subtraction (-) at a18.pl line 28. Use of uninitialized value in subtraction (-) at a18.pl line 28. Use of uninitialized value in division (/) at a18.pl line 29. Use of uninitialized value in division (/) at a18.pl line 29. Illegal division by zero at a18.pl line 29.
      sigh
        You should print out your @array to make sure it contains what you think it does:
        use Data::Dumper; # ... # build @array # ... print Dumper( \@array );
        Your @array variable is probably not properly populated and is likely to be empty (at least in part). This suggest that you might have an earlier problem reading the testFile.txt file.

        Perhaps you could change the file opening line as follows:

        open FILE, "<", 'testFile.txt' or die "Cannot open testFile.txt $!";
        This will not solve the problem but at least it would tell you if the program failed to open the file.

        BTW, you should always check whether such OS related statements succeed.

Re: Update: problem with scalar
by Athanasius (Archbishop) on Apr 24, 2016 at 08:42 UTC

    Another problem is that you open the output file like this:

    open FILE2,'output.txt';

    But with no mode specified, this defaults to reading (i.e., input mode), so each attempt to print to the file results in an error message like this:

    print() on closed filehandle FILE2 at ...

    As a general rule, you should prefer the three-argument form of open with the mode made explicit:

    open FILE, '<', 'testFile.txt'; ... open FILE2, '>', 'output.txt';

    And, as Laurent_R says, you should always check whether the open call succeeds. If you don’t want to write ... or die ... after each call, you can simply add the pragma:

    use autodie;

    at the head of your script. See autodie.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Problem when opening a file with perl
by stevieb (Canon) on Apr 23, 2016 at 23:29 UTC

    Welcome to the Monastery, sarg55!

    Please show the code you're using and having trouble with. Place the code within <code></code> tags.

Re: Update: problem with scalar
by graff (Chancellor) on Apr 24, 2016 at 19:33 UTC
    If I want to read array elements from lines of input, and I intend to treat each element as a numeric value, I'd load the array from the file like this (assuming that the input is coming from STDIN or from any file(s) named on the command line when I run the script):
    use Scalar::Util 'looks_like_number'; my @array; while (<>) { chomp; if ( looks_like_number( $_ )) { push @array, $_; } else { # skip this part if you don't care to know about bad input +... warn "Unusable input line skipped: $_\n"; } }
    It doesn't matter to me what the input looks like or where it comes from. When I expect it to be of a certain type, I test to see that it really is that type before I actually use it (i.e. I seldom treat data files or STDIN as "trusted" sources).

    UPDATE: Having played with that snippet a bit, I'm happy to notice that leading and trailing whitespace characters do not affect the results of "looks_like_number()" (i.e. it returns true for " 123 ", "\t-456\n", and so on). The same is true for arithmetic operators: you can add, subtract, multiply and divide numeric strings that have leading and trailing whitespace.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-24 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found