Problem in getting the correct number from the string.


 
Thread Tools Search this Thread
Top Forums Programming Problem in getting the correct number from the string.
# 1  
Old 05-29-2009
Problem in getting the correct number from the string.

I have string named texts which consist of section label “BOOK-SEC-“. Section starts from 1 to n, where n is a number. For this example conside the value of n is 9. That is, the string variable looks like
“BOOK-SEC-1 blablgjfsdgdflgfablablablablaBOOK-SEC-2blablablabalasdfsdhfhBOOK-SEC-3blajgfyasdasdfgdagfblablaBOOK-SEC-4blablajahdu.....styadhsabfdaskfBOOK-SEC-9blablaahdfgablaz".
The data following the BOOK-SEC-1 belongs to section 1, the data following the BOOK-SEC-2 belongs to section 2 and so on.
See the below code:
#include<iostream>
#include<sstream>
using namespace std;
const std::string SECTION_LABEL ("BOOK-SEC-");
static unsigned int getNumberSections (const std::string &texts );
int main ()
{
string texts;
texts = "BOOK-SEC-1blablgjfsdgdflgfablablablablaBOOK-SEC-2blablablabalasdfsdhfhBOOK-SEC-3blajgfyasdasdfgdagfblablaBOOK-SEC\
-4blablaj......ahdustyadhsabfdaskfBOOK-SEC-9blablaahdfgablaz";
unsigned int number;
number = getNumberSections(texts);
cout<<"\n the number is: "<<number<<endl;
return 0;
}
unsigned int getNumberSections (const string & texts)
{
string::size_type pos = texts.rfind (SECTION_LABEL);
pos += SECTION_LABEL.length ();
string numberAsText;
while (isdigit (texts[pos]))
{
numberAsText.push_back (texts[pos]);
++pos;
}
return atoi (numberAsText.c_str ());
}

This gives the correct output: the number is: 9

The problem when the string looks like:
string texts;
texts = "BOOK-SEC-1blablgjfsdgdflgfablablablablaBOOK-SEC-2blablablabalasdfsdhfhBOOK-SEC-3blajgfyasdasdfgdagfblablaBOOK-SEC\
-4blablaj......ahdustyadhsabfdaskfBOOK-SEC-99blablaahdfgablaz";

check the part "BOOK-SEC-99blablaahdfgablaz. Here section is 9 and another 9 is the data of that section.
For this string I get the output as
the number is: 99
which is wrong.

please suggest a solution to get the correct value.

Note: In any case we should not alter the SECTION_LABEL or to distrub the string variable.
# 2  
Old 05-29-2009
This sick command could do the trick
Code:
sed 's/BOOK/\nBOOK/g' youfile.txt | sed 's/BOOK-SEC-\([1-9]\).*/\1/g'

There must be smarter way to do that but i didn't find out yet.

Last edited by thanhdat; 05-29-2009 at 06:22 AM..
# 3  
Old 05-29-2009
As I noticed the text "BOOK-SEC-" is fixed and occurring again and again, what you can do is :: Search the pattern "BOOK-SEC-" in your string then read one byte. Also maintain count in your string and check the end of string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python : Problem with " TypeError: float() argument must be a string or a number "

Hello ! I'm creating a CGI which allow to display graph from some data. The datas looks like : 2020-01-13-00-00,384.00,350.00 2020-01-13-06-00,384.00,350.00 2020-01-13-12-00,384.00,350.00 2020-01-13-18-00,384.00,350.00 2020-01-14-00-00,384.00,350.00... (1 Reply)
Discussion started by: Tim2424
1 Replies

2. UNIX for Beginners Questions & Answers

Getting correct port number from process id

Hi All, i am trying to find the Jobss port number(either default port number or any other port number assigned) from the running process id. But it's giving me multiple port numbers when searching with netstat command. Can someone help me in finding the correct port number from the... (3 Replies)
Discussion started by: sravani25
3 Replies

3. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

4. Shell Programming and Scripting

Not showing the correct string...

Scripting GURU, I have written a ksh script to get a number from a file and attempting to input the number into the sendsms output script.. But i am finding that ONCALL_NUMBER is showing as $ONCALL_NUMBER as the output for the TO:$ONCALL_NUMBER@sms.info.com message and not as... (3 Replies)
Discussion started by: Junes
3 Replies

5. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

6. Shell Programming and Scripting

string to number problem

Hi actually what happen i have taken a value from database table and stored in variable and that value is 20100601 000000 but this value is stored as string value in database table so after storing this value in variable a when i did operation on this a variable like a=`expr ${a} +1` i m... (1 Reply)
Discussion started by: aishsimplesweet
1 Replies

7. Solaris

Problem in appending the correct log

Hi All, I have a perl module TrxLog.pm and following are codes in it #!/usr/local/bin/perl package TrxLog; %log_begin=""; %log_end=""; %log_msg=""; %log_start_time=""; %log_end_time=""; $ix=0; @arr_msg=""; if (! -e "TrxLog.txt"){ open (TRX,">TrxLog.txt"); }else{ ... (1 Reply)
Discussion started by: megh
1 Replies

8. Shell Programming and Scripting

checking invoice number not correct

hello, I have the following script to check the invoice number is in order or not. However, it cannot show out the correct information. My expect output show below and I would like to only list out the NOT IN SEQUENCE inovice number (not included "ok") #!/bin/sh start=1 for file_number in... (8 Replies)
Discussion started by: happyv
8 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. UNIX for Dummies Questions & Answers

will reinstalling correct my problem??

about 2 months ago i installed mandrake linux 8.0 however due to my current internet setup (dial up upsteam, and cable downstream) i was never able to get it configured properly so i gave up untill i could change to an all cable isp...well that day is tomorrow!!! what i would like to know is... (1 Reply)
Discussion started by: justchillin
1 Replies
Login or Register to Ask a Question