Hi Jeswin,
On Thu, 29 Dec 2011 11:40:09 -0500
Jeswin <[email protected]> wrote:
> Hi,
> I'm doing one of the Project Euler exercises which asks the sum of 100
> fifty-digit numbers[1]. My code is 2 parts:
>
> I open the text file containing the digits and put into an array:
> ========================BEGIN_CODE=========================
> open (F, "Fifty_hundred.txt") || die "Could not open Fifty_hundred.txt: $!\n";
>
1. You should use lexical file handles.
2. You should use three arguments open.
open (my $fh, '<', "Fifty_hundred.txt") or die "Could not open
Fifty_hundred.txt: $!".
> @numbers = <F>;
It would be a better idea to read it line by line, using while.
> close F;
> ========================END_CODE===========================
>
> Next I sum the values in the array:
> ========================BEGIN_CODE=========================
> $sum = 0;
> for (@numbers) {
> $sum += $_;
> }
> ========================END_CODE===========================
>
> I get an answer but its wrong (according to Project Euler). Now I am
> looking into the BigNum functions but I am not sure how they work and
> don't really know how to use it.
See: https://metacpan.org/module/Math::BigInt - you might need some
introduction to object-oriented Perl. You can find it in the book "Modern Perl"
and in other resources - http://perl-begin.org/topics/object-oriented/ .
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/
Whitespace in Python is not a problem: just lay out all the whitespace first,
then add the code around it.
— sizz on Freenode's #perl
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/