Subscribe Bookmark RSS Feed

Count number of lines in a string

vkessler

Community Trekker

Joined:

Dec 23, 2015

Hi,

this is maybe a noob-question, but i´m not an expert in programming and i couldn´t come up with a solution.

Given is a text file, which i imported with "Load Text File" and saved as a string. The string has many lines and looks like this:

"lorem ipsum lorem ipsum

lorem ipsum

lorem ipsum

lorem ipsum lorem ipsum"

Now, i want to count the number of lines or carriage returns within the string. In Java i would use some next iterator within a while loop, but i couldn´t help myself within jmp.

Do you have a solution for me?  Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

There are several ways to do this with a loop. Below is an example using the Words() function that does not require a loop that would work well for at least smaller amounts of text.

t = Load Text File("text.txt");

n_rows = N Items(Words(t, "\!n"));

6 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

Here is one way to get the number of lines.  It first reads the txt file as a data table, letting JMP figure out the lines, etc.  The open code was captured after reading in the txt file using the previewer, and then once it was read in I just cut and pasted the Source code from the created data table.

 

Names Default to Here(1);

file="C:\Users\...........xxx.txt";

 

dt=Open(

       file,

       columns( Column( "c000001", Character, "Nominal" ) ),

       Import Settings(

              End Of Line( CRLF, CR, LF ),

              End Of Field( CSV( 0 ) ), 

              Strip Quotes( 1 ),

              Use Apostrophe as Quotation Mark( 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( 2 ),

              Lines To Read( "All" ),

              Year Rule( "20xx" )

       ) 

);

 

lines=N Rows(dt); 

close(dt,nosave);

 

rr=load text file(file);

 

Jim
Solution

There are several ways to do this with a loop. Below is an example using the Words() function that does not require a loop that would work well for at least smaller amounts of text.

t = Load Text File("text.txt");

n_rows = N Items(Words(t, "\!n"));

vkessler

Community Trekker

Joined:

Dec 23, 2015

Thanks for the quick anwsers. Both examples work just fine!

msharp

Super User

Joined:

Jul 28, 2015

Wouldn't we want: n_rows = N Items(Words(t, "\!n")) - 1; if we are counting the number of carriage returns?  or am I missing something?

ms

Super User

Joined:

Jun 23, 2011

You're missing last line and maybe some more. The Words() function seems to ignore multiple delimiters. Jim's method is safer if there are empty rows in the text file.

Jeff_Perkinson

Community Manager

Joined:

Jun 23, 2011

The Item() and Items() functions are a little-known (perhaps ill-documented, too) functions parallel to the Word() and Words() function. The only difference between them is that Item() and Items() treat multiple, consecutive delimiters individually.

You'll find Item() and Word() and Words() documented in the Character Functions.

-Jeff

-Jeff