zsh-users
 help / color / mirror / code / Atom feed
* joining array elements
@ 2006-01-12  1:22 Wataru Kagawa
  2006-01-12  1:41 ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Wataru Kagawa @ 2006-01-12  1:22 UTC (permalink / raw)
  To: zsh-users

Hi all,

I am trying to join array elements together using a string separator  
containing square brackets.  For example, if I have,

usernames=( Bob Sara Tim )

I would like,

Bob[return]Sara[return]Tim

as an output (without the backslashes in front of the square  
brackets).  I started out with,

echo ${(j:\[return\]:)usernames}

and added extra backslashes, but that did not work for me.  Help is  
greatly appreciated.

Wataru Kagawa


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

* Re: joining array elements
  2006-01-12  1:22 joining array elements Wataru Kagawa
@ 2006-01-12  1:41 ` Bart Schaefer
  2006-01-12  1:59   ` Wataru Kagawa
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2006-01-12  1:41 UTC (permalink / raw)
  To: Wataru Kagawa, zsh-users

On Jan 12, 10:22am, Wataru Kagawa wrote:
}
} Bob[return]Sara[return]Tim
} 
} as an output (without the backslashes in front of the square  
} brackets).  I started out with,
} 
} echo ${(j:\[return\]:)usernames}
} 
} and added extra backslashes, but that did not work for me.  Help is  
} greatly appreciated.

Did you try *removing* the backslashes?  This works fine for me:

echo ${(j:[return]:)usernames}

Also keep in mind that "echo" might be doing some conversions of its
own if you have anything unusual in $usernames -- you might be better
off with e.g.

print -R -- ${(j:[return]:)usernames}


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

* Re: joining array elements
  2006-01-12  1:41 ` Bart Schaefer
@ 2006-01-12  1:59   ` Wataru Kagawa
  2006-01-12  5:17     ` Wataru Kagawa
  0 siblings, 1 reply; 11+ messages in thread
From: Wataru Kagawa @ 2006-01-12  1:59 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Hi Bart,

When I removed the backslashes, I have no output.  Could this problem  
be related to the Zsh options I have set in my environment?  I have  
the following options set (or unset).

	setopt                         \
			auto_pushd             \
		 NO_beep                   \
			brace_ccl              \
		 NO_cdable_vars            \
		 NO_clobber                \
			complete_in_word       \
			correct_all            \
			extended_glob          \
			glob_complete          \
			glob_subst             \
			hist_allow_clobber     \
			hist_reduce_blanks     \
		 NO_hup                    \
			ignore_eof             \
			interactive_comments   \
		 NO_list_ambiguous         \
			local_options          \
			local_traps            \
			long_list_jobs         \
			magic_equal_subst      \
			null_glob              \
			path_dirs              \
			posix_builtins         \
			print_eight_bit        \
			pushd_ignore_dups      \
			pushd_minus            \
			pushd_to_home          \
			rc_expand_param        \
		 NO_sh_in_stdin            \
			share_history          \


I appreciate your help very much.

Wataru Kagawa


On 2006/01/12, at 10:41, Bart Schaefer wrote:

> On Jan 12, 10:22am, Wataru Kagawa wrote:
> }
> } Bob[return]Sara[return]Tim
> }
> } as an output (without the backslashes in front of the square
> } brackets).  I started out with,
> }
> } echo ${(j:\[return\]:)usernames}
> }
> } and added extra backslashes, but that did not work for me.  Help is
> } greatly appreciated.
>
> Did you try *removing* the backslashes?  This works fine for me:
>
> echo ${(j:[return]:)usernames}
>
> Also keep in mind that "echo" might be doing some conversions of its
> own if you have anything unusual in $usernames -- you might be better
> off with e.g.
>
> print -R -- ${(j:[return]:)usernames}


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

* Re: joining array elements
  2006-01-12  1:59   ` Wataru Kagawa
