Re: 'string parsing'

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Thu Oct 20 2011 - 08:18:47 MDT

To add to Dennis' statement about "else if".

He's correct that there is no "else if" statement, but you can actually have an "else" and an "if" on the same line.

You have to edd an "end if" for every "if", though, so it gets messy quickly.

Here's a valid NCL script:

str = "MAR"

if(str.eq."JAN") then
  print("January")
else if(str.eq."FEB") then
  print("February")
else if(str.eq."MAR") then
  print("March")
else if(str.eq."APR") then
  print("April")
else
  print("Enough of this!")
end if
end if
end if
end if

On Oct 19, 2011, at 9:04 PM, Dennis Shea wrote:

> Please read the documentation.
> As indicated in NCL documentation, there is no 'else if' or 'elseif'
>
> Also, as indicated by the comment, the str_get_field return strings
> You can *not* perform an addition with a string value.
>
> ncl 0> x = "15"
> ncl 1> x = x+1
> ncl 2> print(x) ===> 151 ... the + acts to concatenate a string
> ncl 3> y = "15"
> ncl 4> y = toint(y) +1 ===> 16 on right hand side; string "16" on left
> ncl 5> print(y)
>
>
> For less confusion step over each element.
> ==============================================
>
> [SNIP]
> mn = str_get_field(ts, 5, delim)
>
> nts = dimsizes(ts)
> do nt=0,nts-1
> if (toint(mn(nt)) .le. 30) then
> mn(nt) = "00"
> else
> mn(nt) = "00"
> hhi = toint(hh(nt))+1 ; convert to integer
> if (hhi .eq. 24) then
> hh(nt) = "00"
> ddi = toint(dd(nt))+1
> if (ddi.eq. 32) then
> dd(nt) = "1"
> mmi = toint(mm(nt))+1
> mm(nt) = tostring(mmi)
> end if
> end if
> end if
>
> hhi = toint(hh(nt))
> ddi = toint(dd(nt))
>
> if (hhi.le. 8) then
> hhi = hhi-8
> else
> if (hhi .le. 8 .and. ddi .ne. 1) then
> hhi = hhi+24-8
> ddi = ddi-1
> else
> if (hhi .le. 8 .and. ddi .eq. 1) then
> hhi = hhi+24-8
> dd(nt) = "31"
> mmi = mmi-1
> mm(nt) = tostring(mmi)
> end if
> end if
> end if
> end do
>
>
> On 10/19/11 8:21 PM, cnl88 wrote:
>> ;************************************************
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>> load "/usr/local/ncl/lib/ncarg/nclscripts/wrf/WRF_contributed.ncl"
>> load "/usr/local/ncl/lib/ncarg/nclscripts/csm/contributed.ncl"
>> ;************************************************
>> begin
>> ts = (/ "3-31-11 23:00" \
>> , "3-31-11 23:05" \
>> , "3-31-11 23:10" \
>> , "3-31-11 23:15" \
>> , "3-31-11 23:20" \
>> , "3-31-11 23:25" \
>> , "3-31-11 23:30" \
>> , "3-31-11 23:35" \
>> &nb sp; , "3-31-11 23:40" \
>> , "3-31-11 23:45" \
>> , "3-31-11 23:50" /)
>> delim = "- :"
>> tsfc = str_fields_count(ts, delim)
>> print(" number of fields: tsfc="+tsfc(0))
>> mm = str_get_field(ts, 1, delim) ; array of strings
>> dd = str_get_field(ts, 2, delim)
>> yy = str_get_field(ts, 3, delim)
>> hh = str_get_field(ts, 4, delim)
>> mn = str_get_field(ts, 5, delim)
>>
>> if(mn .le. 30) then
>> mn =00
>> else
>> mn=00
>> hh=hh+1
>> else if(hh .eq. 24) then
>> hh=00
>> dd=dd+1
>> else if(dd .eq. 32) then
>> dd =1
>> mm=mm+1
>> end if
>> if (hh .le. 8)then
>> hh=hh-8
>> else if(hh .le. 8 .and. dd .not. 1)then
>> hh=hh+24-8
>> dd=dd-1
>> else if(hh .le. 8 .and. dd .eq. 1)then
>> hh=hh+24-8
>> dd=31
>> mm=mm-1
>> end if
>> print(mm+" "+dd+" "+yy+" "+hh+" "+mn)
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Oct 20 08:18:55 2011

This archive was generated by hypermail 2.1.8 : Mon Oct 24 2011 - 09:29:35 MDT