Windows Vista Forums

Range Operator
  1. #1


    xshell Guest

    Range Operator

    I want PowerShell range operator to work on few other ranges then just
    numbers

    a..z or ('a'..'z') : should produce list of alphabets starting from a
    ending at z.



    Or there should be some way to define custom lists like excel in PowerShell,
    which can be accessed using range operators.



    So I can do something like 'jan'..'dec'. , sun..sat



    I realize that few things can still be done using either .Net
    ([Enum]::GetValues([DayOfWeek]))



    But it will be really nice to write



    a..z

    rather then



    [char[]](([int][char]'a')..([int][char]'z'))





    Let me know if I am the only one who feels this way.




      My System SpecsSystem Spec

  2. #2


    Adam Milazzo Guest

    Re: Range Operator

    "xshell" <xshell@nospam> wrote in message
    news:uoNDene0GHA.4264@TK2MSFTNGP05.phx.gbl...
    >I want PowerShell range operator to work on few other ranges then just
    >numbers
    >
    > a..z or ('a'..'z') : should produce list of alphabets starting from a
    > ending at z.

    I like this idea. The ability to do things like 'a'..'z' or even
    'f01'..'f09' would be nice, and I can think of a number of uses.

    > Or there should be some way to define custom lists like excel in
    > PowerShell, which can be accessed using range operators.
    >
    > So I can do something like 'jan'..'dec'. , sun..sat

    On the other hand, this might be going a bit far. :-) If you want a way to
    define custom lists, I'd say to use a function:

    function months
    {
    return ('jan','feb','mar'...'dec')
    }



      My System SpecsSystem Spec

  3. #3


    Adam Milazzo Guest

    Re: Range Operator

    >> So I can do something like 'jan'..'dec'. , sun..sat
    > On the other hand, this might be going a bit far. :-) If you want a way
    > to define custom lists, I'd say to use a function:
    >
    > function months
    > {
    > return ('jan','feb','mar'...'dec')
    > }


    And if you want to be able to do 'mar'..'jun' or something, you could create
    a months function that takes a numeric range:

    months(2..5) --> ('mar','apr','may','jun')

    If you don't mind converting months to month ordinals. :-)

    Or simply an array:

    $months = ('jan','feb','mar'...'dec')

    and then you can do:

    $months[2..5] --> ('mar','apr','may','jun')

    Or even:

    $months[-1..1] --> ('dec','jan','feb')



      My System SpecsSystem Spec

  4. #4


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    RE: Range Operator

    Actually, this was one of the features that I initially expected to be
    supported too, when I first learned about PowerShell's range operator.

    The syntax 'a'..'z' very much reminds me of good old Turbo Pascal days.
    In case you're not familiar with Pascal (anymore), just have a look at the
    examples on this website:

    http://www.schoenleber.org/pascal/pascal1-08.html

    I agree that this syntax may be very usefull, so if it doesn't break
    something else, I would very much love to see something like this in
    PowerShell as well!

    --
    greetings
    dreeschkind

    "xshell" wrote:

    > I want PowerShell range operator to work on few other ranges then just
    > numbers
    >
    > a..z or ('a'..'z') : should produce list of alphabets starting from a
    > ending at z.
    >
    >
    >
    > Or there should be some way to define custom lists like excel in PowerShell,
    > which can be accessed using range operators.
    >
    >
    >
    > So I can do something like 'jan'..'dec'. , sun..sat
    >
    >
    >
    > I realize that few things can still be done using either .Net
    > ([Enum]::GetValues([DayOfWeek]))
    >
    >
    >
    > But it will be really nice to write
    >
    >
    >
    > a..z
    >
    > rather then
    >
    >
    >
    > [char[]](([int][char]'a')..([int][char]'z'))
    >
    >
    >
    > Let me know if I am the only one who feels this way.
    >
    >
    >
    >


      My System SpecsSystem Spec

  5. #5


    Chris Warwick Guest

    Re: Range Operator

    On Wed, 6 Sep 2006 15:19:02 -0700, dreeschkind
    <dreeschkind@discussions.microsoft.com> wrote:

    >Actually, this was one of the features that I initially expected to be
    >supported too, when I first learned about PowerShell's range operator.
    >
    >The syntax 'a'..'z' very much reminds me of good old Turbo Pascal days.
    >In case you're not familiar with Pascal (anymore), just have a look at the
    >examples on this website:
    >
    >http://www.schoenleber.org/pascal/pascal1-08.html
    >
    >I agree that this syntax may be very usefull, so if it doesn't break
    >something else, I would very much love to see something like this in
    >PowerShell as well!


    I'd vote for this too.

    I was going to say (you beat me to it!) that this reminded me
    instantly of Pascal and the early days of my CS degree back before the
    meteoric rise of C and its decendents. Heady times I'm sure. DCL was
    around about then too - back in the early 80s

    I remember a project to write a Pascal compiler (in C!) - I added
    error checking terminal symbols in a C enum and still recall my
    feeling that the C enum was nowhere near as useful as the Pascal
    enumerated type.

    From http://www.macdonald.egate.net/CompS...cal/henum.html

    The type is declared as an ordinal list in the type section such as
    the following one for the days of the week.

    type
    days = ( SUN, MON, TUES, WED, THURS, FRI, SAT );

    day : days;

    could be assigned a value as follows

    day := TUES

    The use of the enumerated type permits loops to be defined such as the
    following.

    for day := SUN to SAT do


      My System SpecsSystem Spec

  6. #6


    Bruce Payette [MSFT] Guest

    Re: Range Operator

    Please post a bug on the connect site for this. We'd discussed supporting
    this early on but it got dropped from version 1. Voting for it on connect
    will help get it back on the radar. Thanks.

    -bruce

    --
    Bruce Payette [MSFT]
    Windows PowerShell Technical Lead
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.



    "Chris Warwick" <news@remove.this.bit.nuney.com> wrote in message
    news:d0kuf2tf48f5rpgkv0oq2u8nfgi74adf5l@4ax.com...
    > On Wed, 6 Sep 2006 15:19:02 -0700, dreeschkind
    > <dreeschkind@discussions.microsoft.com> wrote:
    >
    >>Actually, this was one of the features that I initially expected to be
    >>supported too, when I first learned about PowerShell's range operator.
    >>
    >>The syntax 'a'..'z' very much reminds me of good old Turbo Pascal days.
    >>In case you're not familiar with Pascal (anymore), just have a look at the
    >>examples on this website:
    >>
    >>http://www.schoenleber.org/pascal/pascal1-08.html
    >>
    >>I agree that this syntax may be very usefull, so if it doesn't break
    >>something else, I would very much love to see something like this in
    >>PowerShell as well!

    >
    > I'd vote for this too.
    >
    > I was going to say (you beat me to it!) that this reminded me
    > instantly of Pascal and the early days of my CS degree back before the
    > meteoric rise of C and its decendents. Heady times I'm sure. DCL was
    > around about then too - back in the early 80s
    >
    > I remember a project to write a Pascal compiler (in C!) - I added
    > error checking terminal symbols in a C enum and still recall my
    > feeling that the C enum was nowhere near as useful as the Pascal
    > enumerated type.
    >
    > From http://www.macdonald.egate.net/CompS...cal/henum.html
    >
    > The type is declared as an ordinal list in the type section such as
    > the following one for the days of the week.
    >
    > type
    > days = ( SUN, MON, TUES, WED, THURS, FRI, SAT );
    >
    > day : days;
    >
    > could be assigned a value as follows
    >
    > day := TUES
    >
    > The use of the enumerated type permits loops to be defined such as the
    > following.
    >
    > for day := SUN to SAT do
    >




      My System SpecsSystem Spec

  7. #7


    =?Utf-8?B?L1wvXG9cL1wvIFtNVlBd?= Guest

    Re: Range Operator

    Tip for getting a Range of Days

    MowPS>1..7 |% {(get-culture).DateTimeFormat.DayNames[($_ - 1)]}
    Sunday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    MowPS>1..7 |%
    {([System.Globalization.CultureInfo]"nl-nl").DateTimeFormat.DayNames[($_ -
    1)]}
    zondag
    maandag
    dinsdag
    woensdag
    donderdag
    vrijdag
    zaterdag

    Tip for getting a Range of Months

    MowPS>1..7 |%
    {([System.Globalization.CultureInfo]"nl-nl").DateTimeFormat.Monthnames[($_ -
    1)]}
    januari
    februari
    maart
    april
    mei
    juni
    juli

    MowPS>1..12 |% {(get-date "2006/$_/1").tostring('MMM')}
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Sep
    Oct
    Nov
    Dec
    MowPS>1..12 |% {(get-date "2006/$_/1").tostring('MMMM')}
    January
    February
    March
    April
    May
    June
    July
    August
    September
    October
    November
    December

    "Bruce Payette [MSFT]" wrote:

    > Please post a bug on the connect site for this. We'd discussed supporting
    > this early on but it got dropped from version 1. Voting for it on connect
    > will help get it back on the radar. Thanks.
    >
    > -bruce
    >
    > --
    > Bruce Payette [MSFT]
    > Windows PowerShell Technical Lead
    > Microsoft Corporation
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    > "Chris Warwick" <news@remove.this.bit.nuney.com> wrote in message
    > news:d0kuf2tf48f5rpgkv0oq2u8nfgi74adf5l@4ax.com...
    > > On Wed, 6 Sep 2006 15:19:02 -0700, dreeschkind
    > > <dreeschkind@discussions.microsoft.com> wrote:
    > >
    > >>Actually, this was one of the features that I initially expected to be
    > >>supported too, when I first learned about PowerShell's range operator.
    > >>
    > >>The syntax 'a'..'z' very much reminds me of good old Turbo Pascal days.
    > >>In case you're not familiar with Pascal (anymore), just have a look at the
    > >>examples on this website:
    > >>
    > >>http://www.schoenleber.org/pascal/pascal1-08.html
    > >>
    > >>I agree that this syntax may be very usefull, so if it doesn't break
    > >>something else, I would very much love to see something like this in
    > >>PowerShell as well!

    > >
    > > I'd vote for this too.
    > >
    > > I was going to say (you beat me to it!) that this reminded me
    > > instantly of Pascal and the early days of my CS degree back before the
    > > meteoric rise of C and its decendents. Heady times I'm sure. DCL was
    > > around about then too - back in the early 80s
    > >
    > > I remember a project to write a Pascal compiler (in C!) - I added
    > > error checking terminal symbols in a C enum and still recall my
    > > feeling that the C enum was nowhere near as useful as the Pascal
    > > enumerated type.
    > >
    > > From http://www.macdonald.egate.net/CompS...cal/henum.html
    > >
    > > The type is declared as an ordinal list in the type section such as
    > > the following one for the days of the week.
    > >
    > > type
    > > days = ( SUN, MON, TUES, WED, THURS, FRI, SAT );
    > >
    > > day : days;
    > >
    > > could be assigned a value as follows
    > >
    > > day := TUES
    > >
    > > The use of the enumerated type permits loops to be defined such as the
    > > following.
    > >
    > > for day := SUN to SAT do
    > >

    >
    >
    >


      My System SpecsSystem Spec

  8. #8


    =?Utf-8?B?L1wvXG9cL1wvIFtNVlBd?= Guest

    Re: Range Operator

    And for the Alphabet Range :

    MowPS>Function get-CharRange
    ([char]$from,[char]$to){[char[]]([int][char]$from..[int][char]$to)}
    MowPS>get-CharRange a g
    a
    b
    c
    d
    e
    f
    g
    MowPS>get-CharRange o k
    o
    n
    m
    l
    k

    Greetings /\/\o\/\/


      My System SpecsSystem Spec

  9. #9


    Lee Holmes [MSFT] Guest

    Re: Range Operator

    Here's a helpful script to give you an arbitrary range operator:

    ## Range.ps1
    ## Support ranges on variable types other than numeric
    ##
    ## range Stop Inquire System.Management.Automation.ActionPreference
    ## range Friday Monday DayOfWeek
    ## range e g char

    param([string] $first, [string] $second, [string] $type)

    $rangeStart = [int] ($first -as $type)
    $rangeEnd = [int] ($second -as $type)

    $rangeStart..$rangeEnd | % { $_ -as $type }

    --
    Lee Holmes [MSFT]
    Windows PowerShell Development
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.

    "/\/\o\/\/ [MVP]" <oMVP@discussions.microsoft.com> wrote in message
    news:C02917C1-2CD0-4472-BFD6-2D9E5DADA94A@microsoft.com...
    > And for the Alphabet Range :
    >
    > MowPS>Function get-CharRange
    > ([char]$from,[char]$to){[char[]]([int][char]$from..[int][char]$to)}
    > MowPS>get-CharRange a g
    > a
    > b
    > c
    > d
    > e
    > f
    > g
    > MowPS>get-CharRange o k
    > o
    > n
    > m
    > l
    > k
    >
    > Greetings /\/\o\/\/
    >




      My System SpecsSystem Spec

Range Operator problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
-ne operator Simon Walsh PowerShell 3 23 Nov 2008
-f operator Tibor Soos PowerShell 3 28 May 2008
64 bit binary or (-bor operator)? l0b0 PowerShell 3 05 Mar 2008
What does the $() operator do? Kevin Buchan PowerShell 3 08 Feb 2008
Suggestion: 1 new operator (well, maybe 6...) dreeschkind PowerShell 10 12 Jul 2007