Go Back   Dynamic Drive Forums > General Coding > JavaScript
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 12-08-2006, 10:02 PM
andyl007 andyl007 is offline
Junior Coders
 
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!
Reply With Quote
  #2  
Old 12-09-2006, 04:01 AM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 19,000
Thanks: 19
Thanked 1,135 Times in 1,121 Posts
Blog Entries: 3
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';
__________________
WWWWWWWWWWWW
- John
________________________

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

Last edited by jscheuer1; 12-09-2006 at 04:06 AM.
Reply With Quote
  #3  
Old 12-09-2006, 12:29 PM
andyl007 andyl007 is offline
Junior Coders
 
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.
Reply With Quote
  #4  
Old 12-09-2006, 03:50 PM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 19,000
Thanks: 19
Thanked 1,135 Times in 1,121 Posts
Blog Entries: 3
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.
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
  #5  
Old 12-09-2006, 04:08 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
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>
__________________
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!

Last edited by Twey; 12-09-2006 at 04:14 PM.
Reply With Quote
  #6  
Old 12-09-2006, 08:37 PM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 19,000
Thanks: 19
Thanked 1,135 Times in 1,121 Posts
Blog Entries: 3
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.
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
  #7  
Old 12-09-2006, 10:29 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
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!
Reply With Quote
  #8  
Old 12-09-2006, 11:11 PM
andyl007 andyl007 is offline
Junior Coders
 
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.
Reply With Quote
  #9  
Old 12-10-2006, 05:13 AM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 19,000
Thanks: 19
Thanked 1,135 Times in 1,121 Posts
Blog Entries: 3
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?
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
  #10  
Old 12-10-2006, 09:05 PM
andyl007 andyl007 is offline
Junior Coders
 
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.
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:42 AM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.