1
$\begingroup$

I wrote a notebook on Mac OS 10.7.4 and run it on Windows XP at work. On my Mac I use a full version of Mathematia 8.0.1.0, whereas on the Windows-system only a trial version (Version 8.0.4.0.).

First of all I cannot run the following code on the Windows system (on Mac it works perfect):

ImportData={{"Test1", 5670.`, 0.`, 180.`, 2756.`, 2443.`, 180.`, 1008.`, 2007.`},
            {"Test2", 100.`, 0.`, 3006.`, 1235.`, 9874.`, 4543.`, 3241.`, 5004.`}}; 

WhitespaceCleaning[string_] := 
     StringReplace[
      string, (StartOfString ~~ Whitespace) | (Whitespace ~~ 
          EndOfString) :> ""]

OriginalData = 
  Drop[ImportData, 1] /. {a_, b_, cc_, d_, e_, f_, g_, h_, i_} ->
  {WhitespaceCleaning@a, {b, cc, d, e, f, g, h, i}};

However I can get the same result as on my Mac with the following code for my Windows system:

allData=ImportData;
numData=allData[[All,2;;]]
strings0=allData[[All,1]];
stringsWOSpacing=WhitespaceCleaning/@strings0;
OriginalData=Transpose[{stringsWOSpacing,numData}]

The rest of my code runs perfect (I get the same results on both operating systems). However, running my code on Windows provokes many messages (such as Divide::index, Divide::infy, Power::infy, General::stop, Less::nord, LessEqual::nord) which does not happen on my Mac.

I don't understand why Mathematica on my Windows system is acting so odd. Does anyone has an idea?

$\endgroup$
7
  • 1
    $\begingroup$ The replacement is not used at all (as the left hand side of the rule does not match anything in the list), though WhitespaceCleaning@a is evaluated before any replacement is done, and its argument is not a string, so an error message is issued. Try RuleDelayed (:>) instead of simply using Rule (->). $\endgroup$ Jun 26, 2012 at 19:48
  • $\begingroup$ @IstvánZachar: I replaced -> with :>. Works now on Windows. However, both -> and :> return the same result on my Mac. I don't understand why "the left hand side of the rule does not match anything in the list". In ImportData I have two lists which do match the pattern {a_, b_, cc_, d_, e_, f_, g_, h_, i_}. $\endgroup$
    – John
    Jun 26, 2012 at 19:57
  • $\begingroup$ Sorry, my mistake: it does match. Do you load any custom packages on your Mac? Did you make sure no previous garbage is left in the kernel? Better run on both machines with a fresh kernel. $\endgroup$ Jun 26, 2012 at 20:03
  • $\begingroup$ @IstvánZachar No problem. I did not load any custom packages and I run the code with fresh kernel on both systems. Problem remains. $\endgroup$
    – John
    Jun 26, 2012 at 20:06
  • 1
    $\begingroup$ Note that you can use StringTrim for your whitespace cleaning: reference.wolfram.com/mathematica/ref/StringTrim.html $\endgroup$ Jun 26, 2012 at 22:22

1 Answer 1

4
$\begingroup$

My feeling is that your Windows system is performing like it should and that the problem lies with your OS X system. Using the -> instead of :> causes the immediate evaluation of the right hand side of the rule yielding an expression of the form

StringReplace[a, (StartOfString ~~ Whitespace) | (Whitespace ~~ EndOfString) :> ""] 

with a still undefined. Since StringReplace expects a string as first argument it balks and generates an error message. It then returns unevaluated. Then the pattern match kicks in, a gets the value "Test2" and evaluation of StringReplace continues, now with the sting "Test2" as argument which works.

In this case the :> is the better choice.

The question should be: why doesn't your Mac version object like the Windows version does (and not the other way around)?

I see a number of options:

  • On your Mac version you have set error messages preferences (Edit > Preferences > Evaluation> Message and Warning action) to not print to the input notebook
  • You have switched off error messages using Off somewhere

You might try (on your Mac MMA) the following and see whether it generates an error message (it should):

Message[StringReplace::strse]
$\endgroup$
2
  • $\begingroup$ I took MikeHoneychurch's tip into consideration and replaced WhitespaceCleaning by StringTrim and changed a_ to a_String (I didn't know that I needed to declare the type of my variables always). I changed the setting for "Message and Warning action". Still, evaluating the same code on both systems results in a red highlighted cell on Windows (hence, indicating a message) and not highlighted cell on Mac OS. However, on both systems the same messages appear (and presumably did also before). $\endgroup$
    – John
    Jun 27, 2012 at 17:42
  • $\begingroup$ The a_String should not be seen as a variable declaration but as a restriction of the definition to arguments of this type. Without it, WhitespaceCleaning fires on any argument including non-strings and undefined symbols which will get you errors in this case. When you add String, it is only executed when it gets an argument with head String which it can process. $\endgroup$ Jun 27, 2012 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.