Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Return position of first character of a particular word

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Return position of first character of a particular word

    I would like to search the contents of a variable for a particular word and return the character position of its first letter. I've had a look in the help file but couldn't find a ready made function. Before I embark on creating my own I wanted to check that there isn't a standard function that I have failed to find.

    The background for this is that I would like to replace whole sections of text in a number of text files and replace them with new text.

    So if there is a standard function to help with this can someone point me in the right direction.

    Thanks

    Geoff

    #2
    Re: Return position of first character of a particular word

    AT2() might be the answer.

    Comment


      #3
      Re: Return position of first character of a particular word

      Originally posted by gmeredith17 View Post
      The background for this is that I would like to replace whole sections of text in a number of text files and replace them with new text.
      Many different ways. It all depends. Is the text in question a certain word? a certain paragraph? some characters in a word?

      Comment


        #4
        Re: Return position of first character of a particular word

        I think AT2() will do it. I'm replacing one or more paragraphs. Each paragraph has a unique identifier for the start and finish.

        I imagine the structure of the UDF being something like this;

        Load text from file in variable vtext using .to_string()
        Get total number of chrs using len() store in variable vtc.
        Search for first word (unique identifier) and get position using at2() and store in variable pstart.
        From this position search for end identifier using at2() and store in variable pend.
        Have two more variables, vtext_start and vtext_end.
        vtext_start will have the contents of vtext from beginning to the pstart-1.
        vtext_end will have the text contents of vtext from pend+1 to vtc.

        Then the new text file content will be
        New_text_file=vtext_start+vnew_text+vtext_end

        Where vnew_text will be a variable containing the replacement text.

        Finally using file.from_string() create a new text file.

        I'm sure there are better or more efficient ways but this is my initial idea. Once I have got it working for one text file I will need to expand the function to perform it on a batch of files.

        Comment


          #5
          Re: Return position of first character of a particular word

          Geoff

          Sounds like you are going down a path that will lead you to string.scanner
          Al Buchholz
          Bookwood Systems, LTD
          Weekly QReportBuilder Webinars Thursday 1 pm CST

          Occam's Razor - KISS
          Normalize till it hurts - De-normalize till it works.
          Advice offered and questions asked in the spirit of learning how to fish is better than someone giving you a fish.
          When we triage a problem it is much easier to read sample systems than to read a mind.
          "Make it as simple as possible, but not simpler."
          Albert Einstein

          http://www.iadn.com/images/media/iadn_member.png

          Comment


            #6
            Re: Return position of first character of a particular word

            Thanks Al. I will take some time to digest the options. I kind of skipped over this function in the help file index. Seeing the term scanner I must have subconsciously thought it was for use with scanning hardware. At first glance it likes like it has some powerful functionality.

            Comment


              #7
              Re: Return position of first character of a particular word

              Hi Al,

              Originally posted by Al Buchholz View Post
              Sounds like you are going down a path that will lead you to string.scanner
              Bad choice IMHO. This is a class with methods (functions) that, as far as I know, Alpha never uses internally. It is not very fast in almost all circumstances compared to alternatives, and I suspect it was relegated to the past after Alpha installed RegEx functions.

              If not using Regex functions (the best here), then I probably would use wordat() or wordatc() to find the word #, then compute the length using word() to get the (1st words + the delimiter) prior to that

              for a regex function use something like this
              textstring="This is a test string that embedtestembed has a test string more than once"
              ' This finds any string
              ?regex_grep(textstring,regex_escape("test"),"$(OFFSET)"+crlf(),"I")
              = 11
              33
              49
              ' This finds only strings separated by a regex defined word bondrary
              ?regex_grep(textstring,"\b"+regex_escape("test")+"\b","$(OFFSET)"+crlf(),"I")
              = 11
              49
              ' This takes the 1st word in the list of found values and returns the offset as a number
              ?val(word(regex_grep(textstring,"\b"+regex_escape("test")+"\b","$(OFFSET)"+crlf(),"I"),1,crlf()))
              = 11
              Regards,

              Ira J. Perlow
              Computer Systems Design


              CSDA A5 Products
              New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
              CSDA Barcode Functions

              CSDA Code Utility
              CSDA Screen Capture


              Comment


                #8
                Re: Return position of first character of a particular word

                Ira - thanks for the alternative.

                It would seem Alpha added the string.scanner function after regex looking at the supported version from the help file. V6 for regex_grep and V7 for sring.scanner.

                Would they not have used the regex within the string.scanner functions?

                They state this in the help file.

                A comparison with OCCURSI() showed that a WHILE...END WHILE loop using <StringScanner>.SkipToString()(similar to the above script) was approximately 60 times faster.
                It seems they did something to improve the speed. Do you know the background to the functions and what they did get the improvements?

                Comment


                  #9
                  Re: Return position of first character of a particular word

                  Hi Geoff,

                  Originally posted by gmeredith17 View Post
                  It would seem Alpha added the string.scanner function after regex looking at the supported version from the help file. V6 for regex_grep and V7 for sring.scanner.

                  Would they not have used the regex within the string.scanner functions?
                  When they referenced
                  A comparison with OCCURSI() showed that a WHILE...END WHILE loop

                  All that means is that a single line of code is faster than a loop, which is almost always a truism. Whether it is as fast as other methods that are essentially 1 line of low-level ("C" code types) functions, I'd kind of doubt it.

                  Stringscanner and regex functions are both available in V6, although there is an indication that regex was present in A5v5, but not exposed at the xbasic level.

                  I don't have any idea if they used regex in stringscanner, but it's probably code that was different than regex, as they have different structures.

                  It's been over a year or two since I last ran checks of stringscanner speeds, but if it were really useful, I'd suspect Alpha would have used it internally, which I don't see any indication that they do.

                  However, feel free to test it's speed. I think you will find it slower than regex, or any other low-level functions option.
                  Regards,

                  Ira J. Perlow
                  Computer Systems Design


                  CSDA A5 Products
                  New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
                  CSDA Barcode Functions

                  CSDA Code Utility
                  CSDA Screen Capture


                  Comment


                    #10
                    Re: Return position of first character of a particular word

                    Originally posted by gmeredith17 View Post
                    Each paragraph has a unique identifier for the start and finish.
                    It is possible that you do not need either.
                    Can you provide a sample?

                    Comment


                      #11
                      Re: Return position of first character of a particular word

                      G,

                      I want to edit several html pages. For example, change all the footers or menu structure in each page. I think any of the suggestions that have been made will be fine and the speed is probably a moot point when dealing with relatively small text files. I suspect disc access will be the major overhead.

                      Ira, thank you for the extra information. I hadn't come across the regex functions so that looks like an area I will have to put some time into to.

                      Geoff

                      Comment


                        #12
                        Re: Return position of first character of a particular word

                        Try looking up word() in help - Say you wanted the P in Pearson in a variable containing Mark Pearson. I would use

                        Left(word(variable,2," "),1)

                        What you need to do in your case is find the word number
                        -----------------------------------------------
                        Regards
                        Mark Pearson
                        [email protected]
                        Youtube channel
                        Website

                        Comment


                          #13
                          Re: Return position of first character of a particular word

                          FYI: Just some related info for anyone reading this thread.

                          Note that these two functions:

                          at() - case sensitive
                          atc() - not case sensitive

                          are similar to at2() except for the third argument. The at2() function takes a third argument which is a starting point while the third argument in the at() and atc() functions is the "occurrence number". So
                          atc( "the", "to the other of them", 2 )
                          would return 9 because the second "the" is found in "other".

                          If you really wanted to find just a specific word - which might be at the very beginning or at the very end - you can add some spaces to both the string you are searching for and the string you are searching through like this:
                          test_str = "The list will be sent to the other of them."
                          atc( " the ", " " + test_str + " ", 2 )

                          NOTE: The wordat() and wordatc() functions only find the first matching word. They cannot be used to find the nth occurrence of a word. Also, they return the word number - not the position number. If parsing a string, you will probably have to convert the word number to a position number - which is why I just use atc().

                          Edit: Note also that the at2() function is case sensitive and there doesn't appear to be a case insensitive alternative. This is the main reason I seldom use this function.
                          Last edited by CALocklin; 02-15-2010, 11:23 AM.

                          Comment


                            #14
                            Re: Return position of first character of a particular word

                            Hi Cal,

                            Originally posted by CALocklin View Post
                            Edit: Note also that the at2() function is case sensitive and there doesn't appear to be a case insensitive alternative. This is the main reason I seldom use this function.
                            For something that is not returning a string derived from the parameter inputs, in this case position of the string, you can make it case insensitive simply by uppercasing all strings, as in
                            AT2(upper(searchstring),upper(stringtosearch),startingposition)
                            Regards,

                            Ira J. Perlow
                            Computer Systems Design


                            CSDA A5 Products
                            New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
                            CSDA Barcode Functions

                            CSDA Code Utility
                            CSDA Screen Capture


                            Comment

                            Working...
                            X