Merging two arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alektrishan
    New Member
    • Oct 2013
    • 7

    Merging two arrays

    I have an array that consists of the lines of a file and wish to insert lines at a specified point.

    The lines come from another file which is being processed (a line at a time) until a certain string is reached, at which point I'm finished with that file.

    Should I read the lines I'm keeping from file 2 into another array? How do I merge the two arrays?

    Thanks.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    To add/insert array elements at specified points, you'd use the splice function.
    See: perldoc -f splice

    To add elements to the end of an array, you'd use the push function.
    See: perldoc -f push

    To add elements to the beginning of the array, you'd use the unshift function.
    See: perldoc -f unshift
    Last edited by RonB; Oct 30 '13, 02:03 PM.

    Comment

    • alektrishan
      New Member
      • Oct 2013
      • 7

      #3
      Thanks. It looks like splice is the answer, but the reference is confusing. I'll google perl spice beginner and see what I get.

      Since splice works with two arrays, I think you're suggesting I read the second file into its own array, right?

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        You haven't provided enough info for me to recommend loading the second file into its own array or loop over it line-by-line.

        If you post your script, I'll see if I can make a recommendation based on what you're doing. I may also need to see the data files as well.

        Comment

        • alektrishan
          New Member
          • Oct 2013
          • 7

          #5
          Not sure what info you would need to make such a determination. The second file is anywhere from 300 to 400 lines long, but I'm processing it to discard everything after a certain string appears.

          I could write the result into another file or into an array.

          Then I want to insert that entire file/array after a line in the first file that matches a (different) string. The first file is about 2500 lines.

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            The files aren't big enough to worry about any performance difference between using splice or push.

            Loop over the 2nd file line-by-line and use splice to add each line to the array of the 1st file. When you reach the string indicating the end of the wanted data, then exit the loop.

            Another option would be to push the lines onto an array instead of the individual splice statements and then when the array is built, you'd use a single splice statement to add it to the first array.

            Comment

            • alektrishan
              New Member
              • Oct 2013
              • 7

              #7
              Does splice add extra newlines?

              I was able to get the lines I want into array2 and then used

              splice(@array1, $index, 0, @array2);

              where $index initially indicates the line where I want to start the insertion.

              Comment

              • RonB
                Recognized Expert Contributor
                • Jun 2009
                • 589

                #8
                Does splice add extra newlines?
                No, it doesn't. The new line character needs to be in each array element that you add.

                Comment

                • alektrishan
                  New Member
                  • Oct 2013
                  • 7

                  #9
                  Somehow, array1 has more blank lines in the vicinity of the spliced lines from array2 than array2 itself has. How do I prevent that? Can I send excerpts from the two arrays and the corresponding files?

                  (How did you quote my message?)

                  Comment

                  • RonB
                    Recognized Expert Contributor
                    • Jun 2009
                    • 589

                    #10
                    I can't say what's wrong or what you need to do to fix it without seeing your code and sample data.

                    To quote something, you simply wrap it in "quote" tags (without the spaces used here).
                    [ quote ] this is a quote [ /quote ]

                    BB Code List

                    Comment

                    • alektrishan
                      New Member
                      • Oct 2013
                      • 7

                      #11
                      Shall I just post it?

                      Comment

                      • RonB
                        Recognized Expert Contributor
                        • Jun 2009
                        • 589

                        #12
                        Only if you want me to help troubleshoot.

                        Comment

                        • alektrishan
                          New Member
                          • Oct 2013
                          • 7

                          #13
                          Thanks, anyway. I got some outside help. Appreciate your efforts.

                          Comment

                          Working...