Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #216 > unrolled thread

two dimensional arrays

Started bydmjcunha <dmjcunha@gmail.com>
First post2011-05-29 15:37 -0700
Last post2011-05-30 05:05 -0700
Articles 6 — 3 participants

Back to article view | Back to comp.lang.postscript


Contents

  two dimensional arrays dmjcunha <dmjcunha@gmail.com> - 2011-05-29 15:37 -0700
    Re: two dimensional arrays "Mark T. B. Carroll" <mtbc@bcs.org> - 2011-05-29 18:48 -0400
      Re: two dimensional arrays luser- -droog <mijoryx@yahoo.com> - 2011-05-30 22:43 -0700
    Re: two dimensional arrays luser- -droog <mijoryx@yahoo.com> - 2011-05-29 19:57 -0700
      Re: two dimensional arrays luser- -droog <mijoryx@yahoo.com> - 2011-05-29 21:29 -0700
        Re: two dimensional arrays dmjcunha <dmjcunha@gmail.com> - 2011-05-30 05:05 -0700

#216 — two dimensional arrays

Fromdmjcunha <dmjcunha@gmail.com>
Date2011-05-29 15:37 -0700
Subjecttwo dimensional arrays
Message-ID<ce9a7ae2-b2c1-4248-8965-1dae803f9b5a@bl1g2000vbb.googlegroups.com>
Hi. I don't know much of postscript I would like some help. How can I
create a 2 dimensional array of null elements? How can I put an
element in a 2 dimensional array? And how can I get an element in a 2
dimensional array?
Thanks in advance.

[toc] | [next] | [standalone]


#217

From"Mark T. B. Carroll" <mtbc@bcs.org>
Date2011-05-29 18:48 -0400
Message-ID<87oc2lxgvy.fsf@ixod.org>
In reply to#216
dmjcunha <dmjcunha@gmail.com> writes:

> Hi. I don't know much of postscript I would like some help. How can I
> create a 2 dimensional array of null elements? How can I put an
> element in a 2 dimensional array? And how can I get an element in a 2
> dimensional array?

You do it as an array of one-dimensional arrays. One useful trick can
be, if an element can't take many different values, an array of strings
can work too.

Mark

[toc] | [prev] | [next] | [standalone]


#221

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-30 22:43 -0700
Message-ID<b07c00b8-8bb0-499b-8931-408173117c97@24g2000yqk.googlegroups.com>
In reply to#217
On May 29, 5:48 pm, "Mark T. B. Carroll" <m...@bcs.org> wrote:
> dmjcunha <dmjcu...@gmail.com> writes:
> > Hi. I don't know much of postscript I would like some help. How can I
> > create a 2 dimensional array of null elements? How can I put an
> > element in a 2 dimensional array? And how can I get an element in a 2
> > dimensional array?
>
> You do it as an array of one-dimensional arrays. One useful trick can
> be, if an element can't take many different values, an array of strings
> can work too.
>
> Mark

One advantage of postscript polymorphism is that all composite
objects
can be accessed using the same syntax with get and put.

