Re: string plus string

From: Saji Hameed <saji.nh_at_nyahnyahspammersnyahnyah>
Date: Sun Mar 20 2011 - 00:36:22 MDT

Hi Mark,

Add the two strings and then use str_squeeze or equivalent to remove the
unwanted spaces from the flanks of your new string.

http://ncl.ucar.edu/Document/Functions/Built-in/str_squeeze.shtml

saji

---
On Sun, Mar 20, 2011 at 1:19 PM, Mark Chan <cym263@yahoo.com> wrote:
> Dear Dennis,
>
> Thank you very much! Actually my data is exactly like this (also see the
> attached files)
>
> value   name1   name2
> 20.0    aaa1
> 80.7    bbb1      bbb2
> 45.5    ccc1      ccc2
> 60.3    ddd1
> 100.6  eee1
>
> I modify your script as below:
>
> begin
> x = asciiread("mydata.dat",-1,"string")
> data   = stringtofloat(str_get_field(x(1:),1," "))
> nx = dimsizes(x)
> ss = new(nx, "string", "No_FillValue")
> do n=0,nx-1
>  nf = str_fields_count(x(n), " ")
>   if (nf.eq.2) then
>      aa1 = str_split(x(n)," ")
>      ss(n) = aa1(1)
>  else
>      aa2 = str_split(x(n)," ")
>      ss(n) = aa2(1)+" "+aa2(2)
>   end if
> end do
> print(ss)
> print(data)
> end
>
>
> My confusion is as:
>
> (1) I want to skip the first row but "ss"always keep "name1" as its values
> (2) dimsizes(x)=dimsizes(data)+1, so "x" didn't skip the first row. why is
> this?
> (3) It seems the first value (20.0) was given to "name1" instead of "aaa1",
> which actually is wrong, since I would like the "ss(0)=aaa1" to correspond
> to
> "data(0)=20"
>
> I may confuse you here, since I was totally confused.
> Thanks again for further suggestion.
>
> Mark
>
>
>
>
> ----- Original Message ----
> From: Dennis Shea <shea@ucar.edu>
> To: Mark Chan <cym263@yahoo.com>
> Cc: ncl-talk@ucar.edu
> Sent: Sat, March 19, 2011 10:29:03 AM
> Subject: Re: string plus string
>
> x = asciiread("....", -1, "string")
>
> nx = dimsizes(x)
> do n=0,nx-1
>   nf = str_fields_count(x(n), " ")
>   if (nf.eq.1) then
>       ss = str_split(x(n)," ")
>   else
>       aa = str_split(x(n)," ")
>       ss = aa(0)+" "+aa(1)
>   end if
>    print(""+ss)
> end do
>
> or
>
> ss = new(nx, "string", "No_FillValue")
> do n=0,nx-1
>   nf = str_fields_count(x(n), " ")
>   if (nf.eq.1) then
>       ss(n) = str_split(x(n)," ")
>   else
>       aa = str_split(x(n)," ")
>       ss(n) = aa(0)+" "+aa(1)
>   end if
> end do
>
> print(""+ss)
>
>
> On 3/18/11 4:59 PM, Mark Chan wrote:
> > NCL users,
> >
> >
> > If "x" is a string array like this:
> >
> > aaa1    aaa2
> > bbb1
> > ccc1    ccc2
> > ddd1
> > eee1
> >
> > I want to make a NCL script as:
> >
> >        s1 = str_get_field(x(1:),1," ")
> >        s2 = str_get_field(x(1:),2," ")
> >        if (s2.ne."null") then  ;------------------how to judge "s2" here
> >        ss =s1+" "+s2
> >        else
> >        ss=s1
> >        end if
> >
> > The aim is plus "s1" and "s2" with a space between only if  "s2" has
> value.
> > Otherwise the string "ss" would have extra space (e.g.  ss=s1+" "), which
> >damage
> > the variable name.
> > Can any one help!
> >
> > Thanks very much.
> >
> > Mark
> >
> >
> >
> >
> > _______________________________________________
> > 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
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Sun Mar 20 00:36:29 2011

This archive was generated by hypermail 2.1.8 : Wed Mar 23 2011 - 16:15:59 MDT