By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,082 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

How to Convert text list into 2d array

P: n/a
At work we have an excel file that contains the list of medications and
their corresponding strengths. I would like to save the excel file as a
text list and paste this list into a javascript function and have JS put
this into an array. Then JS would use this array to create a selection
list which displays only the names of the drugs. When the user
selections one of the drugs, another selection list will be loaded with
the avaiable strengths of that drug. The biggest problem I see if how
to get the list into a javascript array. The list will look something
like (only a lot longer):

Acetaminophen tablets
* 80mg chewable
* 325mg caplet
* 500mg caplet
Acetaminophen supp.
"* 80mg, 120mg, 325mg, 650mg"
Acetaminophen liquid
* 650mg/20.3ml
Acetazolomide tablets
* 250mg scored
Acyclovir capsules
"* 200mg, 800mg"
Adeks tablets
Allbee + C capsules
Aspirin tablets
* 40.5mg chewable
* 81mg chewable
* 81mg enteric coated
* 325mg enteric coated
* 325mg buffered

I am not aware of a way in JS to simply paste this data as is into a
function and have the function read it and put it into an array. Is
there a way I could put the whole list in "" and treat it as a string
and then do an split on the string at each line break? Seeing as the
list is really long, I don't really want to have to type
array.push(value) on each and every line.

Even if this works, I will still likely need a way to turn the data into
a 2d array so that for example:
list[0][0] = acetaminophen tablets
list [0][1] = 80mg chewable, 325 caplet, 500mg caplet
list [1][0] = acetaminophen supp.
etc.

I imagine you could probably do this with some regular expression work
(which I am not very good at, but this would be a good chance to learn).
I just wanted to throw this idea out there and see if anyone has any
ideas on the best way to go about trying to do this. If nothing else, I
could always just not make the web page very exciting and use excel to
save as web page and have people press control+f to find a drug, but
that is just so boring.

Thanks for your thoughts,
Andrew V. Romero

PS: I know a server side langauge would be much better at doing this,
but I have no server side langauge avaiable on the company web server
(not my department).

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <3F**************@icqmail.com>, rr*******@icqmail.com says...
At work we have an excel file that contains the list of medications and
their corresponding strengths. I would like to save the excel file as a
text list and paste this list into a javascript function and have JS put
this into an array. Then JS would use this array to create a selection
list which displays only the names of the drugs. When the user
selections one of the drugs, another selection list will be loaded with
the avaiable strengths of that drug. The biggest problem I see if how
to get the list into a javascript array. The list will look something
like (only a lot longer):

Acetaminophen tablets
* 80mg chewable
* 325mg caplet
* 500mg caplet
Acetaminophen supp.
"* 80mg, 120mg, 325mg, 650mg"
Acetaminophen liquid
* 650mg/20.3ml
Acetazolomide tablets
* 250mg scored
Acyclovir capsules
"* 200mg, 800mg"
Adeks tablets
Allbee + C capsules
Aspirin tablets
* 40.5mg chewable
* 81mg chewable
* 81mg enteric coated
* 325mg enteric coated
* 325mg buffered

I am not aware of a way in JS to simply paste this data as is into a
function and have the function read it and put it into an array. Is
there a way I could put the whole list in "" and treat it as a string
and then do an split on the string at each line break? Seeing as the
list is really long, I don't really want to have to type
array.push(value) on each and every line.

Even if this works, I will still likely need a way to turn the data into
a 2d array so that for example:
list[0][0] = acetaminophen tablets
list [0][1] = 80mg chewable, 325 caplet, 500mg caplet
list [1][0] = acetaminophen supp.
etc.

I imagine you could probably do this with some regular expression work
(which I am not very good at, but this would be a good chance to learn).
I just wanted to throw this idea out there and see if anyone has any
ideas on the best way to go about trying to do this. If nothing else, I
could always just not make the web page very exciting and use excel to


Consider using an XML data island. In HTML you can have a tag of:
<xml id=xmlMeds>
<meds>
<med type="Acetaminophen tablets">
<dose>80mg chewable</dose>
<dose>325 caplet</dose>
<dose>500mg caplet</dose>
</med>
<med type="Another type">
<dose>blah</dose>
</med>
</meds>
</xml>

