Octave - putting words into vector - octave

I am working on creating an email filter. I have a sample email which says something like this:
Hi how are you lets meet up
I want to put each one of these words into a vector. I am looking for something like this.
Words = ['Hi';'how','are','you','lets','meet','up']
and when I enter
words(1), I want it to display Hi.
I really don't know where to start. I found answers for different languages such as Ruby and JS. But not for Octave.

Adding to Andy's answer about cells, you can collect your email as a string and process it using string operations such as strtok, strsplit etc. e.g.
octave:7> s = 'Hi how are you lets meet up';
octave:8> words = strsplit(s, ' ')
words =
{
[1,1] = Hi
[1,2] = how
[1,3] = are
[1,4] = you
[1,5] = lets
[1,6] = meet
[1,7] = up
}
octave:9> words{1}
ans = Hi

Use Cell Arrays of Strings:
octave:1> words = {'hi', 'how', 'are', 'you', 'lets', 'meet', 'up'};
octave:2> words{1}
ans = hi
and you can use indexing:
octave:4> words{3:4}
ans = are
ans = you
if you struggle why this returns a different result:
octave:5> words(3:4)
ans =
{
[1,1] = are
[1,2] = you
}
then read here:
So with ‘{}’ you access elements of a cell array, while with ‘()’ you access a sub array of a cell array.

Related

When appending to a List of JSON Objects in python, why is it duplicating only the "2nd layer" objects?

Here is my code.
def generateNewDevices(numofdevices):
global simulated_devices
for x in range(numofdevices):
new_device = reference_device.copy()
new_device["accel"]["accx"] = random.randint(-32768, 32767)
new_device["accel"]["accy"] = random.randint(-32768, 32767)
new_device["accel"]["accz"] = random.randint(-32768, 32767)
new_device["location"]["gpsla"] = random.uniform(MINLAT, MAXLAT)
new_device["location"]["gpslo"] = random.uniform(MINLON, MAXLON)
new_device["temp"] = random.randint(-127, 127)
new_device["status"] = random.randint(0, 1)
str1 = new_device["deviceName"]
str1 = str1[:-3]
str2 = str(x).zfill(3)
str1 += str2
new_device["deviceName"] = str1
node_red_send(URL, new_device)
print(new_device)
simulated_devices.append(new_device)
generateNewDevices(3)
for x in range(len(simulated_devices)):
print(simulated_devices[x])
Why when printing through the list of values at the end, does the list show the "new device" data for appended JSON objects "1 layer deep" (temp, status and name) but duplicate the data for "2 layers deep" (accx, accy, gpsla)?
The .copy()s are in there because I was having issues with python append duplicating all the values at first. Is this some variation of the same issue? I even tried .copy()ing right before appending to the list. (I come from a c/c++ background so I do not fully understand why python does some of its things)
Any help appreciated.
Kr, apuri123.
I doubt anyone will end up here when searching for an answer, but in case you do, you are looking for "deepcopy":
import copy
original = {} #object with as many objects within objects as you want
myCopy = copy.deepcopy(original)
Google "python deepcopy" and you should be able to find what you are looking for.

Separate strings in a string with Actionscripts3

I'm trying to separate two part of a string, one is Title one is Value, RegExp is confused me. I need your help to solve this thanks
var pattern2:RegExp = new RegExp("TZ_NUM_ANSWER:Telegram code([0-9.-]+)");//TZ_NUM_ANSWER:Telegram code 32263
var data2:Object = pattern2.exec(response);
if (data2 != null && data2[1] != null)
{
var value2:Number = parseFloat(data2[1]);
trace("TZ_NUM_ANSWER " + value2);
txt_BUY1.text = String(value2);
}
Output:
TZ_NUM_ANSWER:Telegram code 32263
It must be:
"TZ_NUM_ANSWER:" "Telegram code 32263"
The result of split is an Array you can access to Array indexes and assign them to a variable.
var STR1:String = "TZ_NUM_ANSWER:Telegram code 32263";
var STR2:String;
var STR3:String;
trace(STR1.split(":"));
STR2 = STR1.split(":")[0];
STR3 = STR1.split(":")[1];
trace (STR2);
trace (STR3);
Result:
TZ_NUM_ANSWER
Telegram code 32263
Don't use RegEx for simple stuff. All you need is basic string methods:
response.split(":");

