Splitting a Qshell Variable
August 7, 2002 Timothy Prickett Morgan
Hey, Ted:
I have two values, separated by a space, in one Qshell variable. I want to split them into two variables. Is there a way to do that?
|
— Hank
I know of one way. I’ll show it to you. It will make you appreciate that good old, antiquated RPG.
Let’s say the two values are a first name and last name, stored in a variable called name, and that you want to put them into variables called first and last.
First, let me give you the commands to split one variable into two. Then I’ll explain how it works.
set $(echo $name | ( read fn ln ; echo $fn ; echo $ln )) first=$1 last=$2
The first echo copies the value of the name variable to the standard output file (stdout).
The vertical bar tells Qshell that the output of the echo command is to be the input to the command that follows the vertical bar.
The command that follows the vertical bar is a compound command, made up of three simple commands: one read and two echos. Qshell knows it’s a compound command because of the parentheses that surround it and the semicolons that separate the three simple commands.
The read reads the piped output of the first echo command and divides it into two variables. Everything before the first space gets stored in variable fn. Everything after the first group of one or more spaces gets stored into variable ln. Variables fn and ln are only defined within the compound command.
The last two echo commands send the values of fn and ln to standard output.
Notice that most of the line is enclosed in a command substitution expression. A command substitution expression begins with a dollar sign ($) and open parenthesis and ends with a close parenthesis. (The use of an older syntax, which uses backquotes as delimiters, is discouraged in Unix circles.) The output of a command substitution expression is fed back into the currently executing Qshell command, which in this case is a set command.
The set command, then, has two arguments–the first and last names that were extracted by the read. It stores them into positional parameters 1 and 2.
Not exactly the epitome of intuitiveness, is it?
The last two lines store the two positional parameters into variables called first and last.
Maybe an example will help.
Suppose variable name contains the value Joe Smith.
- The first echo runs, sending the string Joe Smith into the pipeline.
- The read command reads one record from the pipe, containing the value Joe Smith. Read stores Joe into variable fn and Smith into variable ln.
- The second echo command sends Joe to stdout.
- The third echo command sends Smith to stdout.
- The set command is interpreted as follows: set Joe Smith. This action stores Joe in the first positional parameter and Smith in the second positional parameter.
- The two assignment statements copy the first two positional parameters into variables first and last.
— Ted
Sponsored By WORKSRIGHT SOFTWARE |
On June 30, 2002,
On July 1, 2002, We have the solution! CASS certify your mailing names and addresses and presort your outgoing mail and save. Our CASS certification software ensures that your address files have valid ZIP Code and address information. Our presort software ensures that you can properly prepare you mail for delivery to your Post Office. WorksRight Software, Inc. is the number-one source for iSeries and AS/400 CASS, presort, ZIP Code, and area code software and data.
Visit our Web site – www.worksright.com – to learn more about our CASS and presorting software, or contact WorksRight Software, Inc., phone 601-856-8337, |