@ 2006-01-12  5:17     ` Wataru Kagawa
  2006-01-12  9:10       ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Wataru Kagawa @ 2006-01-12  5:17 UTC (permalink / raw)
  To: zsh-users

I noticed that to get the joining to work, I have to unset the  
'globsubst' option.

Wataru Kagawa


On 2006/01/12, at 10:59, Wataru Kagawa wrote:

> Hi Bart,
>
> When I removed the backslashes, I have no output.  Could this  
> problem be related to the Zsh options I have set in my  
> environment?  I have the following options set (or unset).
>
> 	setopt                         \
> 			auto_pushd             \
> 		 NO_beep                   \
> 			brace_ccl              \
> 		 NO_cdable_vars            \
> 		 NO_clobber                \
> 			complete_in_word       \
> 			correct_all            \
> 			extended_glob          \
> 			glob_complete          \
> 			glob_subst             \
> 			hist_allow_clobber     \
> 			hist_reduce_blanks     \
> 		 NO_hup                    \
> 			ignore_eof             \
> 			interactive_comments   \
> 		 NO_list_ambiguous         \
> 			local_options          \
> 			local_traps            \
> 			long_list_jobs         \
> 			magic_equal_subst      \
> 			null_glob              \
> 			path_dirs              \
> 			posix_builtins         \
> 			print_eight_bit        \
> 			pushd_ignore_dups      \
> 			pushd_minus            \
> 			pushd_to_home          \
> 			rc_expand_param        \
> 		 NO_sh_in_stdin            \
> 			share_history          \
>
>
> I appreciate your help very much.
>
> Wataru Kagawa
>
>
> On 2006/01/12, at 10:41, Bart Schaefer wrote:
>
>> On Jan 12, 10:22am, Wataru Kagawa wrote:
>> }
>> } Bob[return]Sara[return]Tim
>> }
>> } as an output (without the backslashes in front of the square
>> } brackets).  I started out with,
>> }
>> } echo ${(j:\[return\]:)usernames}
>> }
>> } and added extra backslashes, but that did not work for me.  Help is
>> } greatly appreciated.
>>
>> Did you try *removing* the backslashes?  This works fine for me:
>>
>> echo ${(j:[return]:)usernames}
>>
>> Also keep in mind that "echo" might be doing some conversions of its
>> own if you have anything unusual in $usernames -- you might be better
>> off with e.g.
>>
>> print -R -- ${(j:[return]:)usernames}
>


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

* Re: joining array elements
  2006-01-12  5:17     ` Wataru Kagawa
@ 2006-01-12  9:10       ` Peter Stephenson
  2006-01-12 11:44         ` Wataru Kagawa
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2006-01-12  9:10 UTC (permalink / raw)
  To: zsh-users

Wataru Kagawa wrote:
> I noticed that to get the joining to work, I have to unset the  
> 'globsubst' option.

You can use
  ${(j:[return]:)~~usernames}
to get that effect.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Your mail client is unable to display the latest news from CSR. To access our news copy this link into a web browser:  http://www.csr.com/email_sig.html


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

* Re: joining array elements
  2006-01-12  9:10       ` Peter Stephenson
@ 2006-01-12 11:44         ` Wataru Kagawa
  2006-01-12 11:54           ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Wataru Kagawa @ 2006-01-12 11:44 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

Hi Peter,

Thank you for the tip.  I would like to place the expansion into a  
variable, however, I am having a trouble.

% usernames=( Bob Sara Tim )
% print -R ${(j:[return]:)~~usernames}
Bob[return]Sara[return]Tim

However,

% foo=$( printf '%s\n' David Matt ${(j:[return]:)~~usernames} )
% echo $foo

I appreciate any suggestions.

Wataru Kagawa


On 2006/01/12, at 18:10, Peter Stephenson wrote:

> Wataru Kagawa wrote:
>> I noticed that to get the joining to work, I have to unset the
>> 'globsubst' option.
>
> You can use
>   ${(j:[return]:)~~usernames}
> to get that effect.
>
> -- 
> Peter Stephenson <pws@csr.com>                  Software Engineer
> CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
> Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223  
> 692070
>
>
> Your mail client is unable to display the latest news from CSR. To  
> access our news copy this link into a web browser:  http:// 
> www.csr.com/email_sig.html


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

