Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: convert number



Hi,

On 19 May 2010 19:11,  <tartifola@xxxxxxxxx> 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



Messages sorted by: Reverse Date, Date, Thread, Author