Sponsored Content
Top Forums Programming Problem in getting the correct number from the string. Post 302320812 by SamRoj on Friday 29th of May 2009 03:20:29 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
HFSC(8) 							       Linux								   HFSC(8)

NAME
HFSC - Hierarchical Fair Service Curve's control under linux SYNOPSIS
tc qdisc add ... hfsc [ default CLASSID ] tc class add ... hfsc [ [ rt SC ] [ ls SC ] | [ sc SC ] ] [ ul SC ] rt : realtime service curve ls : linkshare service curve sc : rt+ls service curve ul : upperlimit service curve o at least one of rt, ls or sc must be specified o ul can only be specified with ls or sc SC := [ [ m1 BPS ] d SEC ] m2 BPS m1 : slope of the first segment d : x-coordinate of intersection m2 : slope of the second segment SC := [ [ umax BYTE ] dmax SEC ] rate BPS umax : maximum unit of work dmax : maximum delay rate : rate For description of BYTE, BPS and SEC - please see UNITS section of tc(8). DESCRIPTION (qdisc) HFSC qdisc has only one optional parameter - default. CLASSID specifies the minor part of the default classid, where packets not classified by other means (e.g. u32 filter, CLASSIFY target of iptables) will be enqueued. If default is not specified, unclassified packets will be dropped. DESCRIPTION (class) HFSC class is used to create a class hierarchy for HFSC scheduler. For explanation of the algorithm, and the meaning behind rt, ls, sc and ul service curves - please refer to tc-hfsc(7). As you can see in SYNOPSIS, service curve (SC) can be specified in two ways. Either as maximum delay for certain amount of work, or as a bandwidth assigned for certain amount of time. Obviously, m1 is simply umax/dmax. Both m2 and rate are mandatory. If you omit other parameters, you will specify linear service curve. SEE ALSO
tc(8), tc-hfsc(7), tc-stab(8) Please direct bugreports and patches to: <netdev@vger.kernel.org> AUTHOR
Manpage created by Michal Soltys (soltys@ziu.info) iproute2 31 October 2011 HFSC(8)
All times are GMT -4. The time now is 02:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy