zsh-users
 help / color / mirror / code / Atom feed
* convert number
@ 2010-05-19 17:11 tartifola
  2010-05-19 18:39 ` Christian Walther
  0 siblings, 1 reply; 4+ messages in thread
From: tartifola @ 2010-05-19 17:11 UTC (permalink / raw)
  To: zsh-users


Hi,
I'm lost in a shell issue with no clue where to look for a solution.
I have a very long column of three digit numbers and I need to transform
each of them into a one digit number according to the following
"conversion table"

151	2
152	2
153	2
158	4
159	3
160	2
171	4
172	1
173	4

Any suggestion how to solve the problem with a shell script?
Thanks in advance


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: convert number
  2010-05-19 17:11 convert number tartifola
@ 2010-05-19 18:39 ` Christian Walther
  2010-05-20 14:51   ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Christian Walther @ 2010-05-19 18:39 UTC (permalink / raw)
  To: tartifola; +Cc: zsh-users

Hi,

On 19 May 2010 19:11,  <tartifola@gmail.com> wrote:
>
> Hi,
> I'm lost in a shell issue with no clue where to look for a solution.
> I have a very long column of three digit numbers and I need to transform
> each of them into a one digit number according to the following
> "conversion table"
>
> 151     2
> 152     2
> 153     2
> 158     4
> 159     3
> 160     2
> 171     4
> 172     1
> 173     4

if there is no mathematical formula for the "conversion table"
available, I would put this table in a hash, e.g. something like:

typeset -A convtable
convtable[151]=2
convtable[152]=2

etc...

If there is a value that appears very often, such as 2 in your
example, and it's not possible to parse an invalid number (such as
154-157 which doesn't appear to exist in your example), you could drop
every number that needs to be transformed to 2 and use something like:

typeset -A convtable
convtable[158]=4
convtable[159]=3
convtable[171]=4
convtable[172]=1
convtable[173]=4

The transformation would be done as

${convtable[$number]=2}

e.g.

number=158
echo ${convtable[$number]=2}
4

number=151
echo ${convtable[$number]=2}
2

HTH
Christian Walther


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: convert number
  2010-05-19 18:39 ` Christian Walther
@ 2010-05-20 14:51   ` Bart Schaefer
  2010-05-20 14:56     ` tartifola
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2010-05-20 14:51 UTC (permalink / raw)
  To: zsh-users

On Wed, May 19, 2010 at 11:39 AM, Christian Walther <cptsalek@gmail.com> wrote:
>
> if there is no mathematical formula for the "conversion table"
> available, I would put this table in a hash, e.g. something like:
>
> typeset -A convtable
> convtable[151]=2
> convtable[152]=2
>
> etc...

You can shortcut this:

typeset -A convtable
convtable=(151 2 152 2 153 2 158 4 159 3 160 2 171 4 172 1 173 4)

Note that you must have an even number of elements in the assignment
list, because they are taken as key value key value etc.  The
assignment will fail otherwise.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: convert number
  2010-05-20 14:51   ` Bart Schaefer
@ 2010-05-20 14:56     ` tartifola
  0 siblings, 0 replies; 4+ messages in thread
From: tartifola @ 2010-05-20 14:56 UTC (permalink / raw)
  To: zsh-users

On Thu, 20 May 2010 07:51:09 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Wed, May 19, 2010 at 11:39 AM, Christian Walther <cptsalek@gmail.com> wrote:
> >
> > if there is no mathematical formula for the "conversion table"
> > available, I would put this table in a hash, e.g. something like:
> >
> > typeset -A convtable
> > convtable[151]=2
> > convtable[152]=2
> >
> > etc...
> 
> You can shortcut this:
> 
> typeset -A convtable
> convtable=(151 2 152 2 153 2 158 4 159 3 160 2 171 4 172 1 173 4)
> 
> Note that you must have an even number of elements in the assignment
> list, because they are taken as key value key value etc.  The
> assignment will fail otherwise.

Thanks, works perfectly.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-05-20 15:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-19 17:11 convert number tartifola
2010-05-19 18:39 ` Christian Walther
2010-05-20 14:51   ` Bart Schaefer
2010-05-20 14:56     ` tartifola

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).