2005/12/29 | [翻译整理]用lingo计算一月有多少天,findPosNear应用范例。
类别(开发文献) | 评论(0) | 阅读(224) | 发表于 20:54

文章作者:爱伊

前不久在国外网站看到一篇文章,介绍用lingo计算一个月有多少天,今天把里面的主要内容翻译整理出来,要指出的是这篇文章最重要的价值不在于里面介绍的计算函数而是findPosNear的应用。(原文章连接:http://www.mediamacros.com/item/item=1006687634)

下面的脚本代码需要放置在电影脚本中,然后就可以在其他任何地方使用了。你可以指定月和年或者只是指定月份,这时会默认为系统当前年份。如:

put daysInMonth(2,2004)

-- 29

put daysInMonth(2)

-- 28

 

 值得注意的是,这里的月份,你可以使用英文的,如得到2004年二月份有多少天:

你可以用

put daysInMonth("February",2004)

-- 29

 

或者类似这种

put daysInMonth("feb",2004)

-- 29

 

也就是说你不用输入完整的英文单词,而只是前面一部分,这是使用findPosNear一个典型的应用,它自动去搜索最相似的值,要注意的是,为了保证findPosNear返回正确值,需要在执行前,对目标列表进行排序,也就是说 months.sort() 这一句是必要的。

 

具体代码如下:

 

on daysInMonth(theMonth, theYear)  

  months = [#January: 1, #February: 2, #March: 3, #April: 4, #May: 5, #June: 6, #July: 7, #August: 8, #September: 9, #October: 10, #November: 11, #December: 12]

  months.sort()

  if ilk(theMonth) = #string then theMonth = months[months.findPosNear(symbol(theMonth))]

  if theYear = 0 then theYear = the systemDate.year

  return date(theYear, theMonth + 1, 1) - date(theYear, theMonth, 1)

end

 

改一改:

 

  由上面的代码我们稍稍改动一下得到另一个函数"daysInMonth2",让它可以根据指定中文月份来返回对应天数来,同样你可以输入完整的月份名,或者只输入一部分,也可以返回正确的对应天数,如:

put daysInMonth2("二月份",2004)

put daysInMonth2("二月",2004)

put daysInMonth2("",2004)

-- 29

都可以返回2004年二月份的正确天数29

 

具体代码如下:

 

on daysInMonth2(theMonth, theYear)  

  months = [#一月份: 1, #二月份: 2, #三月份: 3, #四月份: 4, #五月份: 5, #六月份: 6, #七月份: 7, #八月份: 8, #九月份: 9, #十月份: 10, #十一月份: 11, #十二月份: 12]

  months.sort()

  if ilk(theMonth) = #string then theMonth = months[months.findPosNear(symbol(theMonth))]

  if theYear = 0 then theYear = the systemDate.year

  return date(theYear, theMonth + 1, 1) - date(theYear, theMonth, 1)

end

 

 

0

评论Comments