Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best Method for Separating a huge array from delimited text field

Status
Not open for further replies.

dantheinfoman

Programmer
May 5, 2015
131
US
Hi All,

I'm wanting to make it so that I can separate delimited information into my listbox from a single table field.

So lets say the listbox array has 3 columns (it really has 9 in the actual version) and I wanted to cram 3 entries into a field.
"100,BILLY,MADISON;200,DANNY,BOY;300,NEXT,ENTRY"

How can I separate these into 3 records (delimited by a colon or semicolon or pipe etc) and furthermore, to take these three records and separate out the fields that are comma separated?

And would that make it glitchy or slow to manipulate??

Thanks!

Dan
 
You wouldn't separate these into 3 records, you would set rowsourcetype to 1 (value) and set this as rowsource, but replace the semicolons with commas.
The columncount will determine how many of the comma separated values will be put into one item, you have to specify as many column values for each record, as there are columns, and that would be it.

Bye, Olaf.
 
If I am understanding Dan correctly, he wants

"100,BILLY,MADISON;200,DANNY,BOY;300,NEXT,ENTRY"

to look like:

100,BILLY,MADISON
200,DANNY,BOY
300,NEXT,ENTRY

There is a FAQ on this: faq182-6039 showing how to split into an array based on a delimiter:

Code:
DIMENSION OutArray[1] && any size.. will be redimensioned to fit
lnRows = Split(@OutArray, '100,BILLY,MADISON;200,DANNY,BOY;300,NEXT,ENTRY', ';' )
for I = 1 to lnRows
  ? OutArray[I]

next
FUNCTION Split
PARAMETERS paArr, pcLine, pcDiv
PRIVATE lnLen, lnWid, lnCnt, lnNum, lnStrt, lnPos, lcSub
  lnLen = aLen( paArr, 1 ) && Number of rows
  lnWid = aLen( paArr, 2 ) && Number of cols
  
  lnCnt = OCCURS( pcDiv, pcLine+pcDiv )
  if lnLen < lnCnt
    if lnWid > 0
      DIMENSION paArr[ lnCnt, lnWid ]
    else
      DIMENSION paArr[ lnCnt ]
    endif
  endif
  lnStrt = 1
  lnNum  = 0
  do While atc( pcDiv, pcLine+pcDiv, lnNum+1 ) > 0
    lnNum = lnNum + 1
    lnPos = atc( pcDiv, pcLine+pcDiv, lnNum )
    if lnStrt > len(pcLine) && 9/23/99 wgcs
      lcSub = ''
    else
      lcSub = Substr( pcLine, lnStrt, lnPos-lnStrt )
    endif
    lnStrt = lnPos + len(pcDiv)
    if lnWid > 0
      paArr[ lnNum, 1 ] = lcSub
    else
      paArr[ lnNum ] = lcSub
    endif
  enddo
    
RETURN lnCnt


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are not good for you.
 
Actually, it's much simpler than that:

[tt]lcX = "100,BILLY,MADISON;200,DANNY,BOY;300,NEXT,ENTRY"
ALINES(laText, lcX, 0, ";")
[/tt]

This will give you an array (laText) like this:

[tt]100,BILLY,MADISON
200,DANNY,BOY
300,NEXT,ENTRY[/tt]

If necessary, you can then use ALINES() again on each of those three elements to get them into an array containing:

[tt]100
BILLY
MADISON
etc[/tt]

Is that what you want, Dan?

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike, you're right, the Split() was written for an old version of FoxPro BEFORE alines() was around



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are not good for you.
 
the Split() was written for an old version of FoxPro BEFORE alines() was around

I thought that might be the case. In fact, I think ALINES() has always been around, but it was only in VFP 7.0 that it had a parameter that let you specify the delimiter character.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
If ALINES() existed in FP 2.6 or VFP 3.0, it is not shown in my Language Reference book.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
All the splitting is not needed when using the rowsourcetype 1.

Bye, Olaf.
 
All the splitting is not needed when using the rowsourcetype 1.

Yes, but wouldn't that give him a single column listbox, containing 100, BILLY, MADISON, etc on separate lines?

Dan, if I've understood your requirement correctly, I think you need something like this:

Code:
WITH thisform.List1
  .RowSourceType = 0
  .ColumnCount = 3

  lcX = "100,BILLY,MADISON;200,DANNY,BOY;300,NEXT,ENTRY"
  lnCount = ALINES(laText, lcX, 0, ";")
  FOR lnI = 1 TO lnCount
    ALINES(laItems, laText(lnI), 0, ",")
    .AddItem(laItems(1))
    .AddListItem(laItems(2), lnI, 2)
    .AddListItem(laItems(3), lnI, 3)
  ENDFOR 
  
ENDWITH

That would give you a listbox with three columns, with each row representing a different peson.

You would still need to do a bit of tidying to line up the columns, but that's easy enough.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Wow, you guys are all Ninjas! I am so excited to finally test out these, Thanks Olaf and Mike and Griff! Yes, I wanted it to be 3 records (or at least to be able to analyze each one with a SCAN or some sort of loop. Thanks guys!!
Dan
 
Oh, You Jedi Masters [yoda] and Ninjas [fumanchu]!
I wasn't aware you are the peace keepers [lightsaber] and so atrocious killers [swords]


Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
ALINES() was added in VFP 6, and the additional parameter in VFP 7.

Tamar
 
You could also do away with the second call to Alines() and substitute GetWordNum() using the comma as the separator.
 
Mike,

Olaf Doschke said:
All the splitting is not needed when using the rowsourcetype 1.
Mike Lewis said:
Yes, but wouldn't that give him a single column listbox, containing 100, BILLY, MADISON, etc on separate lines?

No, it works as I said above, simply try it, as said semicolons would need conversion to comma, but that's just a CHRTRAN, the rest is simply some settings of the listbox:
Code:
Local lcSource
lcSource = "100,BILLY,MADISON;200,DANNY,BOY;300,NEXT,ENTRY"
lcSource = CHRTRAN(lcSource,";",",")
_screen.addobject("listbox1","listbox")
with _screen.listbox1
   .columncount = 3
   .columnwidths = "100,100,100"
   .rowsourcetype = 1
   .rowsource = lcSource
   .Width = 302
   .Visible = .T.
endwith

Bye, Olaf.
 
Dan, you're right, but ALINES() is much faster than repeated use of GetWordNum().

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top