* Re: joining array elements
  2006-01-12 11:44         ` Wataru Kagawa
@ 2006-01-12 11:54           ` Peter Stephenson
  2006-01-12 12:20             ` Wataru Kagawa
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2006-01-12 11:54 UTC (permalink / raw)
  To: Zsh users list

Wataru Kagawa wrote:
> Hi Peter,
> 
> Thank you for the tip.  I would like to place the expansion into a  
> variable, however, I am having a trouble.
> 
> % usernames=( Bob Sara Tim )
> % print -R ${(j:[return]:)~~usernames}
> Bob[return]Sara[return]Tim
> 
> However,
> 
> % foo=$( printf '%s\n' David Matt ${(j:[return]:)~~usernames} )
> % echo $foo

What effect are you trying to achieve?  The command you show does
exactly what I expect it to.  Do you mean you want David and Matt added
in the same way and the result put in foo?

usernames2=(David Matt $usernames)
foo=${(j:[return]:)~~usernames2}

Or do you mean you want the same effect as you get when outputting David
and Matt using printf, but with all the names, so that foo ends up being
joined with newlines?

foo=${(F)usernames2}

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Your mail client is unable to display the latest news from CSR. To access our news copy this link into a web browser:  http://www.csr.com/email_sig.html


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

* Re: joining array elements
  2006-01-12 11:54           ` Peter Stephenson
@ 2006-01-12 12:20             ` Wataru Kagawa
       [not found]               ` <10756.1137069559@logicacmg.com>
  2006-01-12 13:10               ` Peter Stephenson
  0 siblings, 2 replies; 11+ messages in thread
From: Wataru Kagawa @ 2006-01-12 12:20 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

I think my example was unclear (Sorry).  Basically, I wanted an  
output that looks like,

David
Matt
Bob[return]Sara[return]Tim

I can get it with,
foo=$( printf '%s\n' David Matt ${(j:[return]:)usernames} )
with the 'globsubst' option turned off.  However, I was trying to  
incorporate your tip, because I wanted to keep that option turned on.

foo=$( printf '%s\n' David Matt ${(j:[return]:)~~usernames} )
gives me no output (with the 'nullglob' option set).

Thanks.

On 2006/01/12, at 20:54, Peter Stephenson wrote:

> Wataru Kagawa wrote:
>> Hi Peter,
>>
>> Thank you for the tip.  I would like to place the expansion into a
>> variable, however, I am having a trouble.
>>
>> % usernames=( Bob Sara Tim )
>> % print -R ${(j:[return]:)~~usernames}
>> Bob[return]Sara[return]Tim
>>
>> However,
>>
>> % foo=$( printf '%s\n' David Matt ${(j:[return]:)~~usernames} )
>> % echo $foo
>
> What effect are you trying to achieve?  The command you show does
> exactly what I expect it to.  Do you mean you want David and Matt  
> added
> in the same way and the result put in foo?
>
> usernames2=(David Matt $usernames)
> foo=${(j:[return]:)~~usernames2}
>
> Or do you mean you want the same effect as you get when outputting  
> David
> and Matt using printf, but with all the names, so that foo ends up  
> being
> joined with newlines?
>
> foo=${(F)usernames2}
>
> -- 
> Peter Stephenson <pws@csr.com>                  Software Engineer
> CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
> Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223  
> 692070
>
>
> Your mail client is unable to display the latest news from CSR. To  
> access our news copy this link into a web browser:  http:// 
> www.csr.com/email_sig.html


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

* Re: joining array elements
       [not found]               ` <10756.1137069559@logicacmg.com>
