How to String Manipulation with VarGetText(), StrReplace()

Hello I am back 10 years later :cold_sweat:

/* Tomasz wrote this example for someone  22/07/2007   http://tinyurl.com/ybsvows5
fgets - this function reads entire LINE into string variable. You can read all lines using simple while loop and feof()
*/
 
fh = fopen("filename", "r" );
 
line = 0;
while( ! feof( fh ) )
{
  VarSetText("line"+(line++), fgets( fh ));
}
 
fclose( fh );
 
// now you have all lines in lineNNN variables
// manipulate them here and then write to the SAME file
 
fh = fopen("filename", "w" );
 
for( i = 0; i < line; i++ )
{
 fputs( VarGetText("line"+i, fh );
}
 
fclose( fh );





for( i = 0; i < line; i++ )
{
    lineGet = VarGetText( "line" + i );
    //printf( "\nlineGet " + lineGet );

    if( i == 0 )
    {
        _TRACE( "Before lineGet " + i + ",  " + lineGet );
         VarSetText( "line" + 0, StrReplace( lineGet, "ticker", "$FORMAT Ticker" ) );
         VarSetText( "line" + 0, StrReplace( lineGet, "<", "" ) );
         VarSetText( "line" + 0, StrReplace( lineGet, ">", "" ) );
        
        lineGet = VarGetText( "line" + i );
        _TRACE( "After lineGet " + i + ",  " + lineGet );


    }
}

My question now is, in the very first line of the Txt looks like this
<ticker>,<per>,<date>,<high>,<low>,<close>,<open>,<vol>

How can we make a string manipulation ONLY in the first line of the text?
For the above example i wrote this code, but the only ONE line of the code can be executed is with “$FORMAT Ticker”.

What I am missing here and I the first line canot be replaced correctly

thank you

// now you have all lines in lineNNN variables
for( i = 0; i < line; i++ )
{
    lineGet = VarGetText( "line" + i );
    //printf( "\nlineGet " + lineGet );

    if( i == 0 )
    {
        _TRACE( "Before lineGet " + i + ",  " + lineGet );
         VarSetText( "line" + 0, StrReplace( lineGet, "ticker", "$FORMAT Ticker" ) );
         VarSetText( "line" + 0, StrReplace( lineGet, "<", "" ) );
         VarSetText( "line" + 0, StrReplace( lineGet, ">", "" ) );
        
        lineGet = VarGetText( "line" + i );
        _TRACE( "After lineGet " + i + ",  " + lineGet );


    }
}

Sorry @Tomasz. I wrote my test code twice,
Please. Can you delete my «test» of the Top one (your code) if you have time, otherwise people there are going to confuse. Sorry about that.

Next time I have to open the posting window Big enough, so I can read my entire post

1 Like

This one should work.
Note: If you do things just once then use triggers!

/// @link http://forum.amibroker.com/t/how-to-string-manipulation-with-vargettext-strreplace/892
if( ParamTrigger( "CHANGE FILE CONTENT", "CLICK HERE" ) )
{
	path = "C:\\YOUR_FILE.csv";

	fh = fopen( path, "r" );

	line = 0;
	while( ! feof( fh ) )
	{
		VarSetText("line"+(line++), fgets( fh ));
	}

	fclose( fh );

	
	// REPLACING HEADER START ################################################	
	header = StrReplace( VarGetText( "line0" ), "ticker", "$FORMAT Ticker" );
	header = StrReplace( header, "<", "" );
	header = StrReplace( header, ">", "" );

	VarSetText( "line0", header );	
	// REPLACING HEADER END ##################################################


	// now you have all lines in lineNNN variables
	/// manipulate them here and then write to the SAME file
	fh = fopen( path, "w" );
	
	for( i = 0; i < line; i++ )
	{
		fputs( VarGetText("line"+i ), fh );
	}

	fclose( fh );
}
2 Likes

Thanks fxshrat
So now the BACTH works perfect.
Every website has different Formats.
I collect from 3-4 different sites the data and I store all of them in ONE file with different format,
BACTH can recognise different formats, and job done with just a button click.

btw i did this Function to cover most of the format cases, plus the Lowcase fonts



// REPLACING HEADER START ################################################	
function ReplaceHeader( FileToRead, fileToSave ) // Read URL or ASCII, Convert and Save
{
    header = FileToRead; // read a line of text
    header = StrToUpper( header ); // turn fonts to Upper case
    header = StrReplace( header, "<TICKER>", "$FORMAT Ticker" );
    header = StrReplace( header, "<PER>", "Skip" );
    header = StrReplace( header, "<DATE>", "Date_MDY" );
    header = StrReplace( header, "<DTYYYYMMDD>", "Date_YMD" );
    header = StrReplace( header, "<TIME>", "Time" );
    header = StrReplace( header, "<OPEN>", "Open" );
    header = StrReplace( header, "<HIGH>", "High" );
    header = StrReplace( header, "<LOW>", "Low" );
    header = StrReplace( header, "<CLOSE>", "Close" );
    header = StrReplace( header, "<VOL>", "Volume" );
    header = StrReplace( header, "<OPENINT>", "OPENINT" );
    return fputs( header, fileTosave );
}
// REPLACING HEADER END ##################################################


How can I get the tickers Name() from Watchlist 0 and transform them to a hyphenated string?
Here is what I have so far..
Thanks in Advance.

listnum = 0; // Using the existing symbols in Watchlist 0
hyphen = "-"; // ASCII "&#45"
_Name = Name(); //Ticker Symbol
_NewName = "h-y-p-h-e-n-a-t-e-d ticker symbol"

tickers = _Name;

"The last item is " + StrExtract( tickers, -1 ); //How do I extract the ticker symbol string Name(), tokenize it into an array, and output as a H-y-p-h-e-n-a-t-e-d string?
//////////////////
//VarGetText? tickers;
//StrReplace? Not sure how...
//VarSetText?_NewName
/////////////////////////
printf("listing from the end of the list:n");

for( item = -1; ( sym = StrExtract( tickers, item ) ) != ""; item-- )
{
  printf( sym + "n" );
}

AddTextColumn(_NewName,"_NewName"); //Result = H-y-p-h-e-n-a-t-e-d string of Name();
Thanks in Advance

One of many possibilities:

str = "testing";

for( i = 1, output = StrMid( str, 0, 1 ); i < StrLen( str ); i++ )
{
  output += "-" + StrMid( str, i, 1 );
}

printf("%s", output ); 
1 Like

TJ,
Thank you! You are an ace!
MrV