 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Oren Halvani Guest
|
Posted: Fri Sep 15, 2006 1:43 pm Post subject: Finding a string at the end of a line via: Line.Pos( string |
|
|
dear builders,
my programm has to find given string at several postions in a line...
the below for( ) loop doesn't work for strings which should be
found if they occur at the END of the line...but it
works fine for string that occur at the beginning or
at a given position....
has someone a suggestion for me on this ??
Oren
/******************************************************/
for(int x = 0; x < Original->Count; ++x)
{
AnsiString line,
line_pure = Original->Strings[x],
line_and_number = "[ Line: " + IntToStr(x + 1) + " ] " + line_pure;
int diff = line_pure.Length() - Pattern.Length(), idx;
if(CaseSensitive) idx = line_pure.Pos(Pattern);
else idx = CaseInsensitivePos(line_pure, Pattern);
/******** Prepare 'line' before the Add() *******/
if(chkUseLineNumbers) line = line_and_number;
else line = line_pure;
/******** Add the results...*******/
if(chkLineBegin) {if(idx == 1) Results->Add(line);} // works !
if(chkLineEnd) {if(idx == (diff +1)) Results->Add(line);} // DOESN'T WORK !!
if(chkTextPos) {if(idx == txtPos) Results->Add(line);} // works ! |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Sep 15, 2006 3:06 pm Post subject: Re: Finding a string at the end of a line via: Line.Pos( str |
|
|
"Oren Halvani" <Spam (AT) halvani (DOT) de> wrote in message
news:450a67bb$1 (AT) newsgroups (DOT) borland.com...
| Quote: | the below for( ) loop doesn't work for strings which should be
found if they occur at the END of the line...
|
It works fine for me when I try your code as-is. Why do you thing that it
does not work for you? What are the actual values of idx and diff when you
try it? Are you using Pos() or CaseInsensitivePos() when it fails?
Gambit |
|
| Back to top |
|
 |
Clayton Arends Guest
|
Posted: Fri Sep 15, 2006 3:15 pm Post subject: Re: Finding a string at the end of a line via: Line.Pos( str |
|
|
"Oren Halvani" <Spam (AT) halvani (DOT) de> wrote in message
news:450a67bb$1 (AT) newsgroups (DOT) borland.com...
| Quote: | if(chkLineEnd) {if(idx == (diff +1))
Results->Add(line);} // DOESN'T WORK !!
|
The comparison should just be against "diff" not "diff + 1". Let's assume
that line_pure contains the string "this is a test" (14 length) and that
Pattern is "test" (4 length). 'diff' will equal 10 (14 - 4). 'idx' will
equal 10 however you are comparing for diff + 1 (11).
- Clayton |
|
| Back to top |
|
 |
Oren Halvani Guest
|
Posted: Fri Sep 15, 2006 9:22 pm Post subject: Re: Finding a string at the end of a line via: Line.Pos( str |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> schrieb im
Newsbeitrag news:450a7bd0$1 (AT) newsgroups (DOT) borland.com...
| Quote: | It works fine for me when I try your code as-is. Why do you thing that it
does not work for you? What are the actual values of idx and diff when you
try it? Are you using Pos() or CaseInsensitivePos() when it fails?
Gambit
|
| Quote: | Why do you thing that it does not work for you?
|
because it doesn't work ! i don't get any results if i look for example this:
// Text to filter, the Pattern is: "x"
-------------------------------------------------------------
xxx am Anfang !
Auch xxx
Am xxx
-------------------------------------------------------------
why ?? the code i've posted works well for anything else, why not
for LINE-END ?
with this code i get the following values for idx:
/****************************************************************************/
int __fastcall TfrmMainUnit::FilterResult(const AnsiString &FileName,
const AnsiString &Pattern,
bool CaseSensitive,
TStrings *Results)
{
std::auto_ptr<TStringList> Original(new TStringList); Original->LoadFromFile(FileName);
bool chkLineBegin = frmFilterOptions->chkText_LineBegin->Checked,
chkLineEnd = frmFilterOptions->chkText_LineEnd->Checked,
chkTextPos = frmFilterOptions->chkText_Pos->Checked,
chkHowMuch = frmFilterOptions->chkHowMuch->Checked,
chkSeparator = frmOptions->chkSeparator->Checked,
chkUseLineNumbers = frmOptions->chkUseLineNumbers->Checked;
int BoxSelect = frmFilterOptions->boxDelOptions->ItemIndex,
HowMuch = StrToInt(frmFilterOptions->txtHowMuch->Text),
txtPos = StrToInt(frmFilterOptions->txtPos->Text);
String file_sep = frmOptions->txtSeperator->Text + "( " + FileName + " )" + frmOptions->txtSeperator->Text;
FCancelled = false; ::GetAsyncKeyState(VK_ESCAPE);
/************************************************/
if(chkSeparator) {Results->Add(file_sep);}
/************************************************/
Results->BeginUpdate();
try
{
for(int x = 0; x < Original->Count; ++x)
{
if(IsCancelled()) break; // Loop verlassen..
AnsiString line,
line_pure = Original->Strings[x],
line_and_number = "[ Line: " + IntToStr(x + 1) + " ] " + line_pure;
int diff = line_pure.Length() - Pattern.Length(), idx;
if(CaseSensitive) idx = line_pure.Pos(Pattern);
else idx = CaseInsensitivePos(line_pure, Pattern);
/******** Prepare 'line' before the Add() *******/
if(chkUseLineNumbers) line = line_and_number;
else line = line_pure;
/**** keep or filter out ?? ********/
if(BoxSelect == 0)
{
if(chkLineBegin) {if(idx == 1) Results->Add(line);}
if(chkLineEnd) {if(idx == (diff)) Results->Add(line);}
if(chkTextPos) {if(idx == txtPos) Results->Add(line);}
if(chkHowMuch && HowMuch > 1)
{
if(HowMany(line, Pattern, HowMuch) == HowMuch) Results->Add(line);
}
}
if(BoxSelect == 1)
{
if(chkLineBegin) {if(idx != 1) Results->Add(line);}
if(chkLineEnd) {if(idx != (diff + 1)) Results->Add(line);}
if(chkTextPos) {if(idx != txtPos) Results->Add(line);}
if(chkHowMuch && HowMuch > 1)
{
if(HowMany(line, Pattern, HowMuch) != HowMuch) Results->Add(line);
}
}
// if the user haven't made special filter options...
if(!chkLineBegin && !chkLineEnd && !chkTextPos && !chkHowMuch) {if(idx != 0) Results->Add(line);}
}
}
__finally
{
Results->EndUpdate();
}
return Original->Count;
}
/****************************************************************************/
these values:
idx = 1
idx = 7
idx = 4
can you please explain me whats wrong with the code ?
Oren |
|
| Back to top |
|
 |
Oren Halvani Guest
|
Posted: Fri Sep 15, 2006 9:24 pm Post subject: Re: Finding a string at the end of a line via: Line.Pos( str |
|
|
"Clayton Arends" <nospam_claytonarends (AT) hotmail (DOT) com> schrieb im Newsbeitrag news:450a7d35$1 (AT) newsgroups (DOT) borland.com...
| Quote: | The comparison should just be against "diff" not "diff + 1". Let's assume that line_pure contains the string "this is a test" (14
length) and that Pattern is "test" (4 length). 'diff' will equal 10 (14 - 4). 'idx' will equal 10 however you are comparing for
diff + 1 (11).
- Clayton
|
Hi Clayton,
well i changed (diff +1) to (diff) bu still...No Results !!
look on the entire code that i've posted to Remy...
Oren |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sat Sep 16, 2006 1:08 am Post subject: Re: Finding a string at the end of a line via: Line.Pos( str |
|
|
"Oren Halvani" <Spam (AT) halvani (DOT) de> wrote in message
news:450ad368 (AT) newsgroups (DOT) borland.com...
| Quote: | i don't get any results if i look for example this:
// Text to filter, the Pattern is: "x"
-------------------------------------------------------------
xxx am Anfang !
Auch xxx
Am xxx
-------------------------------------------------------------
why ??
|
Look VERY carefully at the text you have shown. Pos() returns the starting
index of the matching pattern. Since you are looking for a 1-character
pattern, Pos() (or CaseInsensitivePos()) is going to return 7 and 4 for the
second and third lines, respectively. But your code that is looking for an
end-of-line pattern is expecting 9 and 6 instead. That is why your code is
failing.
You are not comparing the pattern correctly in the first place. Try the
following code instead:
bool __fastcall IsPattern(const AnsiString &Str, int Index, const
AnsiString &Pattern, bool CaseSensitive)
{
AnsiString Piece = Str.SubString(Index, Pattern.Length());
if( CaseSensitive )
return (Piece.AnsiCompare(Pattern) == 0);
else
return (Piece.AnsiCompareIC(Pattern) == 0);
}
for(int x = 0; x < Original->Count; ++x)
{
AnsiString line_pure = Original->Strings[x],
AnsiString line_and_number = "[ Line: " + IntToStr(x + 1) + " ] "
+ line_pure;
AnsiString line;
if( chkUseLineNumbers )
line = line_and_number;
else
line = line_pure;
if( chkLineBegin )
{
if( IsPattern(line_pure, 1, Pattern, CaseSensitive) )
Results->Add(line);
}
if( chkLineEnd )
{
if( IsPattern(line_pure, line_pure.Length()-Pattern.Length()+1,
Pattern, CaseSensitive) )
Results->Add(line);
}
if( chkTextPos )
{
if( IsPattern(line_pure, txtPos, Pattern, CaseSensitive) )
Results->Add(line);
}
}
Gambit |
|
| Back to top |
|
 |
Oren Halvani Guest
|
Posted: Sat Sep 16, 2006 2:35 am Post subject: Re: Finding a string at the end of a line via: Line.Pos( str |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> schrieb im
Newsbeitrag news:450b08c8$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Look VERY carefully at the text you have shown. Pos() returns the starting
index of the matching pattern. Since you are looking for a 1-character
pattern, Pos() (or CaseInsensitivePos()) is going to return 7 and 4 for the
second and third lines, respectively. But your code that is looking for an
end-of-line pattern is expecting 9 and 6 instead. That is why your code is
failing.
|
thanks alot, i really didn't had a clue why i get these strange values...
| Quote: | You are not comparing the pattern correctly in the first place. Try the
following code instead:
[code...]
Gambit
|
Remy and once again: YOU'VE RESCUED ME !
tanks alot for your great help !!
Oren |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|