Use RegExp and split to read file text flash

I have a file text more than 1 000 000 lines that begins by the character C and other one by M
Example:
C9203007870000000000000006339912610971240095400111200469300000 16122011AMI 00000100010000315 080
C9203007870000000000000006339912610971240095400111200469300000 09122011B 590001000100000270016092100
M920300787000000000000000633991261097124009540011120046930000031122011JVJF004 10 N
M920300787000000000000000633991261097124009540011120046930000009122011DEQP003 10 N
M920300787000000000000000633991261097124009540011120046930000012122011ACQK001 10Z N
C9203007870000000000000006339912610971240095400111200469300000 24122011AMI 00000100010000315 080
C9203007870000000000000006339912610971240095400111200469300000 24122011AMI 00000100010000315 080
I want to put in my array only the lines who begins with the character M
How I can add in my split: var pattern:RegExp = /^M/;
var mFileReference:FileReference;
var mArray:Array = new Array();
function onFileLoaded(event:Event):void
{
mFileReference = event.target as FileReference;
data = mFileReference["data"];
mArray = (data.toString()).split("\n");
}
I don’t want to pass by the loop ‘for’ its take a lot of time and resources
I want to add /^M/ to my split is it possible?
for each (var s:String in mArray)
{
if (pattern.test(s)) {
values.push(s);
}
}
Thanks everybody.
Try this regular expression:
/^M.*/gm
This should match all lines that begin with M and nothing else.
It uses the g flag to match all cases of the expression in the string, and it uses m for multiline mode, so ^ and $ will match the beginning/end of lines instead of the beginning/end of the string.
You can get get your array like this:
mArray = data.toString().match(/^M.*/gm);

How to parse HTML tags in Matlab using regexp?

I'm short on time and specifically wanted to extract a string like the one below. Problem is the tag isn't of the form <a> data </a>.
Given,
s = <em style="font-size:medium"> 5,888 </em>
how to extract out just 5,888 in matlab?
You will find useful info here, or here, or here, all of which are google-first-page results and would have been faster than asking a question here.
Anyway, quick-'n-dirty way: You can filter on the <> symbols:
>> s = '<em style="font-size:medium"> 5,888 </em> <sometag> test </sometag>'
>> a = regexp(s, '[<>]');
>> s( cell2mat(arrayfun(#(x,y)x:y, a(2:2:end-1)+1, a(3:2:end)-1, 'uni',false)) )
ans =
5,888 test
Or, slightly more robust and much cleaner, replace everything between any tags (including the tags) with emptyness:
>> s = regexprep(s, '<.*?>', '')
ans =
5,888 test
Thanks folks for your help. I'm basically trying to get the population of a US county on Matlab. Thought I'l share my code, though not the most elegant. Might help some soul. :)
county = 'morris';
state = 'ks';
county = strrep(county, ' ' , '+');
str = sprintf('https://www.google.com/search?&q=population+%s+%s',county,state);
s = urlread(str);
pop = regexp(s,'<em[^>]*>(.*?)</em>', 'tokens');
pop = char(pop{:});
pop = strrep(pop, ',' , '');
pop = str2num(pop);

Octave vectorize strsplit return value into separate variables

I have a file with a list of records which I parse one line at a time. Each record is newline delimited and each value is space delimited. This is just a simplified example, but it has a similar structure to the real data.
Bob blue pizza
Sally red sushi
The first value is a name, then their favorite color, then their favorite food. Let's say this is in a processing loop and I want to set variables up for each value. For the first line, my values should look like this.
friendsName = "Bob";
favoriteColor = "blue";
favoriteFood = "pizza";
I read in the line and start out with
lineInFile = "Bob blue pizza";
strsplit seems like a good idea, but it outputs a cell array instead of a matrix of strings and I end up with
strsplit(lineInFile, " ") =
{
[1,1] = Bob
[1,2] = blue
[1,3] = pizza
}
I want something like
{friendsName,favoriteColor,favoriteFood} = strsplit(lineInFile, " ");
This gives me error: invalid lvalue function called in expression
Arrays can be used as lvalues, so I tried
cell2mat(strsplit(lineInFile, " "))
ans = Bobbluepizza
That's not what I want.
This worked.
[friendsName favoriteColor favoriteFood] = strsplit(lineInFile, " "){1,:}