Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to format a string? #138

Closed
Geek-007 opened this issue Jun 27, 2020 · 6 comments
Closed

How to format a string? #138

Geek-007 opened this issue Jun 27, 2020 · 6 comments

Comments

@Geek-007
Copy link

Hi!
How to format a string?Just like a shell script:
e.g. printf "%-10s" "abc"

Can NCL format the output string?

tks.

@rbrownrigg
Copy link
Contributor

rbrownrigg commented Jun 27, 2020 via email

@Geek-007
Copy link
Author

嗨,看看sprintf / sprinti函数:http ://ncl.ucar.edu/Document/Functions/Built-in/sprintf.shtml http://ncl.ucar.edu/Document/Functions/Built-in /sprinti.shtml 瑞克

On Sat, Jun 27, 2020 at 2:27 AM QingQing Tong @.***> wrote: Hi! How to format a string?Just like a shell script: e.g. printf "%-10s" "abc" Can NCL format the output string? tks. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#138>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADLWOXUFTA7WTJVE2GZMQF3RYWUNLANCNFSM4OJ5U3GA .

These two functions are applicable to float and integer respectively, strings cannot be used.

@Dave-Allured
Copy link
Contributor

NCL does not provide direct support for type "s" format strings. NCL provides only printf support for numeric format types.

Here is a workaround that uses a sub shell for direct access to the printf command. This is okay for small data scenarios, but not for mass data processing because it is quite inefficient. This is more complicated than you might think, because of several quirks in NCL string handling.

   dq = str_get_dq()                   
   s1 = "abc"                          
   s2 = "printf '%-10s' '" + s1 + "' ; echo ''"
   s3 = str_sub_str (s2, "'", dq)
   s4 = systemfunc (s3)
   print ("[" + (/ s1, s2, s3, s4 /) + "]")

Result:

(0)     [abc]
(1)     [printf '%-10s' 'abc' ; echo '']
(2)     [printf "%-10s" "abc" ; echo ""]
(3)     [abc       ]

@Dave-Allured
Copy link
Contributor

NCL procedures print_table and write_table include direct support for type "s" format strings, as well as compound format strings. If you are just trying to make a formatted text file, this may be what you want. To the best of my knowledge, these are the only functions within NCL that have direct support for type "s" formatting.

@Dave-Allured
Copy link
Contributor

Otherwise, you can do a lot with NCL's string function library and small amounts of custom NCL code. "%-10s" can be implemented quite easily. This would be the right way to go for mass processing and packaged applications.

https://www.ncl.ucar.edu/Document/Functions/string.shtml

@Geek-007
Copy link
Author

Geek-007 commented Jul 1, 2020

Thanks for your answer, I finally solved my problem with the shell.

ncl 0> str=tostring(systemfunc("printf '%-15s\n' abc"))
ncl 1> print(str)

Variable: str
Type: string
Total Size: 8 bytes
1 values
Number of Dimensions: 1
Dimensions and sizes: [1]
Coordinates:
(0) abc
ncl 2> str=tostring(systemfunc("printf '%15s\n' abc"))
ncl 3> print(str)

Variable: str
Type: string
Total Size: 8 bytes
1 values
Number of Dimensions: 1
Dimensions and sizes: [1]
Coordinates:
(0) abc

@Geek-007 Geek-007 closed this as completed Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants