Re: extract fields from string using multiple-space delimiter?

From: Jared Lee <jaredlee_at_nyahnyahspammersnyahnyah>
Date: Fri Jan 03 2014 - 14:24:55 MST

Hi Dave,

[Apologies for the repeat message, but I accidentally replied only to
you initially and not the full list.]

Thanks so much! Employing your approach solves my problem. Using the
example I provided, now I have a three-element array of strings (/"Var 1
[units]", "Var 2 [units]", "Var 3 [units]"/), instead of a nine-element
array (/"Var", "1", "[units]", "Var", "2", "[units]", "Var", "3",
"[units]"/). A three-element array of column titles is indeed what I
was after, and using str_fields_count and str_get_field would always
yield a nine-element array. You're welcome for the fun puzzle.

Jared

On 01/03/2014 01:55 PM, Dave Allured - NOAA Affiliate wrote:
> Jared,
>
> The str_split function has the property of ignoring consecutive
> delimiters. This can be exploited. Also note the use of a unique
> substitution character e.g. "$" which must be chosen carefully to
> never occur in your original title strings.
>
> str1 = "Var 1 [units] Var 2 [units] Var 3 [units]"
> str2 = str_sub_str (str1, " ", "$")
> str3 = str_split (str2, "$")
> str4 = str_strip (str3)
>
> ;; str3 and str4 are string arrays, not single strings.
> print ("str1 = " + str1)
> print ("str2 = " + str2)
> print ("str3 = " + str_concat ("'" + str3 + "' "))
> print ("str4 = " + str_concat ("'" + str4 + "' "))
>
> (0)str1 = Var 1 [units] Var 2 [units] Var 3 [units]
> (0)str2 = Var 1 [units]$$ Var 2 [units]$$$$$Var 3 [units]
> (0)str3 = 'Var 1 [units]' ' Var 2 [units]' 'Var 3 [units]'
> (0)str4 = 'Var 1 [units]' 'Var 2 [units]' 'Var 3 [units]'
>
> Thank you for a fun puzzle on a boring Friday. :-)
>
> --Dave
>
>
> On Fri, Jan 3, 2014 at 1:23 PM, Jared Lee <jaredlee@ucar.edu
> <mailto:jaredlee@ucar.edu>> wrote:
>
> Hi, I'm trying to parse out the fields from the column title line of a
> table, which has the following format:
>
> "Var 1 [units] Var 2 [units] Var 3 [units]"
>
> Each column title is separated by at least two spaces, and never only
> one space. However, to the best of my understanding, the functions
> str_fields_count and str_get_field cannot use multiple consecutive
> spaces (or multiple consecutive anything) as a delimiter, without also
> couting single spaces as a delimiter. Is there any way in NCL to
> parse
> out fields from a string using double spaces but *NOT* single
> spaces as
> a delimiter?
>
> Jared
>
> --
> ================
> Dr. Jared A. Lee
> Postdoctoral Research Scientist
> National Center for Atmospheric Research
>
> Phone: 303.497.8485
> Email: jaredlee@ucar.edu <mailto:jaredlee@ucar.edu>
> Web: https://staff.ucar.edu/users/jaredlee
> ================
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>

-- 
================
Dr. Jared A. Lee
Postdoctoral Research Scientist
National Center for Atmospheric Research
Phone: 303.497.8485
Email: jaredlee@ucar.edu
Web: https://staff.ucar.edu/users/jaredlee
================

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Jan 03 14:25:06 2014

This archive was generated by hypermail 2.1.8 : Mon Jan 06 2014 - 13:09:53 MST