Array.IndexOf - not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Al

    Array.IndexOf - not working


    This statement returns a -1, indicating "not found":
    Find1 = Array.IndexOf(F ilesArray, "sa001")

    But IndexOf on a specific item in that array returns a value of 26:
    Find1 = FilesArray(2).I ndexOf("sa001")
    This shows the value "sa001" is found in FilesArray.

    Any suggestions why Array.IndexOf does not return a number (index of
    item that has the value)?

    Thanks,
    Al

  • Cor Ligthert

    #2
    Re: Array.IndexOf - not working

    Al,

    Are you sure of that, this gives for me 1
    \\\
    Dim filesarray() As String = {"sa000", "sa001", "sa002"}
    Dim Find1 As Integer = Array.IndexOf(f ilesarray, "sa001")
    ///

    I hope this helps,

    Cor


    Comment

    • Kaoru Kodaka

      #3
      Re: Array.IndexOf - not working

      Dear Al,

      Just a confirmation. Is the array "FilesArray " multi dimension?
      It seems so.

      The document said "Returns the index of the first occurrence
      of a value in a *one-dimensional* Array or in a portion of
      the Array. "



      On 1 Jun 2005 19:42:46 -0700
      "Al" <al_baker2005@h otmail.com> wrote:
      [color=blue]
      >
      > This statement returns a -1, indicating "not found":
      > Find1 = Array.IndexOf(F ilesArray, "sa001")
      >
      > But IndexOf on a specific item in that array returns a value of 26:
      > Find1 = FilesArray(2).I ndexOf("sa001")
      > This shows the value "sa001" is found in FilesArray.
      >
      > Any suggestions why Array.IndexOf does not return a number (index of
      > item that has the value)?
      >
      > Thanks,
      > Al[/color]


      ---
      MVP kaok = MVP.ChangeMvpCa tegory("for C# 2004-2005.");
      kaok.Web = "http://www.antoine.st/";

      Comment

      • Al

        #4
        Re: Array.IndexOf - not working

        Thanks for your replies.

        The array is one dimension:
        Public FilesArray() As String

        The application needs to search for words in file names.

        I do this:
        FilesArray = Directory.GetFi les(sFolder)
        All items are unique, since they are file names with path (e.g.
        "C:\imageProjec t\sa002.tif")

        I then use a For-Next loop looking at each item in the array with
        IndexOf:
        Find1 = FilesArray(Fi). IndexOf(word)
        This works OK but with larger array, I thought Array.IndexOf would be
        faster.

        Cor, the simple example you provided is looking at the word at the
        beginning of the string. I had wondered if that was needed for
        Array.IndexOf. But I did a test, searching for "C:\", but it still
        returned with -1.

        It just appears that Array.IndexOf is not working for some reason on
        FilesArray. I'll test with the simple example; perhaps it will turn a
        light on.

        I have Windows XP home edition.

        Considering what the app needs, are there better ways to do this?

        Al

        Comment

        • _AnonCoward

          #5
          Re: Array.IndexOf - not working


          "Al" <al_baker2005@h otmail.com> wrote in message
          news:1117680166 .419793.318480@ o13g2000cwo.goo glegroups.com.. .
          :
          : This statement returns a -1, indicating "not found":
          : Find1 = Array.IndexOf(F ilesArray, "sa001")
          :
          : But IndexOf on a specific item in that array returns a value of 26:
          : Find1 = FilesArray(2).I ndexOf("sa001")
          : This shows the value "sa001" is found in FilesArray.
          :
          : Any suggestions why Array.IndexOf does not return a number (index of
          : item that has the value)?
          :
          : Thanks,
          : Al


          I compiled the following test. Note that s2 contains elements that
          include "sa00#" as part of the value in each element, not the full
          value:

          =============== =============== =============== ======
          imports system
          public class [class]

          public shared sub main
          dim s1 as string() = {"sa001", "sa002", "sa003"}
          dim s2 as string() = {"asa001a", "bsa002b", "csa003c"}

          console.writeli ne(Array.IndexO f(s1, "sa001"))
          console.writeli ne(Array.IndexO f(s2, "sa001"))

          end sub
          end class
          =============== =============== =============== ====


          This generated the following response:

          =============== =============== =============== ====
          0
          -1
          =============== =============== =============== ====


          The 1st writeline statement displayed 0 because there is a string in
          array s1 that matches the exact value "sa001". However, the 2nd line
          displayed -1 because no element in array s2 exactly matches that test
          string. The Array.IndexOf() function will only find the test string if
          the entire string equals that value, not just part of the string.


          Try something along these lines instead

          =============== =============== =============== ====
          imports system
          public class [class]

          public shared sub main
          dim s1 as string() = {"sa001", "sa002", "sa003"}
          dim s2 as string() = {"asa001a", "bsa002b", "csa003c"}

          console.writeli ne(Array.IndexO f(s1, "sa001"))

          '************** *************** **************
          dim ndx As Integer
          for ndx = 0 to s1.getUpperBoun d(0)
          If s1(ndx).indexOf ("sa001") > -1 Then
          exit for
          End If
          next

          if ndx <= s1.getupperboun d(0) then
          console.writeli ne(ndx)
          else
          console.writeli ne("-1")
          end if
          '************** *************** **************

          end sub
          end class
          =============== =============== =============== ====


          (Note: this will only tell you which element in array s2 contains the
          string "sa001", not where it is in that specific string)


          HTH

          Ralf


          Comment

          • Al

            #6
            Re: Array.IndexOf - not working

            Thanks Ralf,

            I was expecting Array.IndexOf to work the same way as String.IndexOf.

            The .NET Help provides this information:

            String.IndexOf Method:
            Reports the index of the first occurrence of a String, or one or more
            characters, within this instance.

            Array.IndexOf Method:
            Returns the index of the first occurrence of a value in a
            one-dimensional Array or in a portion of the Array.

            These sound very similar.
            IMO there should be an inexpensive note in Array.IndexOf Method:
            "The search value must match the whole array item, and is therefore
            different than String.IndexOf. "

            Al

            Comment

            • Al

              #7
              Re: Array.IndexOf - not working

              This is how I changed the app, utilizing Array.IndexOf.

              One time steps:
              1. Create the files array (as was done before).
              FilesArray = Directory.GetFi les(FolderName)
              Each item is a file name with path (e.g."C:\ImageP roject\sa001.ti f")

              2. Create a mirror array FilesArray2 (additional step, but one-time).
              Using string.LastInde xOf in a For/Next loop, extract the file name
              (e.g. "sa001") from each FilesArray item. Both arrays have same index
              numbers.

              When user selects an item (e.g. clicks "Next" button):
              3a. Data file (which may be sorted different ways) provides the file
              name ("e.g. "sa001").
              3b. Use Array.IndexOf on FilesArray2 to find that item.
              3c. Use that index on FilesArray to get the file path.
              3d. Display image file.

              When array is 1000 or more, this should provide faster and more
              consistent performance than a For/Next loop. User may click "Next"
              button fast.

              Al

              Comment

              • Alejandro Lapeyre

                #8
                Re: Array.IndexOf - not working

                "Al" <al_baker2005@h otmail.com> escribió en el mensaje
                news:1117841916 .563041.140470@ g47g2000cwa.goo glegroups.com.. .[color=blue]
                > This is how I changed the app, utilizing Array.IndexOf.
                >
                > One time steps:
                > 1. Create the files array (as was done before).
                > FilesArray = Directory.GetFi les(FolderName)
                > Each item is a file name with path (e.g."C:\ImageP roject\sa001.ti f")
                >
                > 2. Create a mirror array FilesArray2 (additional step, but one-time).
                > Using string.LastInde xOf in a For/Next loop, extract the file name
                > (e.g. "sa001") from each FilesArray item. Both arrays have same index
                > numbers.
                >
                > When user selects an item (e.g. clicks "Next" button):
                > 3a. Data file (which may be sorted different ways) provides the file
                > name ("e.g. "sa001").
                > 3b. Use Array.IndexOf on FilesArray2 to find that item.
                > 3c. Use that index on FilesArray to get the file path.
                > 3d. Display image file.
                >
                > When array is 1000 or more, this should provide faster and more
                > consistent performance than a For/Next loop. User may click "Next"
                > button fast.[/color]

                Array.IndexOf is doing the For/Next loop for you, so this paragraph does not
                apply.
                [color=blue]
                > Al
                >[/color]

                Best Regards
                Alejandro Lapeyre


                Comment

                • Al

                  #9
                  Re: Array.IndexOf - not working


                  Array.IndexOf does something for the programmer, making it much easier
                  than a For/Next loop.

                  The important thing I learned here is the array item must match the
                  whole search value, unlike String.IndexOf.
                  In this app, if the array is large enough, I think the performance is
                  better when the user is clicking from item to item, and it takes just a
                  few seconds to create the second array up front.

                  Please explain what paragraph does not apply.
                  Al

                  Comment

                  • Alejandro Lapeyre

                    #10
                    Re: Array.IndexOf - not working


                    The paragraph where you said that using Array.IndexOf is faster than using a
                    For/Next loop

                    Array.IndexOf uses a For/Next loop to find the item.

                    Best Regards
                    Alejandro Lapeyre

                    "Al" <al_baker2005@h otmail.com> escribió en el mensaje
                    news:1117882309 .073175.163170@ g49g2000cwa.goo glegroups.com.. .[color=blue]
                    >
                    > Array.IndexOf does something for the programmer, making it much easier
                    > than a For/Next loop.
                    >
                    > The important thing I learned here is the array item must match the
                    > whole search value, unlike String.IndexOf.
                    > In this app, if the array is large enough, I think the performance is
                    > better when the user is clicking from item to item, and it takes just a
                    > few seconds to create the second array up front.
                    >
                    > Please explain what paragraph does not apply.
                    > Al
                    >[/color]


                    Comment

                    • Al

                      #11
                      Re: Array.IndexOf - not working

                      Sorry for the delay. I did some performance testing, learning about
                      timespan and formatting the results.
                      If the array is very large, the "two array" method I described is
                      faster.

                      But the difference is subsecond even if the array has one million
                      items:
                      Two array method, selecting "1000000" (one million) did it in 46 to 109
                      milliseconds.
                      One array method did it in 531 to 875 milliseconds.

                      Selecting "10000" (ten thousand) was "zero" milliseconds in both
                      methods.
                      In my app, the array size is expected to be smaller than 10,000. So
                      there is no advantage for this app to use Array.IndexOf.

                      I discovered some interesting things about timespan - at least on my
                      computer.
                      The system reports zero if the number of ticks is less than 156250 (15
                      milliseconds).
                      To test the result formatting, I needed to artificially increase the
                      timespan above 15 milliseconds.
                      I assume there is a good reason not to report the ACTUAL number of
                      ticks.

                      The system reports timespan in increments of 156250 ticks.

                      Al

                      Comment

                      • José Manuel Agüero

                        #12
                        Re: Array.IndexOf - not working

                        Hello Al,

                        Don't you think it would be easier to use the Visual Basic function Filter?:


                        Regards.


                        "Al" <al_baker2005@h otmail.com> escribió en el mensaje news:1117841916 .563041.140470@ g47g2000cwa.goo glegroups.com.. .
                        | This is how I changed the app, utilizing Array.IndexOf.
                        |
                        | One time steps:
                        | 1. Create the files array (as was done before).
                        | FilesArray = Directory.GetFi les(FolderName)
                        | Each item is a file name with path (e.g."C:\ImageP roject\sa001.ti f")
                        |
                        | 2. Create a mirror array FilesArray2 (additional step, but one-time).
                        | Using string.LastInde xOf in a For/Next loop, extract the file name
                        | (e.g. "sa001") from each FilesArray item. Both arrays have same index
                        | numbers.
                        |
                        | When user selects an item (e.g. clicks "Next" button):
                        | 3a. Data file (which may be sorted different ways) provides the file
                        | name ("e.g. "sa001").
                        | 3b. Use Array.IndexOf on FilesArray2 to find that item.
                        | 3c. Use that index on FilesArray to get the file path.
                        | 3d. Display image file.
                        |
                        | When array is 1000 or more, this should provide faster and more
                        | consistent performance than a For/Next loop. User may click "Next"
                        | button fast.
                        |
                        | Al
                        |

                        Comment

                        Working...