Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Blank elements in an array

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,47407
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Blank elements in an array
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Blank elements in an array - 5/22/2007 7:17:23 PM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
I build an array by reading in all the lines of a text file to variable, then using Split on the VBCrLf.

Trouble is if there are blank lines at the end of the text file, these get read in too.  So my array ends up with some blank elements that I need to remove.

Any ideas how?
 
 
Post #: 1
 
 RE: Blank elements in an array - 5/22/2007 7:26:39 PM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
Test the length of each line before adding it to the array.

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to markmcrobie)
 
 
Post #: 2
 
 RE: Blank elements in an array - 5/22/2007 7:36:35 PM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
I did it another way - this is probably not ideal, as I figured it myself!


      

(in reply to ginolard)
 
 
Post #: 3
 
 RE: Blank elements in an array - 5/23/2007 1:36:43 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
If the problem is simply blank lines at the end then use Trim() before you do the split.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to markmcrobie)
 
 
Post #: 4
 
 RE: Blank elements in an array - 5/23/2007 2:17:55 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Doesn't seem to work.  If I have say 10 blank lines at the end of my text file, I get 10 blank MsgBoxs using this code:


      

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Blank elements in an array - 5/23/2007 2:20:00 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
I think the problem is the VbCrLf's in the text file after each blank line.

If I do:

rall = objTextFile.ReadAll
MsgBox rall

the MsgBox has my 2 lines with text, then lots of blank lines.

If I then do

sall=Trim(rall)
MsgBox sall

I get the same - 2 lines of text in the MsgBox, then lots of blank lines

(in reply to markmcrobie)
 
 
Post #: 6
 
 RE: Blank elements in an array - 5/23/2007 2:22:38 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
aah....you can remove all blank lines using a regex if you want. If you need this let me know and I will post an example.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to markmcrobie)
 
 
Post #: 7
 
 RE: Blank elements in an array - 5/23/2007 2:25:39 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Thanks - but would that be any better than what I came up with myself, which was:


      

Thanks.

(in reply to ebgreen)
 
 
Post #: 8
 
 RE: Blank elements in an array - 5/23/2007 2:42:24 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
Ehh...not really. VBScript's Regex engine is just too restrictive. You could do it in Perl but I imagine that doesn't help much.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to markmcrobie)
 
 
Post #: 9
 
 RE: Blank elements in an array - 5/23/2007 2:50:09 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
I guess what I'm getting at is "is there anything wrong with my method, or should I give myself a pat on the back for figuring it out myself?"

;-)

< Message edited by markmcrobie -- 5/23/2007 2:53:01 AM >

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: Blank elements in an array - 5/23/2007 3:10:59 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
definitely worth a pat. There isn't anything wrong with it.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to markmcrobie)
 
 
Post #: 11
 
 RE: Blank elements in an array - 5/23/2007 3:18:00 AM   
  mcds99


Posts: 441
Score: 4
Joined: 2/28/2006
Status: offline
And then there is the old way...


Open the text file and remove the blank lines ;-)

Humor

_____________________________

Sam

Keep it Simple Make it Fun KiSMiF

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: Blank elements in an array - 5/24/2007 4:22:48 AM   
  dm_4ever


Posts: 2723
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Well I've been following this thread and the other one....

http://www.visualbasicscript.com/m_47020/tm.htm
http://www.visualbasicscript.com/m_47407/tm.htm

I figured this, blank elements, was one of the things ehvbs was actually hinting at when he made his original comment.  In general the important thing is to be aware of the pitfalls of doing things in one way or another which ehvbs also mentioned.

The following uses RegEx to attempt to remove any blank spaces in the beginning of a line or trailing blank spaces.


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to markmcrobie)
 
 
Post #: 13
 
 RE: Blank elements in an array - 5/24/2007 10:13:56 AM   
  Skie

 

Posts: 58
Score: 0
Joined: 3/2/2006
Status: offline
mark, your coding is fine so long as you only have blank lines at the end of the file.  If they're anywhere else, your redim is going to drop lines with data and keep blanks.

(in reply to markmcrobie)
 
 
Post #: 14
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Blank elements in an array Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts