ruby 通过*解析为树

描述

  1. 给定一个字符串,“第一 10\r\n第一一 2\r\n第一二 3\r\n**第一二一 12.1\r\n*第一二一 b\r\n第二 10”
  2. 将字符串解析为
[
    {
        "type": 0,
        "id": 0,
        "title": "第一",
        "description": "",
        "write_method": {
            "1": 0
        },
        "max_score": 10,
        "items": [
            {
                "type": 1,
                "id": 0,
                "title": "第一一",
                "description": "",
                "write_method": {
                    "1": 0,
                    "6": 4096
                },
                "max_score": 2
            },
            {
                "type": 0,
                "id": 0,
                "title": "第一二",
                "description": "",
                "write_method": {
                    "1": 0
                },
                "max_score": 3,
                "items": [
                    {
                        "type": 1,
                        "id": 0,
                        "title": "第一二一",
                        "description": "",
                        "write_method": {
                            "1": 0,
                            "6": 4096
                        },
                        "max_score": 12.1
                    }
                ]
            },
            {
                "type": 1,
                "id": 0,
                "title": "第一二一",
                "description": "",
                "write_method": {
                    "1": 0,
                    "6": 4096
                },
                "max_score": 0
            }
        ]
    },
    {
        "type": 1,
        "id": 0,
        "title": "第二",
        "description": "",
        "write_method": {
            "1": 0,
            "6": 4096
        },
        "max_score": 10
    }
]

方法

方法一
	def on_table_setting(aa)
		table_setting = []#存储处理好的数据
		group_status = []#用于深度层级的储存
		new_item = Proc.new do |line,pitem=nil| #最终处理 line需要处理的数据,pitem最终存在那个父级数据
			token=line.split(" ")#通过空格将标题和分数,处理为一个数组['第一',10]
			score=token.last.to_i #记录分数
			unless score.to_s == token.last #如果成绩转换为字符串不等于设置的数组中成绩代表为小数,则成绩转换为小数
				score=token.last.to_f 
				unless score.to_s == token.last
					score = 0
				end
			end
			title = token.first #记录标题
			item = {
        		:type => 1,
        		:id => 0,
        		:title => title,
       		 	:description => "",
        		:write_method => {1 => 0, 6 => 4096},
        		:max_score => score
      		}
			if pitem #如果不为空则代表上头肯定有父亲
				pitem[:items]=[] unless pitem[:items]
				pitem[:type]=0 if pitem[:type] ==1 #如果为1改为0 0代表肯定有儿子
				pitem[:write_method].delete(6) if pitem[:write_method].has_key?(6)
				pitem[:items].push item #将儿子放进父亲里边
			else #独立的一条
				table_setting.push item
			end
		end

		new_gitem = Proc.new do |line,lv|
			loop do
				break if group_status.size <= lv #深度来定位该条数据的父亲
				group_status.pop #如果深度层级储存>他的深度就去掉最后一个,直到等于深度
			end
			Sys::Exception.trigger("input error !") if group_status.size < lv
      		new_item.call(line, group_status.last)
		end
		#计算深度
		get_deep = Proc.new do |line|
			lv=0
			loop do #计算出深度
				break unless line[0] == '*' 
				line[0,1] =""
				lv+=1
			end
			lv
		end

		 aa.split(/[\r\n]+/).each do |line|
		 	line = line.strip
      		lv = get_deep.call(line) #计算出深度
      		if lv == 0 #如果无深度单独一条或者父亲
       			group_status.clear
       			new_item.call(line)
     		 else #有深度代表他是儿子
        		new_gitem.call(line, lv)
     		 end
		 end
	end
	str= "第一 10\r\n*第一一 2\r\n*第一二 3\r\n**第一二一 12.1\r\n*第一二一 b\r\n第二 10"
	p=on_table_setting(str)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值