March 17, 2008
hi,

i try to have a hash and initialize it. it doesn't work. can anybody please tell me how to initialize the below with many values such as


      key                      struct values
"(0002,0000)"     "UL","N","Group Length",
"(0002,0001)"     "OB","N","File Meta Information Version",

                       ....


struct HASHTAG {
  //string Tag;
  string VR;
  string RET;
  string NAME;
}

HASHTAG[string] tags = [
  "(0002,0000)": {"UL","N","Group Length"},
]

this does not work. many thanks in advance.

March 17, 2008
lurker, this works:

import std.stdio: writefln;

struct Hashtag {
    string vr;
    string ret;
    string name;
}

void main() {
    Hashtag[string] tags = ["(0002,0000)": Hashtag("UL","N","Group Length")];
    writefln(tags["(0002,0000)"].name);
}

You may want to define tags inside a static this {}.

Bye,
bearophile
March 17, 2008
lurker wrote:
> hi,
> 
> i try to have a hash and initialize it. it doesn't work. can anybody please tell me how to initialize the below with many values such as 
> 
> 
>       key                      struct values
> "(0002,0000)"     "UL","N","Group Length",
> "(0002,0001)"     "OB","N","File Meta Information Version",
> 
>                        ....
> 
> 
> struct HASHTAG {
>   //string Tag;
>   string VR;
>   string RET;
>   string NAME;
> }
> 
> HASHTAG[string] tags = [
>   "(0002,0000)": {"UL","N","Group Length"},
> ]
> 
> this does not work. many thanks in advance.

A variation on a common theme here: AA literals aren't constants and can therefore not be used as initializers for global variables.
Also, struct literals not directly used as initializers can't use the '{ <members> }' syntax.

Try this instead:
---
HASHTAG[string] tags;
static this() {
    tags = ["(0002,0000)": HASHTAG("UL","N","Group Length")];
}
---
March 17, 2008
thank you so very much you two

March 23, 2008
ok all that worked. how do i  find for ex. "(0002,0000)" and get HASHTAG back??

thanks for any help

Frits van Bommel Wrote:

> lurker wrote:
> > hi,
> > 
> > i try to have a hash and initialize it. it doesn't work. can anybody please tell me how to initialize the below with many values such as
> > 
> > 
> >       key                      struct values
> > "(0002,0000)"     "UL","N","Group Length",
> > "(0002,0001)"     "OB","N","File Meta Information Version",
> > 
> >                        ....
> > 
> > 
> > struct HASHTAG {
> >   //string Tag;
> >   string VR;
> >   string RET;
> >   string NAME;
> > }
> > 
> > HASHTAG[string] tags = [
> >   "(0002,0000)": {"UL","N","Group Length"},
> > ]
> > 
> > this does not work. many thanks in advance.
> 
> A variation on a common theme here: AA literals aren't constants and can
> therefore not be used as initializers for global variables.
> Also, struct literals not directly used as initializers can't use the '{
> <members> }' syntax.
> 
> Try this instead:
> ---
> HASHTAG[string] tags;
> static this() {
>      tags = ["(0002,0000)": HASHTAG("UL","N","Group Length")];
> }
> ---

March 24, 2008
when i do as in the docs


HASHTAG  *tt = ("(0002,0000)" in Tags);

it will always return null.


lurker Wrote:

> ok all that worked. how do i  find for ex. "(0002,0000)" and get HASHTAG back??
> 
> thanks for any help
> 
> Frits van Bommel Wrote:
> 
> > lurker wrote:
> > > hi,
> > > 
> > > i try to have a hash and initialize it. it doesn't work. can anybody please tell me how to initialize the below with many values such as
> > > 
> > > 
> > >       key                      struct values
> > > "(0002,0000)"     "UL","N","Group Length",
> > > "(0002,0001)"     "OB","N","File Meta Information Version",
> > > 
> > >                        ....
> > > 
> > > 
> > > struct HASHTAG {
> > >   //string Tag;
> > >   string VR;
> > >   string RET;
> > >   string NAME;
> > > }
> > > 
> > > HASHTAG[string] tags = [
> > >   "(0002,0000)": {"UL","N","Group Length"},
> > > ]
> > > 
> > > this does not work. many thanks in advance.
> > 
> > A variation on a common theme here: AA literals aren't constants and can
> > therefore not be used as initializers for global variables.
> > Also, struct literals not directly used as initializers can't use the '{
> > <members> }' syntax.
> > 
> > Try this instead:
> > ---
> > HASHTAG[string] tags;
> > static this() {
> >      tags = ["(0002,0000)": HASHTAG("UL","N","Group Length")];
> > }
> > ---
> 

