Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1755958
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: 系统运维

2013-07-28 15:36:14

Simple templates for parsing into words
Parsing  into  words  removes  leading  and  trailing  blanks  from  each  word  before  it  is 
assigned  to  a  variable.  The  exception  to  this  is  the  word  or  group  of  words  assigned 
to  the  last  variable.  The  last  variable  in  a  template  receives  leftover  data,  preserving 
extra  leading  and  trailing  blanks.  Here  is  an  example: 

点击(此处)折叠或打开

  1. /* Preserving extra blanks */
  2. solar5=’Mercury Venus Earth Mars Jupiter ’
  3. parse var solar5 var1 var2 var3 var4
  4. /* var1 =’Mercury’ */
  5. /* var2 =’Venus’ */
  6. /* var3 =’Earth’ */
  7. /* var4 =’ Mars Jupiter ’ */
解释一下: 
默认的template的 delimitor是空格,被blank-delimitorparse的 substring 在赋值给var是,除了除了最后一个是直接赋值给变量,其它substring的leading 以及trailing blanks 都会被删除掉。

The  period  as  a  placeholder 
A  period  in  a  template  is  a  placeholder.  It  is  used  instead  of  a  variable  name,  but  it 
receives  no  data.  It  is  useful: 
v      As  a  “dummy  variable”  in  a  list  of  variables 
v      Or  to  collect  unwanted  information  at  the  end  of  a  string.

点击(此处)折叠或打开

  1. /* Period as a placeholder */
  2. stars=’Arcturus Betelgeuse Sirius Rigil’
  3. parse var stars . . brightest . /* brightest=’Sirius’ */
  4.    
  5. /* Alternative to period as placeholder */
  6. stars=’Arcturus Betelgeuse Sirius Rigil’
  7. parse var stars drop junk brightest rest /* brightest=’Sirius’ */
A  placeholder  saves  the  overhead  of  unneeded  variables. 
简单template以及包含string template 的parsing:

Here  are  two  templates:  a  simple  template  and  a  template  containing  a  literal  string 
pattern: 
var1  var2                    /*  simple  template                                                        */ 
var1  ’,  ’  var2          /*  template  with  literal  string  pattern              */ 
The  literal  string  pattern  is:  ’,  ’.  This  template: 
解析过程:
v      Puts  characters  from  the  start  of  the  source  string  up  to  (but  not  including)  the 
first  character  of  the  match  (the  comma)  into  var1 
v      Puts  characters  starting  with  the  character  after  the  last  character  of  the  match 
(the  character  after  the  blank  that  follows  the  comma)  and  ending  with  the  end  of 
the  string  into  var2.

First,  the  language  processor  scans  the  source  string  for  ',  '.  It  splits  the  source 
string  at  that  point.  The  variable  ln  receives  data  starting  with  the  first  character  of 
the  source  string  and  ending  with  the  last  character  before  the  match.  The  variable 
fn  receives  data  starting  with  the  first  character  after  the  match  and  ending  with  the 
end  of  string. 
A  template  with  a  string  pattern  omits  data  in  the  source  string  that  matches  the 
pattern.

A  template  with  a  string  pattern  can  omit  some  of  the  data  in  a  source  string  when 
assigning  data  into  variables.  The  next  two  examples  contrast  simple  templates  with 
templates  containing  literal  string  patterns. 

点击(此处)折叠或打开

  1. /* Simple template */
  2. name=’Smith, John’
  3. parse var name ln fn /* Assigns: ln=’Smith,*/
  4.                                                                                   /* fn=’John’ */


Notice  that  the  comma  remains  (the  variable  ln  contains  ’Smith,’).  In  the  next 
example  the  template  is  ln  ’,  ’  fn.  This  removes  the  comma. 

点击(此处)折叠或打开

  1. /* Template with literal string pattern */
  2. name=’Smith, John’
  3. parse var name ln ’, ’ fn /* Assigns: ln=’Smith’ */
  4.      /*                    fn=’John’      */

无法匹配的情况:                                                                        
If  the  source  string  does  not  contain  a  match  for  a  string  pattern,  then  any  variables 
preceding  the  unmatched  string  pattern  get  all  the  data  in  question.  Any  variables 
after  that  pattern  receive  the  null  string. 
A  null  string  is  never  found.  It  always  matches  the  end  of  the  source  string.
Parsing multiple strings
Only  ARG  and  PARSE  ARG  can  have  more  than  one  source  string.  To  parse 
multiple  strings,  you  can  specify  multiple  comma-separated  templates.  Here  is  an 
example: 
parse  arg  template1,  template2,  template3 
This  instruction  consists  of  the  keywords  PARSE  ARG  and  three  comma-separated 
templates.  (For  an  ARG  instruction,  the  source  strings  to  parse  come  from 
arguments  you  specify  when  you  call  a  program  or  CALL  a  subroutine  or  function.) 
Each  comma  is  an  instruction  to  the  parser  to  move  on  to  the  next  string. 
Example: 

点击(此处)折叠或打开

  1. /* Parsing multiple strings in a subroutine */
  2. num=’3’
  3. musketeers="Porthos Athos Aramis D’Artagnon"
  4. CALL Sub num,musketeers /* Passes num and musketeers to sub */
  5. SAY total; say fourth /* Displays: "4" and " D’Artagnon" */
  6. EXIT
  7.    
  8. Sub:
  9. parse arg subtotal, . . . fourth
  10. total=subtotal+1
  11. RETURN


Note  that  when  a  REXX  program  is  started  as  a  command,  only  one  argument 
string  is  recognized.  You  can  pass  multiple  argument  strings  for  parsing: 
v      When  one  REXX  program  calls  another  REXX  program  with  the  CALL  instruction 
or  a  function  call. 
v      When  programs  written  in  other  languages  start  a  REXX  program.
If  there  are  more  templates  than  source  strings,  each  variable  in  a  leftover  template 
receives  a  null  string.  If  there  are  more  source  strings  than  templates,  the  language 
processor  ignores  leftover  source  strings.  If  a  template  is  empty  (two  commas  in  a 
row)  or  contains  no  variable  names,  parsing  proceeds  to  the  next  template  and 
source string.

REF:TSO/E  REXX  Reference

阅读(1840) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~