Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.3K Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Counting words in a string of a list of string using a Do loop.

Posted 10 years ago
POSTED BY: Stefan S
5 Replies
Posted 10 years ago

A quick top-level parse might help:

In[1]:= test = {"* \
/Users/Esteban/Documents/Investigacion/Space_UVEG/PHI/SSStudy/Steady/\
BuckLT.asc", "Vg N001 0 15V", 
   "Vdrv N004 N002 PULSE(0 10 0u 100n 100n 3.3u 10u)", 
   "Cf vCf 0 4.7u ic=86.1", ".model D D000"};

In[2]:= parse4[str_] := StringSplit[str, " ", 4]

In[3]:= parsedDeck = parse4 /@ Most@Rest@test

Out[3]= {{"Vg", "N001", "0", "15V"}, {"Vdrv", "N004", "N002", 
  "PULSE(0 10 0u 100n 100n 3.3u 10u)"}, {"Cf", "vCf", "0", 
  "4.7u ic=86.1"}}

In[4]:= TableForm[parsedDeck, 
 TableHeadings -> {None, {"Name", "Node1", "Node2", "Value"}}]

enter image description here

POSTED BY: David Keith
Posted 10 years ago
POSTED BY: Stefan S
Posted 10 years ago

Hi Stefan, you are right about it splitting into at most 4 words. I did that because I recognized the Spice format. Parsing that 4th word is more complicated. For example Pulse( . . . ) must be recognized as a source spec, with the required argument list, and can't be just parsed at spaces until you've decided that the what is in ( . . ) is the argument list. Although you could eliminate the limit of 4 words, and then handle the split string. You could in fact add "(" and ")" to the list of delimiters and wind up with "Pulse" and 7 following arguments. (This is also a good example of a convenient truth: In Mathematica, there is almost always a better solution than a procedural loop.)

Best, David

POSTED BY: David Keith
Posted 10 years ago

Hi, Stefan

to print the first word, you might find this line of code somewhat useful:

StringSplit[#, Except[WordCharacter] ..][[1]] & /@ test

otherwise, this should work:

n = Length[test];
Do[Print["i=", i]; 
  Print[test[[i]]]; 
  words = TextWords[test[[i]]]; 
  Print[words[[1]]], {i, 2, n}];
POSTED BY: Sandu Ursu
Posted 10 years ago
POSTED BY: Stefan S
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard