zsh-users
 help / color / mirror / code / Atom feed
* Number of non-empty elements
@ 2015-09-15 17:15 Jesper Nygårds
  2015-09-15 21:43 ` Daniel Shahaf
  0 siblings, 1 reply; 3+ messages in thread
From: Jesper Nygårds @ 2015-09-15 17:15 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 554 bytes --]

Here's a very little thing that's annoying me.

I have a fairly long function where the result is stored in an array called
"mylines".

I want the function to return normally (status zero) if "mylines" contains
at least one non-empty element, and with non-zero otherwise.

This is the best I've come up with so far:

mylines=...

mylines=(${mylines}) # Remove empty elements
return $(( ${#mylines} == 0 ))

I particularly dislike that I have a separate line to remove empty lines.

Is there a more elegant, preferably one-line, solution to achieve this?

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

* Re: Number of non-empty elements
  2015-09-15 17:15 Number of non-empty elements Jesper Nygårds
@ 2015-09-15 21:43 ` Daniel Shahaf
  2015-09-16  5:16   ` Jesper Nygårds
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Shahaf @ 2015-09-15 21:43 UTC (permalink / raw)
  To: Jesper Nygårds; +Cc: Zsh Users

Jesper Nygårds wrote on Tue, Sep 15, 2015 at 19:15:01 +0200:
> I have a fairly long function where the result is stored in an array called
> "mylines".
> 
> I want the function to return normally (status zero) if "mylines" contains
> at least one non-empty element, and with non-zero otherwise.

    return $(( $mylines[(I)?*] > 0 )0

The (I) modifier interprets the key as a pattern and returns the highest
index of a match, or 0 if there was no match.


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

* Re: Number of non-empty elements
  2015-09-15 21:43 ` Daniel Shahaf
@ 2015-09-16  5:16   ` Jesper Nygårds
  0 siblings, 0 replies; 3+ messages in thread
From: Jesper Nygårds @ 2015-09-16  5:16 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 620 bytes --]

Thank you, Daniel. That works great.


On Tue, Sep 15, 2015 at 11:43 PM, Daniel Shahaf <d.s@daniel.shahaf.name>
wrote:

> Jesper Nygårds wrote on Tue, Sep 15, 2015 at 19:15:01 +0200:
> > I have a fairly long function where the result is stored in an array
> called
> > "mylines".
> >
> > I want the function to return normally (status zero) if "mylines"
> contains
> > at least one non-empty element, and with non-zero otherwise.
>
>     return $(( $mylines[(I)?*] > 0 )0
>
> The (I) modifier interprets the key as a pattern and returns the highest
> index of a match, or 0 if there was no match.
>

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

end of thread, other threads:[~2015-09-16  5:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-15 17:15 Number of non-empty elements Jesper Nygårds
2015-09-15 21:43 ` Daniel Shahaf
2015-09-16  5:16   ` Jesper Nygårds

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