associative arrays in Switch

Post Reply
tz8
Member
Posts: 84
Joined: Mon Aug 13, 2012 12:56 pm

associative arrays in Switch

Post by tz8 »

Good morning fellas,



imagine being faced with the following XML:



<order number="15006888">

<products>

<product type="AH_FOLDEDCARD_4P_A6_POR_4_1">

<orderitem id="3" crc="8f64fc6f" name="15006888-AH_FOLDEDCARD_4P_A6_POR_4_1-3.pdf"/>

</product>

</products>

</order>



i'm calling an external process to check the crc value of ordernumber*.pdf files and getting this back on stdout:



; Generated on 04/15/13 at 14:44:31

;

8f64fc6f ?CRC32*15006888-AH_FOLDEDCARD_4P_A6_POR_4_1-3.pdf



now i want to go through the resulting lines and push them in a multidimensional associative array:



var crcCheck = new Process( args );

// start crc32 check

crcCheck.start();

crcCheck.waitForFinished();



var crc = new Array;



// push results into array

// example output on stdout:

// -------

// ; Generated on 04/15/13 at 14:44:31

// ;

// 8f64fc6f ?CRC32*15006888-AH_FOLDEDCARD_4P_A6_POR_4_1-3.pdf

// -------

// example resulting array:

// crc[15006888-AH_FOLDEDCARD_4P_A6_POR_4_1][3] == 8f64fc6f

var stdOut = crcCheck.readStdout().split("n");

var product;

var id;

for ( i = 0; i < stdOut.length; i++ )

{

var crcLine = stdOut;

// skip clutter lines without CRC values

if ( crcLine.match(/?CRC32*/) )

{

product = crcLine.substring(16).split("-")[1];

id = crcLine.substring(16).split("-")[2].split(".")[0];

if ( !crc[product] )

crc[product] = new Array;

crc[product][id] = crcLine.substring(0, 8);

s.log(1, crcLine.substring(16) + " " + crc[product][id]);

}

}



well guess... it doesn't work... the last s.log line already fails.



Can someone please push me in the right direction on what i'm doing wrong? I can't seem to be able to fill an associative array with strings as keys.



Any hints?
tz8
Member
Posts: 84
Joined: Mon Aug 13, 2012 12:56 pm

associative arrays in Switch

Post by tz8 »

oh i forgot the error message:



Error in line 60 of script : Error. Trying to access undefined member 'AH_FOLDEDCARD_4P_A6_POR_4_1'

line 60 is the s.log() at the end of the code snippet above
freddyp
Advanced member
Posts: 413
Joined: Thu Feb 09, 2012 3:53 pm

associative arrays in Switch

Post by freddyp »

The mistake is in the if. You are trying to test whether crc[product] exists with



if ( !crc[product] )



but this tests whether crc[product] is false, which of course it is not, because it is undefined. With this it works:



if ( typeof(crc[product]) == "undefined" )



Freddy
tz8
Member
Posts: 84
Joined: Mon Aug 13, 2012 12:56 pm

associative arrays in Switch

Post by tz8 »

thanks freddy, that did the trick!
tz8
Member
Posts: 84
Joined: Mon Aug 13, 2012 12:56 pm

associative arrays in Switch

Post by tz8 »

i actually was on the right track with .hasOwnProperty - but i forgot that Switch's javascript implementation is dating back to the age when pyramids were build...
Post Reply