zsh-users
 help / color / mirror / code / Atom feed
* The amazing array feature in zsh
@ 2006-08-16  4:48 sac
  2006-08-16  6:10 ` John Reese
  2006-08-16  7:12 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: sac @ 2006-08-16  4:48 UTC (permalink / raw)
  To: Zsh users list

The array subject in zsh is too huge and fascinating.
I discovered this amazing feature of array recently,
assigning values just like we do in some high level
language. 
Here is a example,

    files=()			# initialize to null
    for mfile in `svn stat $1 | grep '^M' | awk '{
print $2 }'`
      do
      files+=($mfile)
    done
    print -c $files		# print with tabs

Notice tha assignment to the array variable +=. 

And actually this can be iterated like,
    
    for file in $files
    do
        <do something with $file>
    done

I dont know if any other shell provides similar
feature, but this one is too good and useful, and
makes the use of array in shell very easy. But I dont
think this is documented in zsh, atleast I couldn't
find it.

Regards
sac.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


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

* Re: The amazing array feature in zsh
  2006-08-16  4:48 The amazing array feature in zsh sac
@ 2006-08-16  6:10 ` John Reese
  2006-08-16  7:12 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: John Reese @ 2006-08-16  6:10 UTC (permalink / raw)
  To: sac; +Cc: Zsh users list

On 8/15/06, sac <sac_urs@yahoo.co.in> wrote:
> ...
> I dont know if any other shell provides similar
> feature, but this one is too good and useful, and
> makes the use of array in shell very easy. But I dont
> think this is documented in zsh, atleast I couldn't
> find it.
>
> Regards
> sac.
>

it's mentioned briefly in the Array Parameters section of zshparam(1).
 I agree that it's useful and maybe not obvious enough in the
documentation; I find it especially useful with the path variable.
When you look at the baroque suites of functions bash users are
constantly reimplementing and filling global dotfiles with to build up
PATH without duplicates, you realize how much more attractive the zsh
option is:

typeset -U path
path+=(~/bin /sw/bin /opt/local/bin)


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

* Re: The amazing array feature in zsh
  2006-08-16  4:48 The amazing array feature in zsh sac
  2006-08-16  6:10 ` John Reese
@ 2006-08-16  7:12 ` Bart Schaefer
  2006-08-16 10:07   ` sac
  1 sibling, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2006-08-16  7:12 UTC (permalink / raw)
  To: Zsh users list

On Aug 15,  9:49pm, sac wrote:
}
} I discovered this amazing feature of array recently,
} assigning values just like we do in some high level
} language. 

Gosh, and here all these years I thought the shell *was* a high-level
language. :-)

} Here is a example,
} 
}     files=()      		# initialize to null
}     for mfile in `svn stat $1 | grep '^M' | awk '{
} print $2 }'`
}       do
}       files+=($mfile)
}     done

It gets better ... recent versions of zsh can do this:

    for svncode svnfile in $(svn stat $1)
    do
      case $svncode in
      (M) files+=($svnfile);;
      esac
    done

}     print -c $files      	# print with tabs
} 
} Notice tha assignment to the array variable +=. 

I don't know why this isn't discussed under "Array Parameters".  It
gets a passing mention earlier (as John R. pointed out) but that's
only about scalars.  Then it's mentioned in a different context under
"Subscript Parsing":

    It is possible to avoid the use of subscripts in assignments to
    associative array elements by using the syntax:

        aa+=('key with "*strange*" characters' 'value string')

    This adds a new key/value pair if the key is not already present,
    and replaces the value for the existing key if it is.
 
} And actually this can be iterated like,
}     
}     for file in $files
}     do
}         <do something with $file>
}     done
} 
} I dont know if any other shell provides similar
} feature, but this one is too good and useful, and
} makes the use of array in shell very easy. But I dont
} think this is documented in zsh, atleast I couldn't
} find it.

The behavior as in "for file in $files" context is described in the
"Parameter Expansion" section:

    If NAME is an array parameter, and the KSH_ARRAYS option is not
    set, then the value of each element of NAME is substituted, one
    element per word.  Otherwise, the expansion results in one word
    only; with KSH_ARRAYS, this is the first element of an array. No
    field splitting is done on the result unless the SH_WORD_SPLIT
    option is set.


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

* Re: The amazing array feature in zsh
  2006-08-16  7:12 ` Bart Schaefer
@ 2006-08-16 10:07   ` sac
  0 siblings, 0 replies; 4+ messages in thread
From: sac @ 2006-08-16 10:07 UTC (permalink / raw)
  To: Bart Schaefer, Zsh users list

> 
> It gets better ... recent versions of zsh can do
> this:
> 
>     for svncode svnfile in $(svn stat $1)
>     do
>       case $svncode in
>       (M) files+=($svnfile);;
>       esac
>     done
> 
yeah, thanks, this part is really good... 

regards,
sac.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


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

end of thread, other threads:[~2006-08-16 10:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-16  4:48 The amazing array feature in zsh sac
2006-08-16  6:10 ` John Reese
2006-08-16  7:12 ` Bart Schaefer
2006-08-16 10:07   ` sac

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