2

So I have a text file with lines of information, with several data pieces on each line that I need to store in an array of arrays.

This is simple enough when they are split on whitespace or commas, but with this particular file, some of the data has white space within a single data field.

Eg.

123 4325 Hello World 43

394 3892 How are you 23

Anybody know a way to split each line into 4 strings each, with 'Hello World' and 'How are you' remaining together?

Ie: Array[0] = [123, 4325, Hello World, 43] and Array[1] = [394, 3892, How are you, 23]

Sorry I'm fairly new to JS so I'm not sure if there's a reall simple answer staring me in the face.

Thanks

3
  • So you want to split a string after x-amount of words without it cutting into words? If so, I have asked a question just like this here: stackoverflow.com/questions/26507116/… Commented Apr 22, 2015 at 6:08
  • If this is a clearcut case of single field that has multiple spaces then we can write a custom function. Commented Apr 22, 2015 at 6:21
  • F1 F2 ANYTHING * GOES F3 - is this the structure? Commented Apr 22, 2015 at 6:22

3 Answers 3

2

Follow a similar workflow.

  1. Pick a line, split by space.

e.g. "345 578 This is a text 585" -> ["345","578","This", "is", "a", "text","585"]

  1. Take the first two elements into an array.

e.g. ["345","578","This", "is", "a", "text","585"] -> ["345","578"] ["This", "is", "a", "text","585"]

  1. Remove the last element into a separate arr.

e.g. ["345","578"] ["This", "is", "a", "text"] ["585"]

  1. Merge the middle array with space.

e.g. ["345","578"] ["This is a text"] ["585"]

  1. Merge all arrays.

e.g. ["345","578", "This is a text", "585"]

tada!

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the JavaScript API called FileReader. You can search for it in Google. It can read a file using only JS. You can then use

var lines = content.split("\n");

To separate the content by a new line.

2 Comments

First, he needs a JS answer. Second, this does not solve the problem, have a look at the question again.
This is what I use to initially split the file by line, but it doesn't solve the issue of splitting the contents of each line by field.
0

While it is not complex, what you want also does not come out of the javascript box. Which means that there are hundreds of ways to resolve this. I recommend you choose depending on "soft" requirements.

How fast does the code need to be? How easy should it be to understand the code? Do you also need to make sure about the format of each value (ie that the first value is a number and the third is a string)?

If you need to check for the formatting (which I find is most of the time), I would recommend using regular expressions. There are plenty of examples and it would be a different question for SO.

As a solution to the specific problem you have mentioned here, here is the solution I prefer:

function breakByPosition(str, positions) {
    var start = 0;
    var result = [];
    while (positions.length) {
        // splice returns the removed values as an array
        // It also modified the source array in place
        var position = positions.splice(0, 1);
        var substring = str.substring(start, position);
        // The OP wants by position but apparently not the spaces
        // Could be replaced by if (substring !== ' ') depending on needs
        if (substring.trim() !== '') {
            result.push(substring);
        }
        start = position;
    }
    return result;
}

breakByPosition('123 4325 Hello World 43', [3, 4, 8, 9, 20, 21, 23]);

This returns:

["123", "4325", "Hello World", "43"]

2 Comments

The problem with this method is that I'd have to breakByPosition for every unique line that's being fed from the text file. As every line has a unique text field, this wouldn't handle the whole document. But thank you for trying.
Glad you could find the right solution for you. Thank you for your kind reply. Remember to mark the right answer to your question (from Sunny) as the right one. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.