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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tabs spaces and \n in an array

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
I have the following subroutine I am building:

Code:
sub TicketTable
{ # Start of Ticket Table generation subroutine
	##########################################################################################
	#
	# Read passed array references, de-reference them and scope subroutine variables
	#
	##########################################################################################
	my $combin_ref = shift;
	my $tic_vars_ref = shift;
	my @sub_tic_vars = @$tic_vars_ref;
	my @sub_combin =  @$combin_ref;
	##########################################################################################
	open (ALI, ">C:\\stage\\temp\\sub_out.txt");
	print ALI "@sub_tic_vars\n";
	chomp @sub_tic_vars;
	my ($sub_tier, $sub_sev_code, $sub_type, $sub_close_month, $sub_open_month, $sub_close_year, $sub_open_year, $sub_rpt_month, $sub_rpt_yr,$sub_res_anal_code) = split (/,/,@sub_tic_vars);
	print LOG "$sub_tier\t$sub_sev_code\t$sub_type\n";

Heres the problem. When I look at the output of the:

print ALI "@sub_tic_vars\n";

I see this:

Code:
 Tier 1,	warning, system,12,12,2004,2004,02,2005,
					Performance Management
  Tier 1,	warning, system,12,12,2004,2004,02,2005,
					Performance Management
  Tier 1,	normal, system,12,12,2004,2004,02,2005,
					Performance Management
  Tier 1,	warning, system,12,12,2004,2004,02,2005,
					Customer Changes
  Tier 1,	normal, network,12,12,2004,2004,02,2005,
					Reboot / Reset
  Tier 1,	critical, system,12,12,2004,2004,02,2005,
					Customer Changes

And when I look at it in my editor with tabs, spaces etc. turned on, I see many spaces, tabs and even a character return in each element as you can see above.

When I look at the output of the:

#print LOG "$sub_tier\t$sub_sev_code\t$sub_type\n";

after the split, I see this:

Code:
415		
415		
415		
415		
415		
415		
415		
415		
415		
415

I believe 415 is the size of the @sub_tic_vars array.

My split is not working, yet it looks fine to me. I have been having fits with splitting in this program. I have two questions.

1) will tabs spaces and carrage returns in array elements mess up a split or any other operation on that array.

2) if so, whats an easy way to get rid of them or make them not occur in the first place as the array is built from a flat file query like this (example of one):

Code:
KEY_CHAR,NAME,STARTPRGN,STOP
CM00000009762,ROUSE Closed,09-10-2003-16:26:54,09-10-2003-16:27:29
CM00000009762,ROUSE Complete,09-08-2003-16:04:10,09-10-2003-16:26:55
CM00000009762,ROUSE Desktop IMAC SLA,09-08-2003-14:43:29,09-10-2003-16:26:55
CM00000009762,ROUSE Open,09-08-2003-14:43:27,09-08-2003-14:43:53
CM00000009762,ROUSE Work in Progress,09-08-2003-14:43:53,09-08-2003-14:43:55
CM00000009762,ROUSE Work in Progress,09-08-2003-14:43:56,09-08-2003-14:44:43
CM00000009762,ROUSE Work in Progress,09-08-2003-14:44:44,09-08-2003-14:46:04
CM00000009762,ROUSE Work in Progress,09-08-2003-14:46:05,09-08-2003-16:03:00
CM00000009762,ROUSE Work in Progress,09-08-2003-16:03:01,09-08-2003-16:03:03
CM00000009762,ROUSE Work in Progress,09-08-2003-16:03:04,09-08-2003-16:03:08
CM00000009762,ROUSE Work in Progress,09-08-2003-16:03:09,09-08-2003-16:03:45
CM00000009762,ROUSE Work in Progress,09-08-2003-16:03:45,09-08-2003-16:03:47
CM00000009762,ROUSE Work in Progress,09-08-2003-16:03:48,09-08-2003-16:03:49
CM00000009762,ROUSE Work in Progress,09-08-2003-16:03:50,09-08-2003-16:04:10
CM00000009762,ROUSE Work in Progress,09-08-2003-14:43:49,09-08-2003-14:43:51
CM00000009762,ROUSE dispatch,09-08-2003-16:03:45,09-08-2003-16:03:47
CM00000009762,ROUSE dispatch,09-08-2003-16:03:48,09-08-2003-16:03:50
CM00000009762,ROUSE dispatch,09-08-2003-16:03:09,09-08-2003-16:03:45
CM00000009762,ROUSE dispatch Work in Progress,09-08-2003-16:03:45,09-08-2003-16:03:47
CM00000009762,ROUSE dispatch Work in Progress,09-08-2003-16:03:48,09-08-2003-16:03:50
CM00000009762,ROUSE dispatch Work in Progress,09-08-2003-16:03:09,09-08-2003-16:03:45
CM00000009762,ROUSE post impl review,09-08-2003-16:04:10,09-10-2003-16:26:53

