zsh-users
 help / color / mirror / code / Atom feed
* extracting fields
@ 2003-12-22  8:38 Dominic Mitchell
  2003-12-22  9:51 ` Oliver Kiddle
  0 siblings, 1 reply; 4+ messages in thread
From: Dominic Mitchell @ 2003-12-22  8:38 UTC (permalink / raw)
  To: zsh-users

I'm trying to extract stuff from a string in a similiar manner to cut(1)
and getting rather unstuck.  I'm sure that there's a simpler answer, but
I can't seem to find it.

I have the output of psql in an array:

    pg_tables=( "${(f)$( psql -At -c '\d' )}" )

Which gives an array of entries like these:

    % echo $pg_tables[0]
    public|book|table|dom

Now I'm interested in the table name which is the 2nd field.  But I
can't work out how.  I've been playing with things like this:

    % echo ${(@s:|:)pg_tables[2]}

But I can't figure out how to split the array and then subscript the
second word of that newly split array.  Any ideas on how I would do
this?

Thanks,
-Dom


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

* Re: extracting fields
  2003-12-22  8:38 extracting fields Dominic Mitchell
@ 2003-12-22  9:51 ` Oliver Kiddle
  2003-12-22 10:09   ` James Devenish
  2003-12-22 10:24   ` Dominic Mitchell
  0 siblings, 2 replies; 4+ messages in thread
From: Oliver Kiddle @ 2003-12-22  9:51 UTC (permalink / raw)
  To: zsh-users

Dominic Mitchell wrote:
> I'm trying to extract stuff from a string in a similiar manner to cut(1)
> and getting rather unstuck.  I'm sure that there's a simpler answer, but
> I can't seem to find it.

> Now I'm interested in the table name which is the 2nd field.  But I
> can't work out how.  I've been playing with things like this:
> 
>     % echo ${(@s:|:)pg_tables[2]}

Zsh has no concept of two-dimensional arrays so you can't split every
element of an array and expect indexing tow work on each element like
that. You would have to have a for loop for each array element.

Alternatively, if it is is the second field you want, you can
probably do:

   ${${pg_tables#*|}%%|*}

Oliver


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

* Re: extracting fields
  2003-12-22  9:51 ` Oliver Kiddle
@ 2003-12-22 10:09   ` James Devenish
  2003-12-22 10:24   ` Dominic Mitchell
  1 sibling, 0 replies; 4+ messages in thread
From: James Devenish @ 2003-12-22 10:09 UTC (permalink / raw)
  To: zsh-users

In message <25039.1072086714@gmcs3.local>
on Mon, Dec 22, 2003 at 10:51:54AM +0100, Oliver Kiddle wrote:
> Dominic Mitchell wrote:
> Zsh has no concept of two-dimensional arrays so you can't split every
> element of an array and expect indexing tow work on each element like
> that. You would have to have a for loop for each array element.
> 
> Alternatively, if it is is the second field you want, you can
> probably do:
> 
>    ${${pg_tables#*|}%%|*}

Yes, I think he might simply have been overlooking the ${...${...}...}
construct. For example:

> Dominic Mitchell wrote:
> % echo $pg_tables[0]
> public|book|table|dom
[...]
> Now I'm interested in the table name which is the 2nd field.

Selecting "column c" of "record r" could be like this:
% integer r=1 c=2; echo ${${(s:|:)pg_tables[$r]}[$c]}



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

* Re: extracting fields
  2003-12-22  9:51 ` Oliver Kiddle
  2003-12-22 10:09   ` James Devenish
@ 2003-12-22 10:24   ` Dominic Mitchell
  1 sibling, 0 replies; 4+ messages in thread
From: Dominic Mitchell @ 2003-12-22 10:24 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-users

On Mon, Dec 22, 2003 at 10:51:54AM +0100, Oliver Kiddle wrote:
> Dominic Mitchell wrote:
> > I'm trying to extract stuff from a string in a similiar manner to cut(1)
> > and getting rather unstuck.  I'm sure that there's a simpler answer, but
> > I can't seem to find it.
> 
> > Now I'm interested in the table name which is the 2nd field.  But I
> > can't work out how.  I've been playing with things like this:
> > 
> >     % echo ${(@s:|:)pg_tables[2]}
> 
> Zsh has no concept of two-dimensional arrays so you can't split every
> element of an array and expect indexing tow work on each element like
> that. You would have to have a for loop for each array element.

Aha, thank you.  I was thinking that I could do it in a for loop.

> Alternatively, if it is is the second field you want, you can
> probably do:
> 
>    ${${pg_tables#*|}%%|*}

That's perfect.  Thank you very much.  Funnily enough, I started going
down that line, until I realised I was interested in the second field,
not the first.  I got the %%|* bit, but didn't make the leap to
stripping the first field.

Many thanks,
-Dom


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

end of thread, other threads:[~2003-12-22 10:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-22  8:38 extracting fields Dominic Mitchell
2003-12-22  9:51 ` Oliver Kiddle
2003-12-22 10:09   ` James Devenish
2003-12-22 10:24   ` Dominic Mitchell

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