CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

How to read a list of integers from a string?

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 2 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 23, 2021, 11:05
Question How to read a list of integers from a string?
  #1
New Member
 
ONESP-RO
Join Date: Feb 2021
Posts: 24
Rep Power: 3
NotOverUnderated is on a distinguished road
Hello,
I am trying to convert a word (string) to a list as follows:

Code:
List<Foam::label> indices;
const word w = "(0 0 0)";
w >> indices;
This doesn't work. I know I can read from a dictionary using lookup function but I want to know if it is possible to populate a list from a word containing the values using IStream or any other method without using a dictioanry.

Any ideas?

Thank you

Last edited by NotOverUnderated; October 23, 2021 at 14:45.
NotOverUnderated is offline   Reply With Quote

Old   October 28, 2021, 16:06
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,503
Rep Power: 35
olesen will become famous soon enougholesen will become famous soon enough
Quote:
Originally Posted by NotOverUnderated View Post
Hello,
I am trying to convert a word (string) to a list as follows:

Code:
List<Foam::label> indices;
const word w = "(0 0 0)";
w >> indices;
This doesn't work. I know I can read from a dictionary using lookup function but I want to know if it is possible to populate a list from a word containing the values using IStream or any other method without using a dictioanry.

Any ideas?

Thank you

Sure, there are a few ways to do that, but FWIW you would want a string not a 'word' for the intermediate anyhow since a "word" generally should not be containing spaces!
Code:
List<label> indices;


// Use a IStringStream
 string str("( 1 2 3 )");
IStringStream is1(str);
is1 >> indices;

// Parse tokenize directly
string str("( 1 2 3 )");
ITstream is2(str);
is2 >> indices;

// If you have read the input into a list of characters, received from a pipe or whatever
List<char> charbuffer = ....;

UIListStream is3(charbuffer);
is3 >> indices.

// Can even use the same thing for your string
string str("( 1 2 3 )");
UIListStream is4(str.data(), str.length());
is4 >> indices.
Still a few more methods on top of that. If you notice that the UIListStream just takes a pointer and num chars, you'll realize that you can also use these to write your own parser etc.
olesen is offline   Reply With Quote

Old   October 28, 2021, 19:33
Default
  #3
New Member
 
ONESP-RO
Join Date: Feb 2021
Posts: 24
Rep Power: 3
NotOverUnderated is on a distinguished road
Many thanks for providing the answer. I thought that was not possible.

Thank you
NotOverUnderated is offline   Reply With Quote

Reply

Tags
conversion, list, openfoam, programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a vector list with IFstream and create IOobject from it Sylv OpenFOAM Programming & Development 1 October 9, 2017 16:25
Help for the small implementation in turbulence model shipman OpenFOAM Programming & Development 25 March 19, 2014 11:08
[swak4Foam] swak4Foam-groovyBC build problem zxj160 OpenFOAM Community Contributions 18 July 30, 2013 14:14
"parabolicVelocity" in OpenFoam 2.1.0 ? sawyer86 OpenFOAM Running, Solving & CFD 21 February 7, 2012 12:44
Read inside a class tonyuprm OpenFOAM 12 July 14, 2010 03:35


All times are GMT -4. The time now is 17:00.