Perl

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • no number return

    5 answers - 1192 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    PGP SIGNED MESSAGE
    Hash: SHA1
    I am trying to get this to take random amount of widths and compute
    the square footage for each. For example:
    Enter Total Square Footage: 1234
    Enter widths seperated by spaces
    3 4 5
    size 3: amount needed = 0
    size 4: amount needed =
    size 5: amount needed =
    below is the code I have so far:
    # Random width calculator
    $| = 1;
    print("Enter Total Square Footage: ");
    chomp($squareft = <>);
    $| = 0;
    print("Enter widths seperated by spaces: \n");
    while(@numbers = <>)
    {
    # Extract widths
    @width = split(" ", $numbers);
    $result = &calculate($width);
    print("size = $width amount needed = $result \n");
    }
    sub calculate{
    my $a = shift @_;
    my $value = $a / 12;
    my $b = $value * $squareft;
    return $b;
    }
    formula for random width is( for each width divide by 12, take that
    number and multiply by total square footage)
    Some pointers, hints, maybe a doc link anything would be appreciated.
    thanks again
    PGP SIGNATURE
    Version: GnuPG v1.4.5 (MingW32)
    NC8qEWPwlmX0y2jl4bfKQsw=
    =l97N
    PGP SIGNATURE
  • No.1 | | 594 bytes | |

    11/30/06, Chris Parker <chris_parker (AT) adelphia (DOT) netwrote:
    while(@numbers = <>)
    {

    # Extract widths
    @width = split(" ", $numbers);

    Here at this piece of code, @numbers and $numbers are different
    variables. And you never assign anything to $numbers. Probably you
    want this:

    while (<>) { # read a line
    my @numbers = split /\s+/, $_; # split the line (using spaces as
    the delimiter)

    Hint: Always use at the start of the script,

    use strict;
    use warnings;

    and introduce 'my' variable declarations.
  • No.2 | | 1403 bytes | |

    PGP SIGNED MESSAGE
    Hash: SHA1

    Adriano Ferreira wrote:
    11/30/06, Chris Parker <chris_parker (AT) adelphia (DOT) netwrote:
    >while(@numbers = <>) {
    >>

    ># Extract widths @width = split(" ", $numbers);
    >

    Here at this piece of code, @numbers and $numbers are different
    variables. And you never assign anything to $numbers. Probably you
    want this:

    while (<>) { # read a line my @numbers = split /\s+/, $_; # split
    the line (using spaces as the delimiter)
    --
    I added what you suggested and this is what I get now:

    Enter Total Square Footage: 1200
    Enter widths separated by spaces:
    3 4
    Argument "3 4" isn't numeric in division (/) at calc.pl line 27, <>
    line 2.
    size = amount needed = 300

    I am missing something, probably very obvious. It isn't getting the
    split numbers.

    Code is:
    while(<>)
    {
    # Extract widths
    chomp();
    my @numbers = split /\s+/, $_;
    my $result = &calculate;
    print("size = amount needed = $result \n");
    }

    sub calculate {
    my $a = $_;
    my $value = $a / 12;
    my $b = $value * $squareft;
    return $b;
    }

    thanks again for your help

    PGP SIGNATURE
    Version: GnuPG v1.4.5 (MingW32)

    wQsCL1u3pf+xsicNGds8ixA=
    =hq8M
    PGP SIGNATURE
  • No.3 | | 2114 bytes | |

    11/30/06, Chris Parker <chris_parker (AT) adelphia (DOT) netwrote:
    PGP SIGNED MESSAGE
    Hash: SHA1

    Adriano Ferreira wrote:
    11/30/06, Chris Parker <chris_parker (AT) adelphia (DOT) netwrote:
    >while(@numbers = <>) {
    >>

    ># Extract widths @width = split(" ", $numbers);
    >

    Here at this piece of code, @numbers and $numbers are different
    variables. And you never assign anything to $numbers. Probably you
    want this:

    while (<>) { # read a line my @numbers = split /\s+/, $_; # split
    the line (using spaces as the delimiter)
    --
    I added what you suggested and this is what I get now:

    Enter Total Square Footage: 1200
    Enter widths separated by spaces:
    3 4
    Argument "3 4" isn't numeric in division (/) at calc.pl line 27, <>
    line 2.
    size = amount needed = 300

    Sorry. I pointed the immediate problems but hasn't look deeply into
    what you were trying to do. You want to read many numbers input from
    STDIN and then make a loop for them. So you should remove the
    construction of @numbers from the loop, doing

    I am missing something, probably very obvious. It isn't getting the
    split numbers.

    Code is:
    while(<>)
    {
    # Extract widths
    chomp();
    my @numbers = split /\s+/, $_;
    my $result = &calculate;
    print("size = amount needed = $result \n");
    }

    my @numbers = split /\s+/, <>;
    for my $width (@numbers)
    my $result = &calculate($width);
    print("size = amount needed = $result \n");
    }

    I hope it helps more this time.

    while(<>)
    {
    # Extract widths
    chomp();
    my @numbers = split /\s+/, $_;
    my $result = &calculate;
    print("size = amount needed = $result \n");
    }
    --
    sub calculate {
    my $a = $_;
    my $value = $a / 12;
    my $b = $value * $squareft;
    return $b;
    }

    thanks again for your help
    --
    PGP SIGNATURE
    Version: GnuPG v1.4.5 (MingW32)

    wQsCL1u3pf+xsicNGds8ixA=
    =hq8M
    PGP SIGNATURE
    --
  • No.4 | | 1321 bytes | |

    PGP SIGNED MESSAGE
    Hash: SHA1

    Adriano Ferreira wrote:
    >
    >STDIN and then make a loop for them. So you should remove the
    >construction of @numbers from the loop, doing
    >
    >
    >

    Code now is:

    print("Enter widths separated by spaces: \n");
    my @numbers = split /\s+/, <>;
    for my $width (@numbers) {
    my $result = &calculate($width);
    print("size = $width amount needed = $result \n");
    }

    sub calculate {
    my $a = $_;
    my $value = $a / 12;
    my $b = $value * $squareft;
    return $b;
    }

    Here is the results:
    Enter Total Square Footage: 1200
    Enter widths separated by spaces:
    2 3 4
    Use of uninitialized value in division (/) at calc.pl line 23, <line 2.
    size = 2 amount needed = 0
    Use of uninitialized value in division (/) at calc.pl line 23, <line 2.
    size = 3 amount needed = 0
    Use of uninitialized value in division (/) at calc.pl line 23, <line 2.
    size = 4 amount needed = 0

    It is pulling the size out nicely, but the value isnt making it into
    the sub? I have tried my $a = $_[0] , a = @_, a = $width and am lost
    now.

    thanks again
    PGP SIGNATURE
    Version: GnuPG v1.4.5 (MingW32)

    UJUdqByp2gR9/rwo18Q1EC0=
    =5eIq
    PGP SIGNATURE
  • No.5 | | 1847 bytes | |

    Chris Parker am Donnerstag, 30. November 2006 14:57:
    Adriano Ferreira wrote:
    >STDIN and then make a loop for them. So you should remove the
    >construction of @numbers from the loop, doing
    >

    Code now is:

    print("Enter widths separated by spaces: \n");
    my @numbers = split /\s+/, <>;
    for my $width (@numbers) {
    my $result = &calculate($width);
    print("size = $width amount needed = $result \n");
    }

    Hello Chris

    sub calculate {
    my $a = $_;

    This does not what you think it does. You can see that by placing the
    following "poor man's debug statement" after this line:

    print "\$a=$a";

    The solution's first step is to replace $a and $b by $x and $y (or whatever),
    because $a and $b are special variables in perl, see perldoc -f sort.

    Then, the line should be

    my $x=shift;

    or

    my $x=$_[0];

    Read perldoc perlsub for how arguments are passed to the sub.

    There are other improvements and "do not"s in the code; your invited to get
    your script do what it should and then reposting it :-)

    Dani

    my $value = $a / 12;
    my $b = $value * $squareft;
    return $b;
    }

    Here is the results:
    Enter Total Square Footage: 1200
    Enter widths separated by spaces:
    2 3 4
    Use of uninitialized value in division (/) at calc.pl line 23, <line 2.
    size = 2 amount needed = 0
    Use of uninitialized value in division (/) at calc.pl line 23, <line 2.
    size = 3 amount needed = 0
    Use of uninitialized value in division (/) at calc.pl line 23, <line 2.
    size = 4 amount needed = 0

    It is pulling the size out nicely, but the value isnt making it into
    the sub? I have tried my $a = $_[0] , a = @_, a = $width and am lost
    now.

    thanks again

Re: no number return


max 4000 letters.
Your nickname that display:
In order to stop the spam: 1 + 0 =
QUESTION ON "Perl"

EMSDN.COM