Message Boards Message Boards

0
|
5497 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 9 years ago

Dear all

I am not an expert in programming at all and apologies for i, but could you tell me what I am doing wrong in the following piece of code:

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"};
words = TextWords[test[[2]]]
words[[1]]
Do[
    Print["i=", i]
    Print[test[[i]]]
    words = TextWords[test[[i]]]
    Print[words[[1]]],  
{i, 2, n}];

I am using the Print commands to see what happens within the loop. I want to see the first word of each line and do more things on each loop iteration, but so far I get just get an error message like:

Part::partd: Part specification words[[1]] is longer than depth of object. >>

words[[1]]

Set::write: Tag Times in words {Vg,N001,0,15V} is Protected. >>

What am I doing wrong? All the Procedure works well outside the loop, but not within the loop. How should a check what is going on within the loop?

I am sure that it must be a stupid thing.

Thank you,

Stefan

POSTED BY: Stefan S
5 Replies
Posted 9 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 9 years ago

Thanks David also for your solution.

Although this always splits the string in four "words" (or at least this sees to me). And at one stage I want to read also the "ic=" word. Anyhow it looks also very easy. I will give a try as well to see what fits me best.

The table representations looks very informative (and clearly shows that it is Spice file ;) )

Regards,

Stefan

POSTED BY: Stefan S
Posted 9 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 9 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 9 years ago

Dear Sandu

It was just the semicolon! All the weekend reading without finding the answer. Must I end all lines with a semicolon within a loop?

I thought that if you end a command with semicolon it doesn't show an output and this was the reason for omitting it in the loop, but I was wrong.

I will also try the more concise solution you propose.

Thank you, Stefan

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

Group Abstract Group Abstract