removing empty split() fields

D

dutone

sample file:
+123n
blah blah blah
+123+
more blah
sasas
assa
+123+

open IN, "data" or die $!;
my @data = <IN>;
my @splitData = split /\+123(?:\+)?/, "@data";
for(@splitData) { print "LINE ** $_"; }

prints:
LINE ** LINE ** n
blah blah blah
LINE **
more blah
sasas
assa
LINE **

now i understand that split returns the number of matched items plus 1
if no limit is given.
so in order to weed the empty elements out will i always have to do
somthing like the following:

my @splitData = grep { $_ and length $_ > 0 and $_ !~ /^\s+$/ }
split /\+123(?:\+)?/, "@data";

or get a count on how many times /\+123(?:\+)?/ matches in @data and
use that for a limit?

thanks
 
G

Gunnar Hjalmarsson

dutone said:
now i understand that split returns the number of matched items
plus 1 if no limit is given.

What's wrong with the description in "perldoc -f split":

"Splits a string into a list of strings and returns that list. By
default, empty leading fields are preserved, and empty trailing ones
are deleted."
so in order to weed the empty elements out will i always have to do
somthing like the following:

my @splitData = grep { $_ and length $_ > 0 and $_ !~ /^\s+$/ }
split /\+123(?:\+)?/, "@data";

or get a count on how many times /\+123(?:\+)?/ matches in @data
and use that for a limit?

It would be easier to help you if you let us know exactly what it is
you are trying to do. I for one do not fully understand.
 
T

Tad McClellan

dutone said:
sample file:
+123n
blah blah blah
+123+
more blah
sasas
assa
+123+

open IN, "data" or die $!;
my @data = <IN>;
my @splitData = split /\+123(?:\+)?/, "@data";
^ ^
^ ^
^ ^
for(@splitData) { print "LINE ** $_"; }

prints:
LINE ** LINE ** n
blah blah blah
LINE **
more blah
sasas
assa
LINE **


Yes...

.... and what is it that you want it to print instead?

Should we all just guess?

I'll assume you don't want the extra space characters at the
beginning of the lines after the first line?

now i understand that split returns the number of matched items plus 1
if no limit is given.


That is not related to your problem in any way.

so in order to weed the empty elements


There are no "empty elements" here. Each element in the list
returned by split() is non-empty (ie. has length()).



You put the extra space characters in yourself by interpolating
the array.

If you don't want them in there, then don't put them in there. :)


my @splitData = split /\+123(?:\+)?/, join "", @data;

or

my @splitData;
{ local $" = "";
@splitData = split /\+123(?:\+)?/, "@data";
}

or, do without the @data temporary array altogether:

my @splitData;
{ local $/;
@splitData = split /\+123(?:\+)?/, <IN>;
}



Yours is a Frequently Asked Question:

perldoc -q spaces

Why do I get weird spaces when I print an array of lines?
 
D

dutone

Tad McClellan said:
^ ^
^ ^
^ ^
what are you pointing out here?
Yes...

... and what is it that you want it to print instead?

Should we all just guess?

I'll assume you don't want the extra space characters at the
beginning of the lines after the first line?
Yes



That is not related to your problem in any way.



There are no "empty elements" here. Each element in the list
returned by split() is non-empty (ie. has length()).



You put the extra space characters in yourself by interpolating
the array.

If you don't want them in there, then don't put them in there. :)


my @splitData = split /\+123(?:\+)?/, join "", @data;

still prints:

LINE ** LINE ** n
blah blah blah
LINE **
more blah
sasas
assa
LINE **
or

my @splitData;
{ local $" = "";
@splitData = split /\+123(?:\+)?/, "@data";
}

so does this

or, do without the @data temporary array altogether:

my @splitData;
{ local $/;
@splitData = split /\+123(?:\+)?/, <IN>;
}

this does as well.
Yours is a Frequently Asked Question:

perldoc -q spaces

Why do I get weird spaces when I print an array of lines?

doing this:

my @splitData;
{ local $" = ":";
@splitData = split /\+123(?:\+)?/, "@data";
}

prints:

LINE ** LINE ** n
:blah blah blah
:LINE **
:more blah
:sasas
:assa
:LINE **

which leads me to think thats its not an interpolation problem. The
default $" interpolation would not cause an empty space at the
begining of the array. In my sample file '+123' is the first line, no
spaces , tabs etc... above it.

to be clear I do not want to print lines containg /^[^[:graph:]]$/
thanks....
 
T

Tad McClellan

dutone said:
[snip]

In my sample file '+123' is the first line, no
spaces , tabs etc... above it.


That isn't what was in the sample file that you showed to us.

In our data there _is_ a space in the first field.

Please post a short and complete program that *we can run*
that illustrates the problem you are trying to solve.

Do you know how to use the __DATA__ token to include file contents
in your source code file?

(If not, have you seen the Posting Guidelines that are posted here frequently?)
 
D

dutone

Tad McClellan said:
dutone said:
Tad McClellan said:
sample file:
+123n
blah blah blah
+123+
more blah
[snip]

In my sample file '+123' is the first line, no
spaces , tabs etc... above it.


That isn't what was in the sample file that you showed to us.

In our data there _is_ a space in the first field.

oops, sorry.
Please post a short and complete program that *we can run*
that illustrates the problem you are trying to solve.

the question is why there is an extra field in my array when I
split the above listed file (no starting space) on the given delim.
Do you know how to use the __DATA__ token to include file contents
in your source code file?

yes, but not good for this.
 
G

Gunnar Hjalmarsson

dutone said:
the question is why there is an extra field in my array when I
split the above listed file (no starting space) on the given delim.

The extract from "perldoc -f split", that I posted in this thread a
couple of days ago, explains it.
yes, but not good for this.

Why not?
 
T

Tad McClellan

dutone said:
Tad McClellan said:
dutone said:
sample file:
+123n
blah blah blah
+123+
more blah
[snip]

In my sample file '+123' is the first line, no
spaces , tabs etc... above it.


That isn't what was in the sample file that you showed to us.

In our data there _is_ a space in the first field.

oops, sorry.


If you had provided a short and complete program, as suggested in
the Posting Guidelines, you wouldn't be needing to apologize now.

If you had used copy/paste on the output, as suggested in
the Posting Guidelines, you wouldn't be needing to apologize now.



.... and we *still* have not seen one of those.

Are you there? Are you listening?

If you do what has been asked, you are *guaranteed* that someone
will be able to answer the question...

the question is why there is an extra field in my array when I
split the above listed file (no starting space) on the given delim.


If you had _said_ what you expected instead of what you were getting,
as suggested in the Posting Guidelines, then we might have answered
he right question rather than the wrong question.

The output you showed is exactly the expected behavior, which leaves
us all guessing as to what you want to be different.

When I asked if your problem was the extra spaces at the beginning
of the lines after the first one, you said yes.

But now you are saying that you have some _other_ question.

Does "yes" mean something different when you say it?

yes, but not good for this.


No, it is very good for this. It would have saved you from wasting the
time of hundreds of people around the world!

Why do you think it is not good for "this"?



The Posting Guidelines were written to help those that cared to avoid
becoming killfiled.

People that don't follow them risk getting a

*plonk*


You have used up all of your coupons with this one poorly-stated
problem, so long.




[ But since I am here, and you have *finally* made clear what you
want, here is how you can avoid capturing the initial field:

my(undef, @splitData) = split /\+123(?:\+)?/, "@data";

]
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top