Loading ...
Sorry, an error occurred while loading the content.

Re: Index number of array element

Expand Messages
  • Dan Gagne
    Eb, This is an excellent clip for discovering field count in menus and in delimited rows (tables). I use it or variations of it extensively for both purposes.
    Message 1 of 16 , Feb 3 8:51 AM
    • 0 Attachment
      Eb,

      This is an excellent clip for discovering field count in menus and in
      delimited rows (tables). I use it or variations of it extensively for
      both purposes.

      I've cleaned your code; hopefully making it more readable. Try
      replacing ones and zeros with true and false as logic parameters. They
      are more meaningful when you're debugging.

      I also removed the space characters that cohabitated with your
      one/zero logic parameters. Parameter fields don't tolerate additional
      characters.

      Quotes tend to make literal strings more visible in the string
      handling function fields (personal choice).
      ____________________________________
      ^!SetListDelimiter |
      ^!SetArray %Array%=Anthony|Bertha|Carla|David|Eb|Flo
      ^!Set %Name%=^?{(T= L)Select a name ==^%Array%}
      ; long line start
      ^!Set
      %Index%=^$StrCount("|";"^$StrCopyLeft(^%Array%;^$StrPos(^%Name%;^%Array%;true)$)$";false;true)$
      ; long line end
      ^!Info ^%Name% is number ^%Index%
      ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
      This code should work as intended.

      best regards,
      Dan Gagne

      --- In ntb-clips@yahoogroups.com, "Flo" <flo.gehrke@...> wrote:
      >
      > --- In ntb-clips@yahoogroups.com, "ebbtidalflats" <ebbtidalflats@>
      > wrote:
      > >
      > > Balnk lines to separate long lines:
      > >
      > > ;find the offset to the array element
      > >
      > > ^!Set %i%=^$StrPos("|";"|^%array%|";0)$
      > >
      > > ;count the delimiters in that much of the array
      > >
      > > ^!Set %i%=^$StrCount("|";"^$StrCopyLeft("|^%array%";^%i%)$")$
      >
      > Eb,
      >
      > That's another good idea (besides Lotta's solution) but,
      > unfortunately, it's producing a syntax error. I tried to make your
      > idea work. It may look something like this...
      >
      >
      > ^!SetListDelimiter |
      > ^!SetArray %Array%=Anthony|Bertha|Carla|David|Eb|Flo
      > ^!Set %Name%=^?{(T=L)Select a name ==^%Array%}
      > ; long line start
      > ^!Set %Index%=^$StrCount(|;^$StrCopyLeft(|^%Array%;^$StrPos(|^%
      > Name;|^%Array%;1)$)$;0;1)$
      > ; long line end
      > ^!Info ^%Name% is number ^%Index%
      >
      > Agreed?
      >
      > Flo
      >  
      >
    • Dan Gagne
      Eb, Forgot to mention that you have to bump the index count by one because the table delimiters follow the field they reference. Hence, the delimiter count is
      Message 2 of 16 , Feb 3 9:09 AM
      • 0 Attachment
        Eb,

        Forgot to mention that you have to bump the index count by one because
        the table delimiters follow the field they reference. Hence, the
        delimiter count is one less than the field you select.

        ^!SetListDelimiter |
        ^!SetArray %Array%=Anthony|Bertha|Carla|David|Eb|Flo
        ^!Set %Name%=^?{(T= L)Select a name ==^%Array%}
        ; long line start
        ^!Set
        %Index%=^$Calc(^$StrCount("|";"^$StrCopyLeft(^%Array%;^$StrPos(^%Name%;^%Array%;true)$)$";false;true)$+1)$
        ; long line end
        ^!Info ^%Name% is number ^%Index%

        sorry for the oversight.
        Dan Gagne

        --- In ntb-clips@yahoogroups.com, "Flo" <flo.gehrke@...> wrote:
        >
        > --- In ntb-clips@yahoogroups.com, "ebbtidalflats" <ebbtidalflats@>
        > wrote:
        > >
        > > Balnk lines to separate long lines:
        > >
        > > ;find the offset to the array element
        > >
        > > ^!Set %i%=^$StrPos("|";"|^%array%|";0)$
        > >
        > > ;count the delimiters in that much of the array
        > >
        > > ^!Set %i%=^$StrCount("|";"^$StrCopyLeft("|^%array%";^%i%)$")$
        >
        > Eb,
        >
        > That's another good idea (besides Lotta's solution) but,
        > unfortunately, it's producing a syntax error. I tried to make your
        > idea work. It may look something like this...
        >
        >
        > ^!SetListDelimiter |
        > ^!SetArray %Array%=Anthony|Bertha|Carla|David|Eb|Flo
        > ^!Set %Name%=^?{(T=L)Select a name ==^%Array%}
        > ; long line start
        > ^!Set %Index%=^$StrCount(|;^$StrCopyLeft(|^%Array%;^$StrPos(|^%
        > Name;|^%Array%;1)$)$;0;1)$
        > ; long line end
        > ^!Info ^%Name% is number ^%Index%
        >
        > Agreed?
        >
        > Flo
        >  
        >
      • ebbtidalflats
        Thanks for your comments, Dan. ... My code is BELOW the code you have edited. It prefixes the array with an extra delimiter to PREVENT the extra code to
        Message 3 of 16 , Feb 3 10:36 AM
        • 0 Attachment
          Thanks for your comments, Dan.

          My response is embedded in the quoted posts below:



          --- In ntb-clips@yahoogroups.com, "Dan Gagne" <dgagne@...> wrote:
          >
          > Eb,
          >
          > Forgot to mention that you have to bump the index count by one because
          > the table delimiters follow the field they reference. Hence, the
          > delimiter count is one less than the field you select.

          My code is BELOW the code you have edited. It prefixes the array with
          an extra delimiter to PREVENT the extra code to increment the index.


          > ^!Set
          >
          %Index%=^$Calc(^$StrCount("|";"^$StrCopyLeft(^%Array%;^$StrPos(^%Name%;^%Array%;true)$)$";false;true)$+1)$
          > ; long line end
          > ^!Info ^%Name% is number ^%Index%

          >
          > --- In ntb-clips@yahoogroups.com, "Flo" <flo.gehrke@> wrote:
          > >
          > > --- In ntb-clips@yahoogroups.com, "ebbtidalflats" <ebbtidalflats@>
          > > wrote:

          Note the leading delimiter here

          > > ^!Set %i%=^$StrPos("|";"|^%array%|";0)$

          and here (Eb's syntax error corrected) :

          > > ^!Set %i%=^$StrCount("|";"^$StrCopyLeft("|^%array%";^%i%)$";0;0)$


          This code includes the leading delimiter, and will return the correct
          index.


          > >
          > > Eb,

          The guts of Flo's array index, missing '%' and quotes added:

          ; - - - >8 - - -
          ^!Set
          %Index%=^$StrCount("|";^$StrCopyLeft("|^%Array%";^$StrPos("|^%target%";"|^%Array%";1)$)$;0;1)$
          ; - - - >8 - - -

          Note: the omitted trailing delimiter allows possible matches to larger
          elements, that begin with the target. This might be the WRONG index.

          Better would be (2 delimiters added in the StrPos function):

          ; - - - >8 - - -
          ^!Set
          %Index%=^$StrCount("|";^$StrCopyLeft("|^%Array%";^$StrPos("|^%target%|";"|^%Array%|";1)$)$;0;1)$
          ; - - - >8 - - -


          This is more difficult to decipher, than the two-line index code, but
          works equally well. Perhaps even microscopically faster.




          Regards,


          Eb
        • ebbtidalflats
          Hi Flo, My original array index code was missing two extra arguments to the StrCount function. That would generate a syntax error. Your code had a missing %
          Message 4 of 16 , Feb 3 10:53 AM
          • 0 Attachment
            Hi Flo,

            My original array index code was missing two extra arguments to the
            StrCount function. That would generate a syntax error.

            Your code had a missing '%' to the name variable, but there was a
            difference to my original code that might result in the WRONG index
            being returned.

            The initial StrPos function had both the target variable AND the array
            variable strings enclosed in delimiters. This prevents the clip from
            finding false matches, where the target starts off a longer array
            element. See 'Bert' below. There might be some applications where this
            is ok, but for exact matches, the surrounding delimiters are required.

            ^!SetArray %Array%=Anthony|Bertha|Bert|Carla|David|Eb|Flo
            ^!Set %Name%=Bert

            Search for "|Bert" finds "|Bertha"
            Search for "|Bert|" finds "|Bert|"


            The COUNT function does not need a trailing delimiter, since the
            CopyLeft function only copied to the targets leading delimiter.


            Count "|" in "|Anthony|Bertha|" returns 3



            Regards,


            Eb


            --- In ntb-clips@yahoogroups.com, "Flo" <flo.gehrke@...> wrote:
            >
            > --- In ntb-clips@yahoogroups.com, "ebbtidalflats" <ebbtidalflats@>
            > wrote:
            > >
            > > ^!Set %i%=^$StrCount("|";"^$StrCopyLeft("|^%array%";^%i%)$")$

            Should have been

            ^!Set %i%=^$StrCount("|";"^$StrCopyLeft("|^%array%";^%i%)$";0;0)$

            >
            >
            > ^!SetListDelimiter |
            > ^!SetArray %Array%=Anthony|Bertha|Carla|David|Eb|Flo
            > ^!Set %Name%=^?{(T=L)Select a name ==^%Array%}
            > ; long line start
            > ^!Set %Index%=^$StrCount(|;^$StrCopyLeft(|^%Array%;^$StrPos(|^%
            > Name;|^%Array%;1)$)$;0;1)$
            > ; long line end
            > ^!Info ^%Name% is number ^%Index%
            >
            > Agreed?
          • Flo
            ... Hi Eb, ... Thanks for these corrections, Eb! It s clear to see why the trailing delimiter is needed. Without that delimiter, Bert , in
            Message 5 of 16 , Feb 3 2:28 PM
            • 0 Attachment
              --- In ntb-clips@yahoogroups.com, "ebbtidalflats" <ebbtidalflats@...>
              wrote:
              >
              > Hi Flo,
              >
              > My original array index code was missing two extra arguments to the
              > StrCount function. That would generate a syntax error...

              Hi Eb,

              > Your code had a missing '%' to the name variable,...
              > The initial StrPos function had both the target variable AND
              > the array variable strings enclosed in delimiters. This
              > prevents the clip from finding false matches, where the target
              > starts off a longer array element. See 'Bert' below.

              Thanks for these corrections, Eb! It's clear to see why the trailing
              delimiter is needed. Without that delimiter, "Bert",
              in "Anthony|Bertha|Bert|Carla|David|Eb|Flo", is counted as #2. The
              solution is just another "|" in line #4 after ^%Name%.

              On this condition, however, we need another delimiter at the end of
              the array. Without this delimiter, "Flo" would be counted as "0".
              (Normally, a delimiter without a following value could cause some
              trouble but, in this case, it doesn't matter.)

              In my tests, this works fine...

              ^!SetListDelimiter |
              ^!SetArray %Array%=Anthony|Bertha|Bert|Carla|David|Eb|Flo|
              ^!Set %Name%=^?{(T=L)Select a name==^%Array%}
              ; long line start
              ^!Set %Index%=^$StrCount(|;^$StrCopyLeft(|^%Array%;^$StrPos(|^%Name%
              |;|^%Array%;1)$)$;0;1)$
              ; long line end
              ^!Info ^%Name% is number ^%Index%


              Flo
               
            • ebbtidalflats
              Hi Flo, Yes, you need an extra delimiter. However, I would NOT place it at the end of the array. That would require you to structure the array with this
              Message 6 of 16 , Feb 5 9:14 AM
              • 0 Attachment
                Hi Flo,

                Yes, you need an extra delimiter. However, I would NOT place it at the
                end of the array. That would require you to structure the array with
                this particular code in mind, rather than make to index code work with
                ANY array.

                If you place the extra delimiter at the end of the array within the
                string position test, it will always be there, whether the array has a
                trailing delimiter or not. Extracting the StrPos function from your
                codeline (and adding quotes):

                ^$StrPos("|^%name%|";"|^%Array%|";1)$

                would parse as either (without trailing delimiter in array:

                ^$StrPos("|Flo|";"|Anthony|Bertha|Bert|Carla|David|Eb|Flo|";1)$

                or WITH trailing delimiter in the array:

                ^$StrPos("|Flo|";"|Anthony|Bertha|Bert|Carla|David|Eb|Flo||";1)$

                Then, when you count delimiters, you can drop the trailing delimiters:

                ; the whole long line start
                ^!Set
                %Index%=^$StrCount("|";"^$StrCopyLeft("|^%Array%";^$StrPos("|^%Name%|";"|^%Array%|";1)$)$";0;1)$
                ; long line end



                Cheers,


                Eb

                --- In ntb-clips@yahoogroups.com, "Flo" <flo.gehrke@...> wrote:
                >
                > Thanks for these corrections, Eb! It's clear to see why the trailing
                > delimiter is needed. Without that delimiter, "Bert",
                > in "Anthony|Bertha|Bert|Carla|David|Eb|Flo", is counted as #2. The
                > solution is just another "|" in line #4 after ^%Name%.
                >
                > On this condition, however, we need another delimiter at the end of
                > the array. Without this delimiter, "Flo" would be counted as "0".
                > (Normally, a delimiter without a following value could cause some
                > trouble but, in this case, it doesn't matter.)
                >
                > In my tests, this works fine...
                >
                > ^!SetListDelimiter |
                > ^!SetArray %Array%=Anthony|Bertha|Bert|Carla|David|Eb|Flo|
                > ^!Set %Name%=^?{(T=L)Select a name==^%Array%}
                > ; long line start
                > ^!Set %Index%=^$StrCount(|;^$StrCopyLeft(|^%Array%;^$StrPos(|^%Name%
                > |;|^%Array%;1)$)$;0;1)$
                > ; long line end
                > ^!Info ^%Name% is number ^%Index%
                >
              Your message has been successfully submitted and would be delivered to recipients shortly.