zsh-users
 help / color / mirror / code / Atom feed
* modify or delete variable
@ 2024-04-06 14:57 Ray Andrews
  2024-04-07  8:10 ` Lawrence Velázquez
  0 siblings, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2024-04-06 14:57 UTC (permalink / raw)
  To: Zsh Users

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

     var=( ${(M)var:#*(#i)$substring*} )
     var=( ${var//(#bi)($substring)/aaa${match[1]}bbb )

... can those to operations be combined?  If a string exists in a 
variable, modify it, else delete it?  I'm betting yes.  Mind, there's 
nothing wrong with two easy to understand steps even if a microsecond 
might be saved by combining them.

[-- Attachment #2: Type: text/html, Size: 630 bytes --]

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

* Re: modify or delete variable
  2024-04-06 14:57 modify or delete variable Ray Andrews
@ 2024-04-07  8:10 ` Lawrence Velázquez
  2024-04-07 13:24   ` Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Lawrence Velázquez @ 2024-04-07  8:10 UTC (permalink / raw)
  To: zsh-users

On Sat, Apr 6, 2024, at 10:57 AM, Ray Andrews wrote:
>     var=( ${(M)var:#*(#i)$substring*} )
>     var=( ${var//(#bi)($substring)/aaa${match[1]}bbb )
>
> ... can those to operations be combined?  If a string exists in a 
> variable, modify it, else delete it?

var=(${${(M)var:#*(#i)$substring*}//(#bi)($substring)/aaa${match[1]}bbb})

-- 
vq


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

* Re: modify or delete variable
  2024-04-07  8:10 ` Lawrence Velázquez
@ 2024-04-07 13:24   ` Ray Andrews
  2024-04-07 17:53     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2024-04-07 13:24 UTC (permalink / raw)
  To: zsh-users



On 2024-04-07 01:10, Lawrence Velázquez wrote:
> On Sat, Apr 6, 2024, at 10:57 AM, Ray Andrews wrote:
>>      var=( ${(M)var:#*(#i)$substring*} )
>>      var=( ${var//(#bi)($substring)/aaa${match[1]}bbb} )
>>
>> ... can those to operations be combined?  If a string exists in a
>> variable, modify it, else delete it?
> var=(${${(M)var:#*(#i)$substring*}//(#bi)($substring)/aaa${match[1]}bbb})
>
Yeah, nesting like that would have to work.  It's hard on the eyes at 
first  but the step by step substitution logic is there.  But I wonder 
why this doesn't work:

     var=( ${(M)var//(#bi)($substring)/aaa${match[1]}bbb} )

... seems to me the (M) would have the same function -- if the substring isn't there to be matched it can't be modified so kill the line. In both cases the rule would be that empty lines are removed. Functionality would be added and AFAICT nothing would be lost or broken. The (M) doesn't throw an error but neither does it do anything.  Or ... is this one of those things were it is doing something but I misread the intention?







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

* Re: modify or delete variable
  2024-04-07 13:24   ` Ray Andrews
@ 2024-04-07 17:53     ` Bart Schaefer
  2024-04-07 22:35       ` Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2024-04-07 17:53 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

On Sun, Apr 7, 2024 at 9:24 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> On 2024-04-07 01:10, Lawrence Velázquez wrote:
> > var=(${${(M)var:#*(#i)$substring*}//(#bi)($substring)/aaa${match[1]}bbb})
> >
> Yeah, nesting like that would have to work.  It's hard on the eyes at
> first  but the step by step substitution logic is there.  But I wonder
> why this doesn't work:
>
>      var=( ${(M)var//(#bi)($substring)/aaa${match[1]}bbb} )

It doesn't work because the (M) flag only applies to the ${...#...} or
${...%...} replacements.  This is documented.

> ... seems to me the (M) would have the same function -- if the substring isn't there to be matched it can't be modified so kill the line.

That doesn't make sense in any case -- (M) means "include the matched
string", that is, reverse the effect of the :# operator which means
"delete the matching value".  What's the "reverse" of "replace A with
B anywhere"?  Certainly not "delete everything".


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

* Re: modify or delete variable
  2024-04-07 17:53     ` Bart Schaefer
@ 2024-04-07 22:35       ` Ray Andrews
  0 siblings, 0 replies; 5+ messages in thread
From: Ray Andrews @ 2024-04-07 22:35 UTC (permalink / raw)
  To: zsh-users

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



On 2024-04-07 10:53, Bart Schaefer wrote:
>> ... seems to me the (M) would have the same function -- if the substring isn't there to be matched it can't be modified so kill the line.
> That doesn't make sense in any case -- (M) means "include the matched
> string", that is, reverse the effect of the :# operator which means
> "delete the matching value".  What's the "reverse" of "replace A with
> B anywhere"?  Certainly not "delete everything".
>
Yup, another example of getting the story wrong.

[-- Attachment #2: Type: text/html, Size: 1034 bytes --]

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

end of thread, other threads:[~2024-04-07 22:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-06 14:57 modify or delete variable Ray Andrews
2024-04-07  8:10 ` Lawrence Velázquez
2024-04-07 13:24   ` Ray Andrews
2024-04-07 17:53     ` Bart Schaefer
2024-04-07 22:35       ` Ray Andrews

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