in reply to Validate array of numbers in sequence

timtowtdi :)
#!/usr/bin/perl use strict; use warnings; my @list = qw( 1 3 5 4 2 6 7 8 9); my %hash; @hash{@list}=1; delete $hash{$_} for (1..@list); print "is sequence\n" unless %hash;

cLive ;-)

Replies are listed 'Best First'.
Re: Re: Validate array of numbers in sequence
by Corion (Patriarch) on Sep 08, 2003 at 11:01 UTC

    Both, your and Abigail-IIs solution will fail for duplicate elements (although I don't know whether that's a problem or not) :

    #!/usr/bin/perl use strict; use warnings; my @list = qw( 1 1 2 ); my %hash; @hash{@list}=1; delete $hash{$_} for (1..@list); print "is sequence\n" unless %hash;
    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      Fair point. Just switch things around a little...
      my %hash; @hash{1..@list}=1; delete $hash{$_} for (@list);

      Funny, this was how I did it originally, but I thought the other way round looked prettier :)

      cLive ;-)

Re: Re: Validate array of numbers in sequence
by AcidHawk (Vicar) on Sep 08, 2003 at 11:15 UTC

    Thanks for the input but this does not return the correct results.

    E.g. If I have an array with 1 2 2 2 2 2 2 2 9 your code returns is sequence

    -----
    Of all the things I've lost in my life, its my mind I miss the most.