gnuplot-将字符串变量转换为小写 [英] gnuplot - convert a string variable to lowercase

查看:53
本文介绍了gnuplot-将字符串变量转换为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在gnuplot中将字符串转换为小写?
这是gnuplot字符串处理问题.

How do you convert a string to lowercase in gnuplot?
This is a gnuplot string handling question.

示例:-我希望在gnuplot脚本中检查用户键入的参数....

Example:- I wish to check a user typed parameter in a gnuplot script....

if (tolower(ARG2) == "ohms") {.....

因此请接受"ohms","Ohms"或"OHMS".

so by accepting "ohms", "Ohms", or "OHMS".

首选项是不需要使用外部系统"命令,从而使脚本更具可移植性.我目前最好的解决方案是

The preference is to not need to use an external "system" command so that the script is more portable. My current best solution is

  arg2 = system("awk 'BEGIN { print toupper(\"".ARG2."\") }'")

然后测试新的字符串变量"arg2",但是awk(或其他程序)在非unix系统上通常不可用,这使得gnuplot脚本的可移植性较差.

and then test the new string variable "arg2", but awk (or other program) may not be generally available on a non unix system, making the gnuplot script less portable.

我看不到任何改进的gprintf%格式说明符可以修改字符串表示形式-看来gprintf仅用于转换值.

I cannot see any enhanced gprintf % format specifiers that modifies string presentation - it seems gprintf is only for converting values.

推荐答案

全功能宏解决方案(感谢theozh)让我再次考虑如何将此作为函数实现.使用查找表通过等于序数来转换字符的想法是一个好主意.首先将单个字符的大小写转换封装到一个函数中,然后再进行递归,这使得处理我最初寻找的完整字符串成为可能.我希望这现在是所有人的一个整洁的解决方案.分享并享受.

The full function Macro solution (Thanks theozh) has me thinking again of how to implement this as a function. The idea of using a lookup table to convert characters by equating an ordinal number was a great idea. Encapsulating single character case conversion into a function was the start, and then along with recursion, it has made it possible to handle full strings as I had first looked for. I hope this is now a tidy solution for all. Share and enjoy.

# GNUPLOT string case conversion
# string_case.gnu   M J Pot, 14/1/2019

# Index lookup table strings
UCases="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LCases="abcdefghijklmnopqrstuvwxyz"

# Convert a single character
# Char to convert is added to string so it is always found to default other chars
toupperchr(c)=substr( UCases.c, strstrt(LCases.c, c), strstrt(LCases.c, c) )
tolowerchr(c)=substr( LCases.c, strstrt(UCases.c, c), strstrt(UCases.c, c) )

# Convert whole strings
# Conversion first char (or return null), and recurse for the remaining
toupper(s) = s eq ""  ?  ""  :  toupperchr(s[1:1]).toupper(s[2:*])
tolower(s) = s eq ""  ?  ""  :  tolowerchr(s[1:1]).tolower(s[2:*])

添加:改进的解决方案

这是对递归案例转换作为自包含函数的重做.稍加努力即可解决第一个解决方案过多的堆栈使用情况.当我遇到问题时,我只是在考虑一个单词的字符串.注意:-单字符转换已变得更加可靠.

Addition: Improved solution

This is a re-work of the recursive case conversion as self contained functions. A little more effort has resolved the excessive stack usage of the first solution. I had only been considering strings of single words when I had the problem. Note:- the single character conversion has been made more robust.

# GNUPLOT string case conversion
# string_case.gnu   M J Pot, 29/1/2019
# toupper(), tolower() functions

# Index lookup table strings
UCases="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LCases="abcdefghijklmnopqrstuvwxyz"

# Convert a single character
# Char to convert is added to string so it is always found to default other chars
# Null strings are returned null
toupperchr(c)= c eq ""  ?  ""  :  substr( UCases.c, strstrt(LCases.c, c), strstrt(LCases.c, c) )
tolowerchr(c)= c eq ""  ?  ""  :  substr( LCases.c, strstrt(UCases.c, c), strstrt(UCases.c, c) )

# Divide & conquer
# A simple linear recursive call uses too much stack for longer strings.
# This undertakes a binary tree division to make the stack growth order log_2(length)
toupper(s) = strlen(s) <= 1 ? toupperchr(s) : toupper( substr(s,1,strlen(s)/2) ) . toupper( substr(s,(strlen(s)/2)+1,strlen(s)) ) 
tolower(s) = strlen(s) <= 1 ? tolowerchr(s) : tolower( substr(s,1,strlen(s)/2) ) . tolower( substr(s,(strlen(s)/2)+1,strlen(s)) ) 

这篇关于gnuplot-将字符串变量转换为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