From Javascript, you can do something like..
<script type="text/javascript">
function getdoses(medtype) {
var nodes = xmlMeds.selectNodes("/meds/med [" + medtype + "]/dose");
for (var x = 0; x < nodes.length; x++) {
// do what you need to do
}
}
</script>

Warning: Air Code!!!
--

Remove NOT from email address to reply. AntiSpam in action.
Jul 20 '05 #2

P: n/a
Fox
It's a two step process -- convert the excel file (Save As...) to a CSV
or TSV (tab separated values, which the following script uses) text
document -- open it up in a text editor and select all and copy.

Run the following script and paste the excel data into the textarea --
click the convert button (when it's done, copy and paste the entire
contents into an html or js file) -- your excel data will be converted
easily into javascript format.

[* this script should handle several hundred lines easily (I converted
an excel file with 730 lines with 13 fields each very easily with this
script (less than a couple of seconds) -- If you have to, you can break
up the excel tsv/csv file into chunks and assemble the final JS array by pieces]

[** using TSV removes the problem of commas that might be used within
the data, as in addresses or business names (e.g.: business, inc. etc...)]

[*** any "empty" field becomes an empty string]

<xmp><!-- remove this line to run -->

<title>Spreadsheet to JS Converter</title>
<script language = javascript>

function
formatData(e)
{
// Excel sometimes adds quotes where none were entered
// so clear those
e=e.replace(/"/g,"");

// this script uses tab delimited data
// or use /,/g as the regex for comma separated values (csv)
e = e.replace(/\t/g, '","');

// this just handles each new line and does a little formatting
// so it's "neat" when you paste it into your html/js file
e = e.replace(/[\n\r]/g, '"],\n\t\t\t["');

return "var spreadsheetData = [\n\t\t\t[\"" + e + "\"]\n];";

}

function
handler(f)
{
f.data.value = formatData(f.data.value);
return false;

}
</script>

<body>
<center>
<h1>Spreadsheet to JS Converter</h1>
<form name=exchanger onsubmit = "return handler(this)">
<textarea name=data rows = 24 cols = 100></textarea>
<br><br>
<input type=submit value = Convert onclick = "return handler(this.form)">
</form>
</center>
</body>

</xmp><!-- remove this line to run -->

"Andrew V. Romero" wrote:

At work we have an excel file that contains the list of medications and
their corresponding strengths. I would like to save the excel file as a
text list and paste this list into a javascript function and have JS put
this into an array. Then JS would use this array to create a selection
list which displays only the names of the drugs. When the user
selections one of the drugs, another selection list will be loaded with
the avaiable strengths of that drug. The biggest problem I see if how
to get the list into a javascript array. The list will look something
like (only a lot longer):

Acetaminophen tablets
* 80mg chewable
* 325mg caplet
* 500mg caplet
Acetaminophen supp.
"* 80mg, 120mg, 325mg, 650mg"
Acetaminophen liquid
* 650mg/20.3ml
Acetazolomide tablets
* 250mg scored
Acyclovir capsules
"* 200mg, 800mg"
Adeks tablets
Allbee + C capsules
Aspirin tablets
* 40.5mg chewable
* 81mg chewable
* 81mg enteric coated
* 325mg enteric coated
* 325mg buffered

I am not aware of a way in JS to simply paste this data as is into a
function and have the function read it and put it into an array. Is
there a way I could put the whole list in "" and treat it as a string
and then do an split on the string at each line break? Seeing as the
list is really long, I don't really want to have to type
array.push(value) on each and every line.

Even if this works, I will still likely need a way to turn the data into
a 2d array so that for example:
list[0][0] = acetaminophen tablets
list [0][1] = 80mg chewable, 325 caplet, 500mg caplet
list [1][0] = acetaminophen supp.
etc.

I imagine you could probably do this with some regular expression work
(which I am not very good at, but this would be a good chance to learn).
I just wanted to throw this idea out there and see if anyone has any
ideas on the best way to go about trying to do this. If nothing else, I
could always just not make the web page very exciting and use excel to
save as web page and have people press control+f to find a drug, but
that is just so boring.

Thanks for your thoughts,
Andrew V. Romero

PS: I know a server side langauge would be much better at doing this,
but I have no server side langauge avaiable on the company web server
(not my department).

Jul 20 '05 #3

P: n/a
"Mosley Jones III" <me@info.gov.mars> writes:
I make 2d or 3d arrays in JS like this.
I make up a fake tag with attribuits, and put them in the head section

<fakeTag att1=value1 att2=name1 att3=address1 ></fakeTag>
<fakeTag att1=value2 att2=name2 att3=address2 ></fakeTag>
<fakeTag att1=value3 att2=name3 att3=address3 ></fakeTag> .... var dis = getElementByTagName('fakeTag')
var dis = document.getElementsByTagName('fakeTag');

so dis(0).att1 would = value1


Actually not in all browsers, but
dis[0].getAttribute("att1")
would give "value1".
In Mozilla, the result of documents.getElementsByTagName is not a function.
In neither Mozilla nor Opera is "att1" a property of the fake elements,
so you must use "getAttribute".
If you are going to invent your own HTML extension (which obviously
won't allow the page to validate), then you might as well use XML.

Still, neither are very good ideas if you want all browsers to
understand it. If you embed the arrays in javascript, then all
browsers that understand Javascript will also be able to access the
table.

All modern browsers seem to accept your fakeTag method, but ofcourse
NS4 doesn't. It would understand a Javascript literal as the one
Douglas Crockford gave as example.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:r8**********@hotpop.com...
"Mosley Jones III" <me@info.gov.mars> writes:
I make 2d or 3d arrays in JS like this.
I make up a fake tag with attribuits, and put them in the head section

<fakeTag att1=value1 att2=name1 att3=address1 ></fakeTag>
<fakeTag att1=value2 att2=name2 att3=address2 ></fakeTag>
<fakeTag att1=value3 att2=name3 att3=address3 ></fakeTag> ...
var dis = getElementByTagName('fakeTag')


var dis = document.getElementsByTagName('fakeTag');


thank you


so dis(0).att1 would = value1


Actually not in all browsers, but
dis[0].getAttribute("att1")
would give "value1".
In Mozilla, the result of documents.getElementsByTagName is not a

function. In neither Mozilla nor Opera is "att1" a property of the fake elements,
so you must use "getAttribute".
If you are going to invent your own HTML extension (which obviously
won't allow the page to validate), then you might as well use XML.

Still, neither are very good ideas if you want all browsers to
understand it. If you embed the arrays in javascript, then all
browsers that understand Javascript will also be able to access the
table.

All modern browsers seem to accept your fakeTag method, but ofcourse
NS4 doesn't. It would understand a Javascript literal as the one
Douglas Crockford gave as example.
it should if you use the NN4 DOM


/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #5

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:3c**********@hotpop.com...
"Mosley Jones III" <me@info.gov.mars> writes:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:r8**********@hotpop.com...
"Mosley Jones III" <me@info.gov.mars> writes:

> I make 2d or 3d arrays in JS like this.
>
>
> I make up a fake tag with attribuits, and put them in the head section >
> <fakeTag att1=value1 att2=name1 att3=address1 ></fakeTag>
> <fakeTag att1=value2 att2=name2 att3=address2 ></fakeTag>
> <fakeTag att1=value3 att2=name3 att3=address3 ></fakeTag>
...
> var dis = getElementByTagName('fakeTag')

var dis = document.getElementsByTagName('fakeTag');

thank you

>
> so dis(0).att1 would = value1

Actually not in all browsers, but
dis[0].getAttribute("att1")
would give "value1".
In Mozilla, the result of documents.getElementsByTagName is not a

function.
In neither Mozilla nor Opera is "att1" a property of the fake elements, so you must use "getAttribute".
If you are going to invent your own HTML extension (which obviously
won't allow the page to validate), then you might as well use XML.

Still, neither are very good ideas if you want all browsers to
understand it. If you embed the arrays in javascript, then all
browsers that understand Javascript will also be able to access the
table.

All modern browsers seem to accept your fakeTag method, but ofcourse
NS4 doesn't. It would understand a Javascript literal as the one
Douglas Crockford gave as example.


it should if you use the NN4 DOM


That is the problem. There is no way in the NN4 DOM to access
arbitrary elements. In order to access elements through the
document.layers collection, they must be absolutely positioned,
which makes little sense for elements in the head element.


yes it does, it would make no sence to put them in the body
There is no equivalent to getElementsByTagName.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.