@ 2006-01-12 13:01                 ` Wataru Kagawa
  0 siblings, 0 replies; 11+ messages in thread
From: Wataru Kagawa @ 2006-01-12 13:01 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-users

Hi Oliver,

Yes!  $~~foo did the trick.
I am grateful to all the helpful advices.
Thanks again.

Wataru Kagawa

On 2006/01/12, at 21:39, Oliver Kiddle wrote:

> Wataru Kagawa wrote:
>> foo=$( printf '%s\n' David Matt ${(j:[return]:)~~usernames} )
>> gives me no output (with the 'nullglob' option set).
>
> It works for me. How are you going about inspecting the value of the
> variable foo after this?
>
> try
>   echo $~~foo
> or
>   typeset -p foo
>
> Oliver
>
>
> This e-mail and any attachment is for authorised use by the  
> intended recipient(s) only. It may contain proprietary material,  
> confidential information and/or be subject to legal privilege. It  
> should not be copied, disclosed to, retained or used by, any other  
> party. If you are not an intended recipient then please promptly  
> delete this e-mail and any attachment and all copies and inform the  
> sender. Thank you.


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

* Re: joining array elements
  2006-01-12 12:20             ` Wataru Kagawa
       [not found]               ` <10756.1137069559@logicacmg.com>
@ 2006-01-12 13:10               ` Peter Stephenson
  2006-01-12 15:16                 ` Wataru Kagawa
  1 sibling, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2006-01-12 13:10 UTC (permalink / raw)
  To: Zsh users list

Wataru Kagawa wrote:
> I think my example was unclear (Sorry).  Basically, I wanted an  
> output that looks like,
> 
> David
> Matt
> Bob[return]Sara[return]Tim
> 
> I can get it with,
> foo=$( printf '%s\n' David Matt ${(j:[return]:)usernames} )
> with the 'globsubst' option turned off.  However, I was trying to  
> incorporate your tip, because I wanted to keep that option turned on.
> 
> foo=$( printf '%s\n' David Matt ${(j:[return]:)~~usernames} )
> gives me no output (with the 'nullglob' option set).

Are you sure that it's not the nullglob option being applied to the
expansion of $foo?
Try

  print -r "$foo"

You can also do

  usernames=(Bob Sara Tim)
  usernames2=(David Matt ${(j.[return].)~~usernames})
  foo=${(F)~~usernames2}

Come to think of it, simply quoting the values is a bit neater than the
the ~~ flag:

  usernames=(Bob Sara Tim)
  usernames2=(David Matt "${(j.[return].)usernames}")
  foo="${(F)usernames2}"

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Your mail client is unable to display the latest news from CSR. To access our news copy this link into a web browser:  http://www.csr.com/email_sig.html


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

* Re: joining array elements
  2006-01-12 13:10               ` Peter Stephenson
@ 2006-01-12 15:16                 ` Wataru Kagawa
  0 siblings, 0 replies; 11+ messages in thread
From: Wataru Kagawa @ 2006-01-12 15:16 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users, Wataru Kagawa

Hi Peter,

I am not sure if I understand your question about the nullglob  
option, but regardless of the nullglob option,

   echo $foo

didn't output the right thing.  The difference between having or not  
having the nullglob option was, no output when nullglob option was  
set versus the following output when the nullglob option was turned off.

   zsh: no matches found: David\nMatt\nBob[return]Sara[return]Tim

print -r "$foo" worked under both conditions.  Actually, I wanted to  
pass the value to a command (zsh function), so the two examples you  
gave me,

>   usernames=(Bob Sara Tim)
>   usernames2=(David Matt ${(j.[return].)~~usernames})
>   foo=${(F)~~usernames2}
>
>   usernames=(Bob Sara Tim)
>   usernames2=(David Matt "${(j.[return].)usernames}")
>   foo="${(F)usernames2}"

are exactly what I was looking for.  They work perfectly.

Thank you very much.  I really appreciate all the help.

Wataru Kagawa


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

end of thread, other threads:[~2006-01-12 15:16 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-12  1:22 joining array elements Wataru Kagawa
2006-01-12  1:41 ` Bart Schaefer
2006-01-12  1:59   ` Wataru Kagawa
2006-01-12  5:17     ` Wataru Kagawa
2006-01-12  9:10       ` Peter Stephenson
2006-01-12 11:44         ` Wataru Kagawa
2006-01-12 11:54           ` Peter Stephenson
2006-01-12 12:20             ` Wataru Kagawa
     [not found]               ` <10756.1137069559@logicacmg.com>
2006-01-12 13:01                 ` Wataru Kagawa
2006-01-12 13:10               ` Peter Stephenson
2006-01-12 15:16                 ` Wataru Kagawa

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