standardising number of delimiters in a string

Discussion in 'Perl Misc' started by Ian Wilson, Mar 24, 2005.

  1. Ian Wilson

    Ian Wilson Guest

    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,,
    Ian Wilson, Mar 24, 2005
    #1
    1. Advertising

  2. Ian Wilson

    Paul Lalli Guest

    Ian Wilson wrote:
    > 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
    Paul Lalli, Mar 24, 2005
    #2
    1. Advertising

  3. Ian Wilson

    Paul Lalli Guest

    Paul Lalli wrote:
    > Ian Wilson wrote:
    >
    >> 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));


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


    >
    > print "$_\n";
    > __END__
    >
    > HTH,
    > Paul Lalli
    Paul Lalli, Mar 24, 2005
    #3
  4. Ian Wilson

    Jeff Guest

    Ian Wilson wrote:
    > 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";
    Jeff, Mar 24, 2005
    #4
  5. Ian Wilson

    Ian Wilson Guest

    Paul Lalli wrote:
    > Ian Wilson wrote:
    >
    >> 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


    Thanks, Paul (and Jeff too),
    that is the sort of solution I was hoping for.
    Ian.
    Ian Wilson, Mar 24, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dmitrey
    Replies:
    4
    Views:
    446
  2. Gunter Hansen
    Replies:
    5
    Views:
    915
    Roedy Green
    Sep 1, 2011
  3. Neville Franks
    Replies:
    2
    Views:
    77
    Neville Franks
    Feb 8, 2007
  4. Gary C40
    Replies:
    6
    Views:
    140
    MonkeeSage
    Dec 16, 2007
  5. Albert Schlef

    String#split and capturing delimiters

    Albert Schlef, Oct 30, 2009, in forum: Ruby
    Replies:
    3
    Views:
    174
    Rajinder Yadav
    Oct 30, 2009
Loading...

Share This Page