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

splitting a file query ....

Status
Not open for further replies.

rabinski

Programmer
May 13, 2005
21
GB
Gurus

I have a file in this format -

field1 field2

I am substituting the spaces for commas and then doing a foreach splitting on the comma - great -

But I have just found that the first field can be of differing lengths hence the number of commas inserted will change

eg - after the substitution

field1,,,,,,,,,,field2
f1,,,,,,,,,,,,,,field1

etc ...

Is Text::CSV the way to go ?

cheers guys

Rab
 
I don't think you have given enough information - can you elaborate - or post some sample data?


Kind Regards
Duncan
 
I was thinking exactly the same thing...

Why convert 48 spaces to 48 commas - and then split it up??? Not much gain there...


Kind Regards
Duncan
 
split uses a regex, so why not
Code:
use strict;
use warnings;

while (<DATA>) {
        print join(",", split(/\s+/, $_, 2));
}

__DATA__
a   b  
cd e
f g
h    i
?

I guess you want to do something with them rather than print them out comma-separated so
Code:
use strict;
use warnings;

while (<DATA>) {
        my @fields = split(/\s+/, $_, 2);
        chomp @fields; # newlines were a help in the last example, but not in this one

        # do stuff with @fields here
}
is probably going to be more use.

HTH

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top