/A [ [ 16#30 16#31 16#32 16#33 ]
     [ 16#34 16#35 16#36 16#37 ] ] def
/B [ (0123) (4567) ] def
/C << 0 (0123) 1 (4567) >> def

A [ 0 0 ] {get} forall
B [ 0 0 ] {get} forall
C [ 0 0 ] {get} forall

All three of these fetch the integer 48 (ascii zero) using 0 get 0
get.
But one is an array of arrays of integers, one is an array of strings,
and the third is a dictionary of strings with integer keys.

[toc] | [prev] | [next] | [standalone]


#218

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-29 19:57 -0700
Message-ID<84e356d7-2258-4591-b701-885ba5bffa8b@v8g2000yqb.googlegroups.com>
In reply to#216
On May 29, 5:37 pm, dmjcunha <dmjcu...@gmail.com> wrote:
> Hi. I don't know much of postscript I would like some help. How can I
> create a 2 dimensional array of null elements? How can I put an
> element in a 2 dimensional array? And how can I get an element in a 2
> dimensional array?
> Thanks in advance.

As Mark said, postscript doesn't have multi-dimensional arrays per
se.
But since an array is a first-class object, arrays can contain arrays
to an arbitrary depth.

Probably the easiest way to create an n-element array of m-element
arrays of null is:

    [ n { m array } repeat ]

So if you define A to yield a 2 by 2 array,

    /A [ 2 { 2 array } repeat ] def

you can set element (x,y) by getting row x and putting into column y.

    A 0 get 0 1 put
    A 0 get 1 0 put
    A 1 get 0 0 put
    A 1 get 1 1 put

This would be equivalent to constructing the array more directly:

    /A [ [ 1 0 ] [ 0 1 ] ] def

For retrieving element (0,0) you can do:

    A 0 get 0 get

Or the "shortcut":

    A [ 0 0 ] { get } forall

HTH
L
X
T

[toc] | [prev] | [next] | [standalone]


#219

Fromluser- -droog <mijoryx@yahoo.com>
Date2011-05-29 21:29 -0700
Message-ID<d67176cd-3d2d-4ad8-97be-10f0944cdd9f@s2g2000yql.googlegroups.com>
In reply to#218
On May 29, 9:57 pm, luser- -droog <mijo...@yahoo.com> wrote:
> On May 29, 5:37 pm, dmjcunha <dmjcu...@gmail.com> wrote:
>
> > Hi. I don't know much of postscript I would like some help. How can I
> > create a 2 dimensional array of null elements? How can I put an
> > element in a 2 dimensional array? And how can I get an element in a 2
> > dimensional array?
> > Thanks in advance.
>
> As Mark said, postscript doesn't have multi-dimensional arrays per
> se.
> But since an array is a first-class object, arrays can contain arrays
> to an arbitrary depth.
>
> Probably the easiest way to create an n-element array of m-element
> arrays of null is:
>
>     [ n { m array } repeat ]
>
> So if you define A to yield a 2 by 2 array,
>
>     /A [ 2 { 2 array } repeat ] def
>
> you can set element (x,y) by getting row x and putting into column y.
>
>     A 0 get 0 1 put
>     A 0 get 1 0 put
>     A 1 get 0 0 put
>     A 1 get 1 1 put
>
> This would be equivalent to constructing the array more directly:
>
>     /A [ [ 1 0 ] [ 0 1 ] ] def
>
> For retrieving element (0,0) you can do:
>
>     A 0 get 0 get
>
> Or the "shortcut":
>
>     A [ 0 0 ] { get } forall

I was intrigued enough to explore whether the "shortcut" could be
used for both put and get. The result is a little complicated, but
it works.

%!

% array {all-but} {last}  forall-1  -
% "forall minus one"
% execute {all-but} for elements 0 .. n-2
% execute {last} for element n-1
/STATIC 2 array def
/forall-1 {
    <<
        0 { pop pop pop } % length == 0: do nothing
        1 { exch pop forall } % length == 1: do {last}
    >>
    3 index length %dup =
    2 copy known { get exec }{ pop pop
        2 index 0
        1 index length
        1 sub getinterval %dup ==
        3 1 roll
        4 -1 roll
        dup length 1 sub get %dup =
        exch
        //STATIC astore pop
        forall
        //STATIC aload pop
        exec
    } ifelse
} def

% m-array [ indices ]  mget  element(indices)
/mget {
    { get } forall
} def

% m-array [ indices ] value  mput  -
/mput {
    3 1 roll { get } { 3 -1 roll put } forall-1
} def


/A
[ 5 {
    [ 5 {
        [ 5 { null } repeat ]
    } repeat ]
} repeat ] def

A [ 4 4 4 ] 37 mput
A [ 4 4 4 ] mget
=

currentfile flushfile
!!gs %
GPL Ghostscript 8.62 (2008-02-29)
Copyright (C) 2008 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
37
GS>GS>

[toc] | [prev] | [next] | [standalone]


#220

Fromdmjcunha <dmjcunha@gmail.com>
Date2011-05-30 05:05 -0700
Message-ID<aa86a981-bbcb-4af1-ae89-52879210b6da@j23g2000yqc.googlegroups.com>
In reply to#219
On 30 maio, 00:29, luser- -droog <mijo...@yahoo.com> wrote:
> On May 29, 9:57 pm, luser- -droog <mijo...@yahoo.com> wrote:
>
>
>
> > On May 29, 5:37 pm, dmjcunha <dmjcu...@gmail.com> wrote:
>
> > > Hi. I don't know much of postscript I would like some help. How can I
> > > create a 2 dimensional array of null elements? How can I put an
> > > element in a 2 dimensional array? And how can I get an element in a 2
> > > dimensional array?
> > > Thanks in advance.
>
> > As Mark said, postscript doesn't have multi-dimensional arrays per
> > se.
> > But since an array is a first-class object, arrays can contain arrays
> > to an arbitrary depth.
>
> > Probably the easiest way to create an n-element array of m-element
> > arrays of null is:
>
> >     [ n { m array } repeat ]
>
> > So if you define A to yield a 2 by 2 array,
>
> >     /A [ 2 { 2 array } repeat ] def
>
> > you can set element (x,y) by getting row x and putting into column y.
>
> >     A 0 get 0 1 put
> >     A 0 get 1 0 put
> >     A 1 get 0 0 put
> >     A 1 get 1 1 put
>
> > This would be equivalent to constructing the array more directly:
>
> >     /A [ [ 1 0 ] [ 0 1 ] ] def
>
> > For retrieving element (0,0) you can do:
>
> >     A 0 get 0 get
>
> > Or the "shortcut":
>
> >     A [ 0 0 ] { get } forall
>
> I was intrigued enough to explore whether the "shortcut" could be
> used for both put and get. The result is a little complicated, but
> it works.
>
> %!
>
> % array {all-but} {last}  forall-1  -
> % "forall minus one"
> % execute {all-but} for elements 0 .. n-2
> % execute {last} for element n-1
> /STATIC 2 array def
> /forall-1 {
>     <<
>         0 { pop pop pop } % length == 0: do nothing
>         1 { exch pop forall } % length == 1: do {last}
>     >>
>     3 index length %dup =
>     2 copy known { get exec }{ pop pop
>         2 index 0
>         1 index length
>         1 sub getinterval %dup ==
>         3 1 roll
>         4 -1 roll
>         dup length 1 sub get %dup =
>         exch
>         //STATIC astore pop
>         forall
>         //STATIC aload pop
>         exec
>     } ifelse
>
> } def
>
> % m-array [ indices ]  mget  element(indices)
> /mget {
>     { get } forall
>
> } def
>
> % m-array [ indices ] value  mput  -
> /mput {
>     3 1 roll { get } { 3 -1 roll put } forall-1
>
> } def
>
> /A
> [ 5 {
>     [ 5 {
>         [ 5 { null } repeat ]
>     } repeat ]
>
> } repeat ] def
>
> A [ 4 4 4 ] 37 mput
> A [ 4 4 4 ] mget
> =
>
> currentfile flushfile
> !!gs %
> GPL Ghostscript 8.62 (2008-02-29)
> Copyright (C) 2008 Artifex Software, Inc.  All rights reserved.
> This software comes with NO WARRANTY: see the file PUBLIC for details.
> 37
> GS>GS>

Thank you very much for the very helpful answers.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.postscript


csiph-web