PDA

View Full Version : strings into an array



andyl007
12-08-2006, 10:02 PM
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!

jscheuer1
12-09-2006, 04:01 AM
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:


myArray='1 hello,2 how are you,3 goodbye'.split(',');

Would produce an array called myArray with these items:


myArray[0]='1 hello';
myArray[1]='2 how are you';
myArray[2]='3 goodbye';

andyl007
12-09-2006, 12:29 PM
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!

jscheuer1
12-09-2006, 03:50 PM
It could be something like:


<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.

Twey
12-09-2006, 04:08 PM
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).
<script type="text/javascript">
var myArray = "<?php echo str_replace("\n", '\n', $_GET['filename']); ?>".split(',');
</script>

jscheuer1
12-09-2006, 08:37 PM
John, your PHP will work, but . . .


<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:


str_replace('\n', ',', $_GET['filename'])

or something more like that? It looks to me as though you are replacing a newline with a newline.

Twey
12-09-2006, 10:29 PM
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.

andyl007
12-09-2006, 11:11 PM
Yeah I probably should have specified that I am running this in PHP.

Thanks,

Andy.

jscheuer1
12-10-2006, 05:13 AM
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.


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?

andyl007
12-10-2006, 09:05 PM
So are you are saying that the following 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.

jscheuer1
12-10-2006, 09:54 PM
Your code:


$newarray = split("\n", ',', $_GET['filename']);

appears to me to be mixing PHP and javascript in a way that probably will not work in either. Do you even want to use javascript for this at all? In what language (javascript or PHP) do you want your resultant array to be?

Twey
12-10-2006, 10:05 PM
Certainly not. You'd need something like:
<script type="text/javascript">
var a = [
"<?php echo(implode("\",\n \"", file($_GET['filename'])); ?>"
];
</script>

jscheuer1
12-10-2006, 10:20 PM
<script type="text/javascript">
var myArray = "<?php echo str_replace("\n", '\n', $_GET['filename']); ?>".split(',');
</script>


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.


Certainly not. You'd need something like:
<script type="text/javascript">
var a = [
"<?php echo(implode("\",\n \"", file($_GET['filename'])); ?>"
];
</script>

Or, I guess you could implode . . . That's a new one on me. At least you didn't explode, though nearly (and I bet that's a PHP method too, huh?). However, following what was already laid out, I guess this would work to produce a javascript array parsed on the textfile's newlines:


<script type="text/javascript">
var myArray = "<?php echo str_replace("\n", ',', $_GET['filename']); ?>".split(',');
</script>

Twey
12-10-2006, 10:36 PM
I guess this would workIndeed so.
I bet that's a PHP method too, huh?Yes :) Documentation: implode (http://www.php.net/implode)(), explode (http://www.php.net/explode)().

andyl007
12-10-2006, 11:59 PM
Yeah sorry for the confusion; I am writing in PHP. I posted a similar question on another forum, however, and was told it was javascript! :)

Andy.

jscheuer1
12-11-2006, 04:00 AM
I think I'll let Twey take this one from here or, if he is busy, advise you to take this to the PHP forum. Looks like you were misled into posting here in the javascript forum in the first place.

Things that would help:

A link to your page.

A link to a page elsewhere (if one exists) that does what you are attempting.

A full explanation of your project.

Twey
12-11-2006, 03:52 PM
You've had, in fact, two examples of possible code (although beware: neither is secure! Validation should be performed on the filename first).

jscheuer1
12-11-2006, 06:00 PM
I feel I should mention that andyl007 had laid out a bit of his PHP code in post number 15 of this thread and apparently edited it out after I responded and before Twey saw it.

I'm not sure why andyl007 did that and think that s/he would have gotten a more helpful response from Twey if s/he hadn't removed the code from that post.