question about keeping space with split

B

Brian

Hello everyone,
Here I have a question

I use split to deal with the following data 60,,,74,0,,62,0,,42
some spaces exist between commas.

while (<INPUT>)
{
chomp;
my @array = split/,/;
}

In the @array the spaces between two comma change to 0. But I still
want to save the space distintively in the array other than a 0,
because I want to do some operation on the space in the @array late.
For instance
if ($fields[$num]== space ){
$fields[$num]=100;
}

Could someone help me about it? Thanks alot.
Brian
 
J

John W. Krahn

Brian said:
Here I have a question

I use split to deal with the following data 60,,,74,0,,62,0,,42
some spaces exist between commas.

while (<INPUT>)
{
chomp;
my @array = split/,/;
}

In the @array the spaces between two comma change to 0.

perl doesn't do that so you must be doing it yourself.
But I still
want to save the space distintively in the array other than a 0,
because I want to do some operation on the space in the @array late.

Your example doesn't have spaces between the commas. Two commas next to
each other would result in a zero-length string. If you use a
non-numeric string in a numerical expression it will be evaluated as 0.
For instance
if ($fields[$num]== space ){
^^^^^^^^
You probably meant:

if ($fields[$num] eq '' ){

You can't compare strings with numerical comparison operators.
$fields[$num]=100;
}


John
 
T

Tad McClellan

Brian said:
I use split to deal with the following data 60,,,74,0,,62,0,,42
some spaces exist between commas.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Then you should show data that has some spaces between commas.

There are no spaces between commas in the data you've shown.

while (<INPUT>)
{
chomp;
my @array = split/,/;
}

In the @array the spaces between two comma change to 0.


No they don't.


-------------------------------
#!/usr/bin/perl
use warnings;
use strict;

while (<DATA>)
{
chomp;
my @array = split/,/;

print "[$_]\n" for @array;
}

__DATA__
60,,,74,0,,62,0,,42
-------------------------------

output:
[60]
[]
[]
[74]
[0]
[]
[62]
[0]
[]
[42]

But I still
want to save the space distintively in the array other than a 0,


There are neither spaces nor "added" zeros in any of the fields.

Could someone help me about it?


Help you with what?

As far as we can see the problem you describe does not exist.

If we could duplicate the problem we could help solve the problem.

But we can't, so we can't.


Post a short and complete program that we can run that illustrates
your problem, and we will help you fix the problem.

Have you seen the Posting Guidelines that are posted here frequently?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top