[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] JavaScript Array to MacPerl



In article <mac-perl.199811060555.FAA03972@pacific.telebyte.com>, Brian
McNett <brianmc@telebyte.net> wrote:

>In MessageID: <199811060538.VAA18082@igor.caltech.edu>  Jeff at MacTech 
><online@mactech.com> wrote:
>
>>I'm not sure how a JavaScript array is structure, but if you give me an
>>example of what you have in your file then I can probably help. (If you
>>would like, you can contact me by direct email rather than posting to the
>>list.)
>
>Here's what the array looks like.  This is the actual data:
>
>       arTXT = new Array(
>         " Mushroom Hunt CD is Coming ",
>         " Medicinal Mushrooms III -- A Polypore Named Versicolor ",
[snip]
>         " Great Canadian Scientists T-Shirt ",
>         " Eric Dannels Cantharellus Thesis "
>       )


As I had imagined, the syntax for this array is very similar to the Perl
syntax. This is sort of a strange way to go about it, but I have a
suggestion to get you started. This assumes that the above array is in a
text file, and it is the only thing there. (So you may need to modify as
necessary):

open(IN, "<File_with_array"); #use the actual file name, qualified as
                              #necessary

my @lines = <IN>;   #read the whole file into an array
close(IN);
shift @lines;       #throw away the first line, "arTXT = new Array("
my @final_array;    #this is the array you will fill in with your JS array

#here's the tricky part

eval    '@final_array = ('.     #add beginning of array statement
        join("", @lines).       #join the lines of your file into a string
        ';'                 ;   #add the closing semicolon in the statment

#now @final_array should contain your array.

There are less tricky ways to do this (I believe there is a CPAN module
for parsing comma-separated lists), but in a sense this is the simplest.
What I did, in a nutshell, is take the text of your JS array-creating
statement and fixed it up to be the text of a legal Perl-creating array
statement, and then I used eval to execute this text. So, I let perl's
parser do the actual parsing for me. Now @final_array should have your
array. Note that the whitespace inside of the quotes is still there in
each array element, so if you don't want it there you will either have to
strip it out or not put it ther to begin with.

I hope this helped!
-- 
__________________________________________________________________________

Jeff Clites                Online Editor           http://www.MacTech.com/
online@MacTech.com         MacTech Magazine
__________________________________________________________________________

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch