<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Read Text File, Find Lines Containing a Certain String in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499732#M73500</link>
    <description>&lt;P&gt;Here is another way to handle this.&amp;nbsp; It may be faster, or it may be slower.....I don't know.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);
dt0 = Open(
	"example.txt",
	columns( New Column( "Line", Character, "Nominal" ) ),
	Import Settings(
		End Of Line( CRLF, CR, LF ),
		End Of Field( CSV( 0 ) ),
		Strip Quotes( 1 ),
		Use Apostrophe as Quotation Mark( 0 ),
		Use Regional Settings( 0 ),
		Scan Whole File( 1 ),
		Treat empty columns as numeric( 0 ),
		CompressNumericColumns( 0 ),
		CompressCharacterColumns( 0 ),
		CompressAllowListCheck( 0 ),
		Labels( 0 ),
		Column Names Start( 1 ),
		Data Starts( 1 ),
		Lines To Read( "All" ),
		Year Rule( "20xx" )
	)
);

pass_qty_row_array = dt0 &amp;lt;&amp;lt; get rows where(contains( :file_content, "Pass") );&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 03 Jun 2022 21:53:06 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2022-06-03T21:53:06Z</dc:date>
    <item>
      <title>Read Text File, Find Lines Containing a Certain String</title>
      <link>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499621#M73496</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a script I have that works. I want to know if there is a better way to do the same thing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I first use "load text file" to read a text file into the string variable "file_text1".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Next, I use "words" with "\!n" to separate the lines and load them into the string array "file_text2".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Next, I then load the "file_text2" into a data table. I then use the "get rows where" combined with "contains" to find the line number in the original text file that contains a certain string.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a function equivalent to "get rows where" but usable for string arrays? This way, I can skip the portion to load into data table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;file_text1 = load text file( "example.txt" );
file_text2 = words(file_text1 , "\!n");
dt0 = new table("file_dt", new column("file_content", character) );
	dt0:file_content &amp;lt;&amp;lt; set values(file_text2);

pass_qty_row_array = dt0 &amp;lt;&amp;lt; get rows where(contains( :file_content, "Pass") );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jun 2023 17:00:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499621#M73496</guid>
      <dc:creator>LaserGuy</dc:creator>
      <dc:date>2023-06-09T17:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: Read Text File, Find Lines Containing a Certain String</title>
      <link>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499732#M73500</link>
      <description>&lt;P&gt;Here is another way to handle this.&amp;nbsp; It may be faster, or it may be slower.....I don't know.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);
dt0 = Open(
	"example.txt",
	columns( New Column( "Line", Character, "Nominal" ) ),
	Import Settings(
		End Of Line( CRLF, CR, LF ),
		End Of Field( CSV( 0 ) ),
		Strip Quotes( 1 ),
		Use Apostrophe as Quotation Mark( 0 ),
		Use Regional Settings( 0 ),
		Scan Whole File( 1 ),
		Treat empty columns as numeric( 0 ),
		CompressNumericColumns( 0 ),
		CompressCharacterColumns( 0 ),
		CompressAllowListCheck( 0 ),
		Labels( 0 ),
		Column Names Start( 1 ),
		Data Starts( 1 ),
		Lines To Read( "All" ),
		Year Rule( "20xx" )
	)
);

pass_qty_row_array = dt0 &amp;lt;&amp;lt; get rows where(contains( :file_content, "Pass") );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jun 2022 21:53:06 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499732#M73500</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-06-03T21:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: Read Text File, Find Lines Containing a Certain String</title>
      <link>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499767#M73503</link>
      <description>&lt;P&gt;Personally, I've created a Map(), Filter(), and Reduce() function for problems such as this.&amp;nbsp; Here is an example using the Map() function and on my system is is roughly 50% - 60% faster than creating a table to do the search.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the map function (explicitly globalized):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;::map = Function( {inputs /* list, function */ },
	/* uses a single underscore _ as the wild-card */
	{__i__, __result__, __list__, _, __, __keys__},
	__list__ = Eval( Arg( inputs, 1 ) );
	If( Is List( __list__ ),
		__result__ = {};
		Eval(
			Substitute(
				Expr(
					For( __i__ = 1, __i__ &amp;lt;= __N__, __i__++,
						_ = __list__[__i__];
						__result__[__i__] = __function__
					)
				)
			,
				Expr( __N__ ), N Items( __list__ ),
				Expr( __function__ ), Arg( inputs, 2 )
			);
		);
	,
		Is Associative Array( __list__ ),
		__result__ = [=&amp;gt;];
		__keys__ = __list__ &amp;lt;&amp;lt; Get Keys;
		Eval(
			Substitute(
				Expr(
					For( __i__ = 1, __i__ &amp;lt;= __N__, __i__++,
						__ = __keys__[__i__];
						_ = __list__[__];
						__result__[__] = __function__
					)
				)
			,
				Expr( __N__ ), N Items( __keys__ ),
				Expr( __function__ ), Arg( inputs, 2 )
			)
		)
	);
	__result__
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and here is a comparison of using a table vs. the map function:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
filename = Convert File Path( "$SAMPLE_IMPORT_DATA/UN Malaria 2009.csv", absolute, windows );
result = Load Text File( filename );
Show( result );

file_text2 = Words( result, "\!N" );
N = 10000;
word = "malaria";
s = HP Time();
Summation( i = 1, N,
	dt0 = New Table( "file_dt", New Column( "file_content", character ), Private );
	dt0:file_content &amp;lt;&amp;lt; set values( file_text2 );

	pass_qty_row_array 1 = dt0 &amp;lt;&amp;lt; get rows where( Contains( :file_content, word ) );
	close( dt0, No Save );
	0
);
Show( time 1 = (HP Time() - s) / 1000000 );

s = HP Time();
Summation( i = 1, N,
	pass_qty_row_array 2 = loc( Matrix( ::map({ file_text2, Contains( _, word ) }) ) );
	0
);
Show( time 2 = (HP Time() - s) / 1000000 );&lt;BR /&gt;&lt;BR /&gt;Show( All( pass_qty_row_array 1 == pass_qty_row_array 2 ) );

1 - (time 1 - time 2) / time 1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Aside from defining the map function, using it is usually much cleaner looking in code than any other solution.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jun 2022 03:18:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499767#M73503</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2022-06-04T03:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: Read Text File, Find Lines Containing a Certain String</title>
      <link>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499843#M73507</link>
      <description>&lt;P&gt;There is also For Each, Filter Each (and Transform Each) which can help with cases like this and it should be fairly fast.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

file_txt = Load Text File("$SAMPLE_IMPORT_DATA/Animals.txt");
file_list = words(file_txt, "\!n");
search_word = "fall";

pass_qty_row_array = [];
For Each({line, idx}, file_text2,
	If(Contains(line, search_word),
		Insert Into(pass_qty_row_array, idx);
	);
);
show(pass_qty_row_array);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jun 2022 06:52:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/499843#M73507</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-06-04T06:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Read Text File, Find Lines Containing a Certain String</title>
      <link>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/504688#M73640</link>
      <description>&lt;P&gt;Thank you everyone and especially jthi.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have determined that using a for-loop on N Items(file_text2) was faster than using a data table. And then I saw jthi's method using "For Each", which from time measurements is even faster.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 22:12:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Read-Text-File-Find-Lines-Containing-a-Certain-String/m-p/504688#M73640</guid>
      <dc:creator>LaserGuy</dc:creator>
      <dc:date>2022-06-09T22:12:00Z</dc:date>
    </item>
  </channel>
</rss>

