zsh-users
 help / color / mirror / code / Atom feed
* Parameter expansion: tr?
@ 2001-04-06 13:13 Vincent Lefevre
  2001-04-06 16:53 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Vincent Lefevre @ 2001-04-06 13:13 UTC (permalink / raw)
  To: zsh-users

I've seen how one can do a string substitution in a parameter expansion,
but how can I do a tr, e.g. to swap the "." and "/" characters?

Is there a simple way to do that, or do I need associative arrays?

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/> - 100%
validated HTML - Acorn Risc PC, Yellow Pig 17, Championnat International des
Jeux Mathématiques et Logiques, TETRHEX, etc.
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


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

* Re: Parameter expansion: tr?
  2001-04-06 13:13 Parameter expansion: tr? Vincent Lefevre
@ 2001-04-06 16:53 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2001-04-06 16:53 UTC (permalink / raw)
  To: zsh-users, Vincent Lefevre

On Apr 6,  3:13pm, Vincent Lefevre wrote:
} Subject: Parameter expansion: tr?
}
} I've seen how one can do a string substitution in a parameter expansion,
} but how can I do a tr, e.g. to swap the "." and "/" characters?
} 
} Is there a simple way to do that, or do I need associative arrays?

The following requires 3.1.9 or later:

  function ztr {
    setopt extendedglob noshwordsplit
    local chunk=''
    while read -u0k 4096 chunk; do
	print -Rn ${chunk//(#b)([$1])/${2[${1[(I)$match]}]}}
	chunk=''
    done
    # "read -k" will exit nonzero on the last partial chunk, print it
    (( $#chunk )) && print -Rn ${chunk//(#b)([$1])/${2[${1[(I)$match]}]}}
  }

% ztr "abcde ." "NOPQR-/" <<EOF
The quick brown fox jumped over the lazy dog.
EOF
ThR-quiPk-Orown-fox-jumpRQ-ovRr-thR-lNzy-Qog/
% 

I think the "read -k" behavior on a partial chunk is a bug, but I seem to
recall some zsh-workers discussion to the effect that other shells also
behave that way.  I may be thinking of some other "read" behavior, though.

Using ${1[(I)$match]} emulates the real `tr aaa xyz` == `tr a z` behavior.
If you give the above function only one argument, it acts like "tr -d".
"tr -s" and "tr -c" left as exercises for the reader.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

end of thread, other threads:[~2001-04-06 16:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-06 13:13 Parameter expansion: tr? Vincent Lefevre
2001-04-06 16:53 ` Bart Schaefer

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