in reply to while (scalar()

I cant't find the place where you pop or shift elements from your arrays, so your while-loops are infinite.