March 24, 2008
below is the program fragment. i am using dmd 2012.
does anybody have a clue on how to search for a key and return the HASHTAG  value?

struct HASHTAG {
  string VR;
  string RET;
  string NAME;
}

class Tag {
  private:
  HASHTAG[string] Tags;

  public:
  this() {
    Tags = [
    "(0002,0000)": HASHTAG("UL","N","Group Length"),
    "(0002,0001)": HASHTAG("OB","N","File Meta Information Version"),
    "(0002,0002)": HASHTAG("UI","N","Media Storage SOP Class UID")
     ];
   }

  bool Search(string key, ref TData pData) {
	  string RET,VR;
	  int nVR; HASHTAG *tt;

// the below works, it displays values correctly

    HASHTAG xx[] = Tags.values;
    for(int i= 0; i <Tags.length; i++) {
      writefln("%s %s", xx[i].VR, xx[i].NAME);
    }

 // ----> will always be null, not finding the HASHTAG
    tt = ("(0002,0000)" in Tags);
    if (tt == null)
      return false;
  }
}

displaying the keys of the hash shows, that "(0002,0000)" is contained in the hash.





lurker Wrote:

> when i do as in the docs
> 
> 
> HASHTAG  *tt = ("(0002,0000)" in Tags);
> 
> it will always return null.
> 
> 
> lurker Wrote:
> 
> > ok all that worked. how do i  find for ex. "(0002,0000)" and get HASHTAG back??
> > 
> > thanks for any help
> > 
> > Frits van Bommel Wrote:
> > 
> > > lurker wrote:
> > > > hi,
> > > > 
> > > > i try to have a hash and initialize it. it doesn't work. can anybody please tell me how to initialize the below with many values such as
> > > > 
> > > > 
> > > >       key                      struct values
> > > > "(0002,0000)"     "UL","N","Group Length",
> > > > "(0002,0001)"     "OB","N","File Meta Information Version",
> > > > 
> > > >                        ....
> > > > 
> > > > 
> > > > struct HASHTAG {
> > > >   //string Tag;
> > > >   string VR;
> > > >   string RET;
> > > >   string NAME;
> > > > }
> > > > 
> > > > HASHTAG[string] tags = [
> > > >   "(0002,0000)": {"UL","N","Group Length"},
> > > > ]
> > > > 
> > > > this does not work. many thanks in advance.
> > > 
> > > A variation on a common theme here: AA literals aren't constants and can
> > > therefore not be used as initializers for global variables.
> > > Also, struct literals not directly used as initializers can't use the '{
> > > <members> }' syntax.
> > > 
> > > Try this instead:
> > > ---
> > > HASHTAG[string] tags;
> > > static this() {
> > >      tags = ["(0002,0000)": HASHTAG("UL","N","Group Length")];
> > > }
> > > ---
> > 
> 

March 24, 2008
lurker wrote:
> below is the program fragment. i am using dmd 2012.
> does anybody have a clue on how to search for a key and return the HASHTAG  value?
> 
[snip]
>   this() {
>     Tags = [
>     "(0002,0000)": HASHTAG("UL","N","Group Length"),
>     "(0002,0001)": HASHTAG("OB","N","File Meta Information Version"),
>     "(0002,0002)": HASHTAG("UI","N","Media Storage SOP Class UID")
>      ];
>    }
> 
>   bool Search(string key, ref TData pData) {
[snip]
>  // ----> will always be null, not finding the HASHTAG 
>     tt = ("(0002,0000)" in Tags);
>     if (tt == null)        
>       return false;
>   }
> }
> 
> displaying the keys of the hash shows, that "(0002,0000)" is contained in the hash.

Weird... That works fine on 1.028, but indeed seems to fail on 2.012.
You may want to file a bug report at <http://d.puremagic.com/issues/> if there isn't one for this already.
Top | Discussion index | About this forum | D home