473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to Convert text list into 2d array

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
5 17942
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
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
"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Oschler | last post by:
With PHP 4, is there a way to convert an associative array to an object? For example, suppose I have the following arrays inside a nested associative array ($nestedAA): $AA1 = 'fieldValue1'; ...
4
by: Daniel Köster | last post by:
Is there someone who has got some tips on how to convert text encoded with character referense ({) to unicode or uft-8 format using VB.net? Is there a function or something that can help with the...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
2
by: Jaime Stuardo | last post by:
Hi all... I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in VB 6.0, I used Stream object to accomplish this which was trivial. I cannot do the same thing in VB.NET. Here...
5
by: Peter Nofelt | last post by:
Hi all, My scenario is as follows: * Receive a base 1 array (index starts at 1 instead of 0) of data from a COM component . * Need to pass this array to a .net component that requires its...
3
by: David | last post by:
Hi, how to convert a char array(byte) to a string variable? byte buffer; string strTest; /*the blow codes all get type of 'buffer': System.Byte! strTest = buffer.ToString() strTest =...
7
by: Jim Lewis | last post by:
I'm trying to move a function into pyrex for speed. The python side needs to pass a list to the pyrex function. Do I need to convert to array or something so pyrex can generate tight code? I'm not...
4
by: Kurien Mathew | last post by:
Hello, What will be a concise & efficient way to convert a list/array.array of n elements into a hex string? For e.g. given the bytes I would like the formatted string 0x74 0x6f 0x6e 0x67...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.