#77630 - 2001-04-19 02:52 PM
Array elements after split()
|
Anonymous
Anonymous
Unregistered
|
Hi all, Just wondering if there is a way to address the nth element of a an array after a split() function. like this :$line="aaa bbb ccc ddd" $Record=Split($line," ",-1) $Field=$record[3] Or maybe i should put this in suggestions. Thanks in advance, Salah. ------------------
|
Top
|
|
|
|
#77632 - 2001-04-19 03:11 PM
Re: Array elements after split()
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Yup, there sure is.. you can use the ubound() function to return the largest available subscript (nth index) of an array ...code:
break on $line="aaa bbb ccc ddd"
$record=split($line," ",-1)
$nthfield=$record[ubound($record)]
?"nthfield=$nthfield"
exit
Shawn. [This message has been edited by Shawn (edited 19 April 2001).]
|
Top
|
|
|
|
#77633 - 2001-04-19 04:06 PM
Re: Array elements after split()
|
Anonymous
Anonymous
Unregistered
|
Shawn, Yes it is working, but in my real script $line="aaa bbb cc dddd" so the field separator is variable, and with my previous experience with awk() which is a a Unix command, i was expecting that split() will remove the leading blanks of the fields. So with awk() I'll get $1=aaa $2=bbb $3=cc $4=dddd but split will give 15 Fields, most of them " ". Anyway Thanks for the prompt answer, and it is definetly a suggestion.Salah.
|
Top
|
|
|
|
#77634 - 2001-04-19 04:12 PM
Re: Array elements after split()
|
Anonymous
Anonymous
Unregistered
|
Hi all, In my previous reply, $line="aaa bbb cc dddd" should read $line="aaa***bbb*cc***********dddd" I replaced the blanks with stars. because the bulletinborad edited it.Thanks, Salah.
|
Top
|
|
|
|
#77635 - 2001-04-19 04:58 PM
Re: Array elements after split()
|
cj
MM club member
   
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
Aha...Instead of SPLIT(), try this: SalahSplit.K2K
code:
break on ; your input $line="aaa bbb cc dddd" $array=split($line)
; create Comma Separated List $line2="" for each $thing in $array if $thing<>"" $line2=$line2+$thing+"," endif next
; re-split new line without the last blank entry $array=split(substr($line2, 1, len($line2)-1), ",")
; display results "There are " ubound($array)+1 " items:" ? for $i=0 to ubound($array) $i ": '" $array[$i] "'" ? next
cj
|
Top
|
|
|
|
#77636 - 2001-04-19 05:22 PM
Re: Array elements after split()
|
Anonymous
Anonymous
Unregistered
|
Thanks cj, I works fine, but i would be nice if split does it by design. So for field separators : is equiv ::: is equiv ::::: Maybe Ruud is watching to this thread and will implement it in the final version.Thanks anyway. Salah.
|
Top
|
|
|
|
#77637 - 2001-04-19 07:02 PM
Re: Array elements after split()
|
cj
MM club member
   
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
No, the current SPLIT is correct:Your string "aaa*bbb*cc********dddd" IS "aaa", "bbb", "cc", "", "", "", "", "", "", "", "dddd" and SPLIT must treat it this way. This is good because it allows you to have empty entries in an array. Entries that may be filled later, or may be optional inputs or may be fields from a database... My script stripped the split. (say that 3 times quickly)  cj
|
Top
|
|
|
|
Moderator: ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 509 anonymous users online.
|
|
|