Thanks in advance,

Nick

I got a Biz Degree! How the h*ll did I get here?
 
Heres a better example of the flat file I am reading:

Code:
COMPANY,LOGICAL_NAME,NUMBERPRGN,ASSIGNMENT,UIS_ELAPSED_START_T,CLOSE_TIME,RES_ANAL_CODE,USER_PRIORITY,SEVERITY_CODE
ROUSE,TERMSERV2.hq.us.na.trc,IM00012266761,NCCBB-T1-TRC,06-17-2004-07:05:23,06-17-2004-07:07:21,Cancel,Medium,2
ROUSE,TERMSERV51.hq.us.na.trc,IM00012266765,NCCBB-T1-TRC,06-17-2004-07:06:42,06-17-2004-07:08:08,Cancel,Medium,3
ROUSE,TERMSERV2.hq.us.na.trc,IM00012266759,NCCBB-T1-TRC,06-17-2004-07:05:02,06-17-2004-07:06:36,Cancel,Medium,3
ROUSE,TERMSERV4.hq.us.na.trc,IM00012266758,NCCBB-T1-TRC,06-17-2004-07:04:40,06-17-2004-07:05:46,Cancel,Medium,3
ROUSE,ROUSE/C392C01,IM00012267081,ROUSE- Service Desk,06-17-2004-10:37:39,06-17-2004-10:40:18,Password Reset / Unlock,,3
ROUSE,INET4.hq.us.na.trc,IM00012267037,SMCBB-T1-TRC,06-17-2004-09:48:25,06-18-2004-18:52:51,Performance Management,Medium,2
ROUSE,,IM00012267704,ROUSE- Service Desk,06-17-2004-12:33:28,06-17-2004-12:36:08,Password Reset / Unlock,,3
ROUSE,ROUSE/51N3011,IM00012267572,ROUSE- Technicians,06-17-2004-12:15:55,06-17-2004-14:06:40,Technical Advise / How-To,,3
ROUSE,,IM00012267618,ROUSE- Service Desk,06-17-2004-12:21:35,06-17-2004-12:24:11,Password Reset / Unlock,,3
ROUSE,CAT1.hq.us.na.trc,IM00012267512,SMCBB-T1-TRC,06-17-2004-12:06:17,06-17-2004-12:08:23,Cancel,Medium,3
ROUSE,ROUSE/1HZ7F11,IM00012267332,ROUSE- Service Desk,06-17-2004-11:34:34,06-17-2004-11:43:01,Password Reset / Unlock,,2
ROUSE,MIZNERPARKNAS.rmt.us.na.trc,IM00012268485,SMCBB-T1-TRC,06-17-2004-14:07:06,06-17-2004-15:14:40,Performance Management,Medium,2
ROUSE,,IM00012268372,ROUSE- Technicians,06-17-2004-13:58:24,06-18-2004-15:28:39,Technical Advise / How-To,,3
ROUSE,COLUMBIANAS.rmt.us.na.trc,IM00012268105,SMCBB-T1-TRC,06-17-2004-13:38:30,06-17-2004-15:13:42,Performance Management,Medium,2
ROUSE,,IM00012267991,ROUSE- Service Desk,06-17-2004-13:08:02,06-17-2004-13:11:09,Password Reset / Unlock,,2

