Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Defining Arrays

by skullbowl (Monk)
on May 17, 2001 at 15:53 UTC ( #81203=perlquestion: print w/replies, xml ) Need Help??

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

How do I define an Array to contain a list of sequencing numbers from the lowest to the highest with an increment of 0.1 ? e.g I want the numbers (1.5,1.6,1.7,1.8,1.9,2.0 to 9.4). When i used the following code, it didn't work.

@results=(1.5..9.4);
use Text::Soundex; print soundex ("Skullbowl");
RESULT : S414

Edit: chipmunk 2001-05-17

Replies are listed 'Best First'.
Re: Defining Arrays
by andye (Curate) on May 17, 2001 at 16:14 UTC
    Something like this?
    my @ary = map {$_ / 10} (15...94) ;
    update: something like my @ary = map { sprintf("%.1f", $_ / 10) } (15...94) ; if you want to limit it to one decimal place.
      Going one step further to prevent floating point math problems:
      my @ary = map { int ($_ / 10) . '.' . ($_ % 10) } (15..94);

      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

        FYI, I see no advantage to this method whatsoever.

                - tye (but my friends call me "Tye")
(jeffa) Re: Defining Arrays
by jeffa (Bishop) on May 17, 2001 at 16:00 UTC
    Here is an evil way with that uses a closure that I just read about in the Damian's book Object Oriented Perl :
    use strict; use vars qw($iterator $next); $iterator = iterate(0.1,9.4,0.1); while ($iterator->($next)) { # the value is in $next, but since we are # iterating by a fraction, some numbers will # not be rounded up (e.g. 4.999999 instead of 5.0) my ($rounded) = $next =~ /^(\d+(\.\d)?)/; $rounded .= '.0' unless length $rounded > 1; print "$rounded\n"; } sub iterate { my ($from,$to,$step) = @_; my $next = $from - $step; my $cref = sub { $next += $step; return if $next > $to; $_[0] = $next; return 1; }; return $cref; }
    Eat your heart out, Java.

    UPDATE:
    Thanks to alfie and andye for catching the rounding error. Actually, i didn't post this for it's practical use, andye's solution is much more consise and hits the nail on the head - i just thought this was way cool!

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Defining Arrays
by Masem (Monsignor) on May 17, 2001 at 15:59 UTC
    The array incrementer will only work for intergers, and will only add one to the values during generatation.

    It's just as easy to do something like this:

    my @results; my $value = 1.5; my $max = 9.4; my $increment = 0.1; push @results, $value while ( ($value += $increment) && $value < $max +);

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Defining Arrays
by TheoPetersen (Priest) on May 17, 2001 at 16:17 UTC
    You can use the .. operator, just munge the results with map:

    @results = map {$_ / 10} (15 .. 94);

      Thanks for the many solutions. I was thinking that Andye's post has a typo error when he used
      (15...94)
      the {..} operator has an additional {.}! But it works fine.

      Learnt 2 things !

      use Text::Soundex; print soundex ("Skullbowl");
      RESULT : S414
        skullbowl - the difference between '..' and '...' is that '...' includes the final value.

        So (1...3) will give you (1,2,3) whereas (1..3) will give you (1,2). Can be a crucial difference, as in Lethal Weapon.

        andy.

        update: Dave's right. Forget you've read this node - apart from the Lethal Weapon reference. You can remember that if you want, but forget the rest - it's wrong.

Re: Defining Arrays
by alfie (Pilgrim) on May 17, 2001 at 16:07 UTC
    The .. operator steps by one, you can't change that behavior. But you can do it with a for loop:
    my @results; for (my $i=0; $i <= 79; $i++) { push @results, ($i/10+1.5); }
    It seems a bit awkward but it give's you the right result. Please notice that I didn't loop from 1.5 to 9.4 with a stepping of .1 for that would get imprecise (I got with a quick perl -e test 5.99999999999999 instead of 6). You can't go precisely if you are calculating with floats.
    --
    use signature; signature(" So long\nAlfie");
Re: Defining Arrays
by ckohl1 (Hermit) on May 17, 2001 at 16:07 UTC
    A verbose method:
    use strict; my @array; for (my $i = 1.5; $i < 9.4; $i += 0.1){ push(@array,sprintf("%.1f",$i)); } foreach (@array){ print "Value: $_\n"; } exit;



    Chris
    'You can't get there from here.'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-05-28 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (99 votes). Check out past polls.

    Notices?