standardising number of delimiters in a string

I

Ian Wilson

I have some data in the form of a string that contains values delimited
by a character. As obtained, the number of delimiters is variable. I
need to change this so that the number of delimiters is always the same
- by appending delimiters as needed.

I tried the following but was wondering if there is a better approach
that doesn't produce warnings.


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

my $list = 'a,b,c,d';

my ($a,$b,$c,$d,$e,$f) = split (/,/,$list);
$list = join (',', $a,$b,$c,$d,$e,$f);

print $list,"\n";


Use of uninitialized value in join or string at ./idw.pl line 8.
Use of uninitialized value in join or string at ./idw.pl line 8.
a,b,c,d,,
 
P

Paul Lalli

Ian said:
I have some data in the form of a string that contains values delimited
by a character. As obtained, the number of delimiters is variable. I
need to change this so that the number of delimiters is always the same
- by appending delimiters as needed.

I tried the following but was wondering if there is a better approach
that doesn't produce warnings.


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

my $list = 'a,b,c,d';

my ($a,$b,$c,$d,$e,$f) = split (/,/,$list);
$list = join (',', $a,$b,$c,$d,$e,$f);

print $list,"\n";


Use of uninitialized value in join or string at ./idw.pl line 8.
Use of uninitialized value in join or string at ./idw.pl line 8.
a,b,c,d,,

It sounds like all you want to do is append characters to the end of a
string. You don't actually care about split/join.
Step 1: Count the existing delimeters.
Step 2: Subtract from the desired number.
Step 3: Append.

#!/usr/bin/perl
use strict;
use warnings;
$_ = 'a,b,c,d';
my $total = 5;

my $num = tr/,//;
$_ .= (',' x (5-$num));

print "$_\n";
__END__

HTH,
Paul Lalli
 
P

Paul Lalli

Paul said:
It sounds like all you want to do is append characters to the end of a
string. You don't actually care about split/join.
Step 1: Count the existing delimeters.
Step 2: Subtract from the desired number.
Step 3: Append.

#!/usr/bin/perl
use strict;
use warnings;
$_ = 'a,b,c,d';
my $total = 5;

my $num = tr/,//;
$_ .= (',' x (5-$num));

That should, of course, be $total-$num, rather than the constant. Oops.
 
J

Jeff

Ian said:
I have some data in the form of a string that contains values delimited
by a character. As obtained, the number of delimiters is variable. I
need to change this so that the number of delimiters is always the same
- by appending delimiters as needed.

I tried the following but was wondering if there is a better approach
that doesn't produce warnings.

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

my $list = 'a,b,c,d';

my ($a,$b,$c,$d,$e,$f) = split (/,/,$list);

Despite the fact that there's no warning issued for this, it's
considered a dangerous thing to name your own variables $a or $b, since
they are the defaults sort() works on.
$list = join (',', $a,$b,$c,$d,$e,$f);

print $list,"\n";


Use of uninitialized value in join or string at ./idw.pl line 8.
Use of uninitialized value in join or string at ./idw.pl line 8.
a,b,c,d,,

I'm taking a lot of cold medicine, so I make no defense for anything
stupid here, but it's tested and does as requested.


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

my $list = 'a,b,c,d';
my $outputData;

my @inputData = split (/,/,$list);
for ( @inputData[0 .. 4] ) {
$outputData .= $_ || '';
$outputData .= ',';
}
$outputData .= $inputData[5] || '';

print $outputData,"\n";
 
I

Ian Wilson

Paul said:
It sounds like all you want to do is append characters to the end of a
string. You don't actually care about split/join.
Step 1: Count the existing delimeters.
Step 2: Subtract from the desired number.
Step 3: Append.

#!/usr/bin/perl
use strict;
use warnings;
$_ = 'a,b,c,d';
my $total = 5;

my $num = tr/,//;
$_ .= (',' x (5-$num));

print "$_\n";
__END__

HTH,
Paul Lalli

Thanks, Paul (and Jeff too),
that is the sort of solution I was hoping for.
Ian.
 

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