I got a Biz Degree! How the h*ll did I get here?
 
You're splitting an array, not a scalar
--Paul

cigless ...
 
Paul,

Can you elaborate some? Whats the difference between splitting a scalar and splitting an array? I've split arrays before with :
my ($sub_tier, $sub_sev_code, $sub_type, $sub_close_month, $sub_open_month, $sub_close_year, $sub_open_year, $sub_rpt_month, $sub_rpt_yr,$sub_res_anal_code) = split (/,/,@sub_tic_vars);

and it workd fine!

Nick

I got a Biz Degree! How the h*ll did I get here?
 
Dunno what the sperator is for an array by default, I usually use a foreach to get all the elements of an array, and split for a scalar, or single line

Just the way I've done it

IBOB (I've Beer on Board) and I'm not on a boat, I'll have closer look in the morning

Cheers
--paul

cigless ...
 
Thanks Paul...a little IBOB for me too! hehe.

see ya manyana.

nick

I got a Biz Degree! How the h*ll did I get here?
 
I've never seen this done before:

split (/,/,@sub_tic_vars);

so I did a test, what it returns is the number of lines in the array thats in the split argument.
 
Using an array as your EXPR to split will almost definitely not do what you want. @sub_tic_vars is being evaluated in a scalar context, which means that it will return the number of elements in it. `split' then tries to split that number on the comma character. Since the comma doesn't appear in the number returned, `split' returns that number.

 
Thanks folks. What a stupid mistake. I can't believe I did not see it. I used the format that works:


while (@sub_tic_vars) {
my ($var1, $var2, var3) = split (/,/);
}

Question:

How many hours of coding for how many weeks does it take to take hours to figure out a stupid easy mistake like this.

I've been doing 60 hours for about 2 months now on this project.

Nick

[3eyes]

I got a Biz Degree! How the h*ll did I get here?
 
The more hours you spend, the fewer silly mistakes you tend to make. As far as I'm concerned, it'd down to experience and good learning materials. After messing around for ages with no proper learning, I read ``Programming Perl'' cover-to-cover while travelling for work.

Learned more in two weeks than I ever had before.

Of course the worst silly mistakes are the ones that *seem* to work ok until, inevitably, it all goes horribly wrong when you're in the middle of demoing your system!
 
Another thing when you've got problems in your code is to go away, and sneak back up on it in a totally dispassionate mode, and pretend someone else wrote the code.

Or better again is get a peer to review your code, on the grounds that you review theirs - again dispassionately

You can both learn from each other, and it's much easier to spot errors in someone elses code

Sometimes schizophrenia can have its uses ;)

--Paul

cigless ...
 
Point taken ishnid. I have all the Perl O'Reilly books but have truly only read Deitel, How to Program Perl cover to cover. When I was beginning, The O'Reilly Programming was too far over my head. As soon as this job is done, I am will take your advice and read Programming.

BTW... to my original question on this post, what about the tabs spaces and carrage returns in my arrays?

Nick

I got a Biz Degree! How the h*ll did I get here?
 
nick said:
1) will tabs spaces and carrage returns in array elements mess up a split or any other operation on that array.

2) if so, whats an easy way to get rid of them or make them not occur in the first place as the array is built from a flat file query like this (example of one):
1. No, not if you're not splitting on those characters
2. Substitution
Code:
$string=~ s/\t//g;
$string=~ s/\r\n//g; #on windows
$string=~ s/\n//g;   #newline characters, NOT recommended unless it's exactly what you need.

HTH
--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top