Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: strings into an array

  1. #1
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default strings into an array

    Hi,

    Im fairly new to javascript etc so please be patient!

    Im have a text file which I need to split into strings, and then enter these strings into an arrray.

    Thus, I have something like this:

    1 hello
    2 how are you
    3 goodbye

    I want to split this into strings (with each line being a new string) and turn this into the following:

    a[0] = 'hello'
    a[1] = 'how are you'
    a[2] = 'goodbye'

    Once in the array, the numbers in the original text become unimportant.

    Now I know that this can be achieved with an arraylist and possibly a scanner(?). But I dont know the syntax to implement these constructs.

    Any help would be much appreciated!

    If you cant help with this particular issue, general help on how to put a string into an array would also be useful.

    Thanks!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Javascript will not (by itself) read a text file. Ajax can possibly be used for that or, if it is on the server, server side scripting.

    It is unclear to me what you want to do vs. what you want the script to do. Could you supply more specific information about your project?

    In any case, the javascript method split() would most likely come in handy. It's syntax is:

    string.split('char');

    Where string is a literal quoted value or a variable containing the string value. The char you use would be a character that you want the string to be split on. For example:

    Code:
    myArray='1 hello,2 how are you,3 goodbye'.split(',');
    Would produce an array called myArray with these items:

    Code:
    myArray[0]='1 hello';
    myArray[1]='2 how are you';
    myArray[2]='3 goodbye';
    Last edited by jscheuer1; 12-09-2006 at 04:06 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    Thanks for your reply. Basically I am trying to get a program to accept a variable (a word), and then read through a string and if the variable is in the string, to highlight it.

    Im using PHP to get the variables etc. In fact the majority of the program uses PHP.

    Thanks for any help!
    Last edited by andyl007; 12-11-2006 at 03:58 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It could be something like:

    HTML Code:
    <script type="text/javascript">
    myArray = <? $_GET['filename'] ?>.split(',');
    </script>
    However, this could only happen as your page loaded, its extension would need to be .php - And it would depend upon $_GET['filename'] being in the form of a comma delimited string.

    Notes: PHP, in its various incarnations, uses different shorthand to tell the server when to turn on and off the PHP interpreter. In my above example, I used the simplest and most common that I am aware of:

    <? do PHP stuff here ?>

    For more specifics, you could try asking about this in the PHP forum.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    John, your PHP will work, but won't output the result, and if it did would output newlines literally, breaking the Javascript. The script would be broken anyway, however, since no quotes surround the string.
    You probably intended to use the <?=expr?> shorthand, which outputs the result of expr. However, since it can't be certain that the server in question will have any form of shorthand enabled, the full syntax, <?php statements ?>, should be used whenever writing scripts for other people (or scripts one might wish to move to another server in the future).
    Code:
    <script type="text/javascript">
    var myArray = "<?php echo str_replace("\n", '\n', $_GET['filename']); ?>".split(',');
    </script>
    Last edited by Twey; 12-09-2006 at 04:14 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    John, your PHP will work, but . . .

    Code:
    <script type="text/javascript">
    var myArray = "<?php echo str_replace("\n", '\n', $_GET['filename']); ?>".split(',');
    </script>
    Thanks Twey, your example looks much more likely to succeed. I am both rusty and inexperienced with PHP. But, and I'm probably just missing something here, shouldn't that str_replace() be:

    PHP Code:
    str_replace('\n'','$_GET['filename']) 
    or something more like that? It looks to me as though you are replacing a newline with a newline.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No. Double quotes do interpolation; single quotes don't. So, I'm replacing a newline with the literal string, \n, which will then be parsed by Javascript.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yeah I probably should have specified that I am running this in PHP.

    Thanks,

    Andy.

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    No. Double quotes do interpolation; single quotes don't. So, I'm replacing a newline with the literal string, \n, which will then be parsed by Javascript.
    That would be fine then, unless he wants to parse the array on the newline from the text file, in which case either the split() or the str_replace() could be changed to accommodate this.

    Quote Originally Posted by andyl007 View Post
    Yeah I probably should have specified that I am running this in PHP.

    Thanks,

    Andy.
    Does this mean that you now have what you were looking for?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So are you are saying that the following code:

    Code:
    $newarray = split("\n", ',', $_GET['filename']);
    Will create a new array, read in text from a pre determined file ('filename'), and input the data into the array as seperated by linebreaks?

    Thanks for your help,

    Andy.
    Last edited by andyl007; 12-11-2006 at 03:58 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •