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

Re: kinda perl split ...



On Tue, Mar 21, 2006 at 02:42:12PM +0100, Marc Chantreux wrote:
> Hi all,
> 
> i'm always searching for shortest ways in zsh (coming from perl world).
> 
> in perl :
> 
> while ( <>) {
> 	chomp;
> 	my ( $a , $b ) = split /\t/;
> 	print "b=$b a=$a"
> }
> 
> in zsh :
> 
> while {read} {
> 	content=( ${(ps:\t:)REPLY} )
> 	a=$content[1]
> 	b=content[2]
> 	print "b=$b a=$a"
> }
> 
> is there something more faster to initialize a and b (peraps closer than 
> split) ?

Just use the read builtin:

  echo foo bar | while read a b junk
  do
    echo "a=$a b=$b"
  done

I've got a habit of reading any remaining fields into a variable called
"junk", just in case.

-Dom



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