Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Test if list is range

by Kenosis (Priest)
on Sep 29, 2012 at 15:05 UTC ( #996391=note: print w/ replies, xml ) Need Help??


in reply to Test if list is range

Perhaps Perl's (v5.10+) smart-match operator can help:

use strict; use warnings; my @array = qw/1 2 3/; print 'The variable is ' . ( is123(@array) ? '' : 'not ' ) . '[1 .. 3] +.'; sub is123 { @_ ~~ [ 1 .. 3 ] }

Output:

The variable is [1 .. 3].


Comment on Re: Test if list is range
Select or Download Code
Re^2: Test if list is range
by DeepThought (Initiate) on Oct 01, 2012 at 10:09 UTC

    Hm, I guess I could use the first and last element of a array and create a range with that which I can compare against the array like this to see if it is identical.

    But I think I will just ignore the problem and expect the user to tell me what he wants. The range operator would have been very nice meta programming syntax sugar though.

    I wanted to use it for data validations where for a test a user could pass a list or range of valid entries. But of course for a range I don't want to actually test every element just a few within the range.

    Thanks for all your answers

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996391]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-07-12 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls