从ruby中的数组创建哈希

时间:2016-12-10 00:12:44

标签: arrays ruby hash

我有一个特定顺序的数组,并希望创建一个散列,其中数组的奇数条目作为索引,偶数作为值。这段代码完美地完成了,但是从数组中省去了一对值。

'(.*)\\\\s-'  

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

我想你想要:

;with recurse
as (

    select Objects.CMID Id, ObjectClasses.Name Class, ObjectNames.NAME Name
    , cast('CognosObjects' as nvarchar(max)) ObjectPath
    from CMOBJECTS Objects
    inner join CMOBJNAMES ObjectNames 
        on ObjectNames.CMID = Objects.CMID 
        and ObjectNames.IsDefault = 1 --only get 1 result per object (could filter on language=English (LocaleId=24 / select LocaleId from CMLOCALES where Locale = 'en'))
    inner join CMCLASSES ObjectClasses on ObjectClasses.CLASSID = Objects.CLASSID
    where Objects.PCMID = objects.CMID --cleaner than selecting on root since not language sensitive
    --where ObjectClasses.NAME = 'root'

    union all 

    select Objects.CMID Id, ObjectClasses.Name Class, ObjectNames.NAME Name
    , r.ObjectPath + '\' + ObjectNames.NAME ObjectPath  --I use a backslash rather than forward slash as using this to build a windows path 
    from recurse r
    inner join CMOBJECTS Objects 
        on objects.PCMID = r.Id 
        and Objects.PCMID != objects.CMID --prevent ouroboros
    inner join CMOBJNAMES ObjectNames 
        on ObjectNames.CMID = Objects.CMID
        and ObjectNames.IsDefault = 1 --only get 1 result per object (could filter on language=English (LocaleId=24 / select LocaleId from CMLOCALES where Locale = 'en'))
    inner join CMCLASSES ObjectClasses 
        on ObjectClasses.CLASSID = Objects.CLASSID

)
select * 
from recurse 
where Class in ('report','query')
order by ObjectPath

插图:

resolv_hash = namerslv_array.each_slice(2).to_h