I have a Perl script to capture Mathematica output, but am trying to find a good way to just catch the line starting with "Out[1]".
I thought I would just check if ( $line =~ /^Out[1]=/ )
What am I doing wrong? Code is below, and then output of program below that:
#!/usr/bin/perl
#perl testMathy.pl
use strict;
use warnings;
### Test 1: Print entire Mathematica output from calling Roots[]
my @charPoly = "-R^3 + R^2 + 3*R - 3";
my @roots = `echo \" Roots[@charPoly == 0, R] \" | math`;
print "\n***ROOTS (TEST 1)***: [@roots]\n~~~~~~~~~~\n";
### Test 2: Print line beginning with Out[1]
my $rootList = "";
for my $line ( @roots ){
#take line that starts with Out[1]
print "LINE:", $line;
if ( $line =~ /^Out[1]=/ ){
print "success\n";
$rootList .= $line;
last; #break
}
}
print "\n***ROOTS (TEST 2)***: $rootList\n~~~~~~~~~~\n";
Output:
***ROOTS (TEST 1)***: [Mathematica 10.1.0 for Linux x86 (64-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:=
Out[1]= R == Sqrt[3] || R == -Sqrt[3] || R == 1
In[2]:=
]
~~~~~~~~~~
LINE:Mathematica 10.1.0 for Linux x86 (64-bit)
LINE:Copyright 1988-2015 Wolfram Research, Inc.
LINE:
LINE:In[1]:=
LINE:Out[1]= R == Sqrt[3] || R == -Sqrt[3] || R == 1
LINE:
LINE:In[2]:=
***ROOTS (TEST 2)***:
~~~~~~~~~~
Why is ROOTS(TEST 2): blank?
Square brackets are special characters in a regex. Try:
$line =~ /^Out\[1\]=/
I'd recommend reading a regex tutorial like this one to learn this kind of thing.