zsh-users
 help / color / mirror / code / Atom feed
* Getting a particular char
@ 2004-08-06 10:55 DervishD
  2004-08-10 11:51 ` DervishD
  0 siblings, 1 reply; 3+ messages in thread
From: DervishD @ 2004-08-06 10:55 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

    First of all, my excuses because although this is a shell related
question, is not exactly a zsh related question. Is more about
portability, about how to implement a zsh facility in portable shell.

    If I want to get the character number 'n' from a parameter, in
zsh I do something like: $parameter[n]. Easy, short... and non
portable.

    So I change it to something like 'expr substr $parameter n 1'.
This has some problems, like '$parameter' needing some tweaking so
'expr' doesn't whine because it starts with '(', '+', etc. or it is
something like the word 'match'. But all that is not important,
because the 'substr' expression is not SUSv3 compliant, so I cannot
do that neither.

    So I'm implementing it like that:

    # Dirty and ugly hack, needs some polishing...
    # First we get rid of all characters at the beginning
    while [ ${#parameter} -gt n ]
    do
        parameter=${parameter#?}
    done

    #   Now, get rid of the characters after
    # the one we are interested in

    while [ ${#parameter} -gt 1 ]
    do
        parameter=${parameter%?}
    done

    I would like another ideas, shorter, faster, or simply less ugly,
and it must be portable, so no zsh tricks :(( If you could please
help me... Thanks a lot in advance.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Getting a particular char
  2004-08-06 10:55 Getting a particular char DervishD
@ 2004-08-10 11:51 ` DervishD
  0 siblings, 0 replies; 3+ messages in thread
From: DervishD @ 2004-08-10 11:51 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

 * DervishD <raul@pleyades.net> dixit:
>     If I want to get the character number 'n' from a parameter
[...]
>     So I change it to something like 'expr substr $parameter n 1'.
> This has some problems, like '$parameter' needing some tweaking so
> 'expr' doesn't whine because it starts with '(', '+', etc. or it is
> something like the word 'match'. But all that is not important,
> because the 'substr' expression is not SUSv3 compliant, so I cannot
> do that neither.

    Finally I did this way (thanks to Paul Eggert, coauthor of GNU
coreutils, for the help):

    expr "x$parameter" : ".\{n\}\(.\)"

    Where 'n' is the index of the character we want. Easy and fully
SUSv3 compliant, where {N} is a valid BRE for repetition.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* RE: Getting a particular char
@ 2004-08-06 11:24 Sean Johnston
  0 siblings, 0 replies; 3+ messages in thread
From: Sean Johnston @ 2004-08-06 11:24 UTC (permalink / raw)
  To: Zsh Users

From: DervishD [mailto:raul@pleyades.net]
Sent: 06 August 2004 11:56

>     So I'm implementing it like that:
> 
>     # Dirty and ugly hack, needs some polishing...
>     # First we get rid of all characters at the beginning
>     while [ ${#parameter} -gt n ]
>     do
>         parameter=${parameter#?}
>     done
> 
>     #   Now, get rid of the characters after
>     # the one we are interested in
> 
>     while [ ${#parameter} -gt 1 ]
>     do
>         parameter=${parameter%?}
>     done
> 
> I would like another ideas, shorter, faster, or simply less ugly,
> and it must be portable, so no zsh tricks :(( If you could please
> help me... Thanks a lot in advance.
> 

How about:

	parameter=`echo "$parameter" | cut -cn`

Sean.

_____________________________________________________________________
This message has been checked for all known viruses by Xenicom delivered through the MessageLabs Virus Control Centre. 


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

end of thread, other threads:[~2004-08-10 11:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-06 10:55 Getting a particular char DervishD
2004-08-10 11:51 ` DervishD
2004-08-06 11:24 Sean Johnston

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).