Concatenating an array into one string?

Discussion in 'Perl Misc' started by Rapier, Oct 13, 2004.

  1. Rapier

    Rapier Guest

    Hi,
    I have an array I want to concatenate into one string without \n's in the
    code below all I get in the output for jobcat is 1111 why? How do I correct
    this code?

    $test[0] = "one\n";
    $test[1] = "two\n";
    $test[2] = "three\n";


    $jobcat = "";
    $i = 0;
    foreach(@test)
    {
    $jobcat .= chomp($test[$i]);
    $i++;
    }

    print ("jobcat=$jobcat\n");

    Thanks in advance.
     
    Rapier, Oct 13, 2004
    #1
    1. Advertising

  2. Rapier

    Paul Lalli Guest

    "Rapier" <> wrote in message
    news:...
    > Hi,
    > I have an array I want to concatenate into one string without \n's in

    the
    > code below all I get in the output for jobcat is 1111 why?


    Please read the documentation for the functions you use.
    perldoc -f chomp
    It does not return the chomp'ed string.

    How do I correct
    > this code?
    >
    > $test[0] = "one\n";
    > $test[1] = "two\n";
    > $test[2] = "three\n";
    >
    >
    > $jobcat = "";
    > $i = 0;
    > foreach(@test)
    > {
    > $jobcat .= chomp($test[$i]);
    > $i++;
    > }


    Why are you keeping a counter and also using a foreach-style loop?
    Within that loop, each element of @test is assigned to $_. There is no
    need for $i at all.

    This entire program can be written better as simply:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my @test;
    $test[0] = "one\n";
    $test[1] = "two\n";
    $test[2] = "three\n";
    chomp @test;
    my $jobcat = join '', @test;
    print $jobcat;
    #or instead of the above two lines, simply:
    print @test;
    __END__

    Paul Lalli
     
    Paul Lalli, Oct 13, 2004
    #2
    1. Advertising

  3. "Rapier" <> wrote in
    news::

    > I have an array I want to concatenate into one string without \n's in
    > the code below all I get in the output for jobcat is 1111 why?


    Sometimes I regain my faith in humanity ... Then I lose it again.

    > $jobcat .= chomp($test[$i]);


    Read perldoc -f chomp and see what chomp returns.

    Sinan.
     
    A. Sinan Unur, Oct 13, 2004
    #3
  4. Rapier wrote:
    > Hi,
    > I have an array I want to concatenate into one string without \n's in
    > the code below all I get in the output for jobcat is 1111 why? How do
    > I correct this code?


    You are missing:

    use warnings; use strict;

    > $test[0] = "one\n";
    > $test[1] = "two\n";
    > $test[2] = "three\n";


    This is easier written as:

    my @test = ("one\n", "two\n", "three\n");

    > $jobcat = "";


    No need to initialize $jobcat, Perl does it automatically for you when you
    declare the variable:

    my $jobcat;

    > $i = 0;
    > foreach(@test)
    > {
    > $jobcat .= chomp($test[$i]);
    > $i++;
    > }


    Ouch, this whole loop hurts!
    - What do you need the $i for when you are looping through each element
    anyway?
    - Why are you not using $_ but $test[$i]?
    - why chomp() each element individually?
    - why individually append the elements?

    Just replace the whole mess with a simple

    $jobcat = join '', chomp @test;

    > print ("jobcat=$jobcat\n");


    Ok, that line seems to be fine.

    jue
     
    Jürgen Exner, Oct 13, 2004
    #4
  5. Rapier

    Paul Robson Guest

    On Wed, 13 Oct 2004 15:53:19 +0100, Rapier wrote:

    > Hi,
    > I have an array I want to concatenate into one string without \n's in the
    > code below all I get in the output for jobcat is 1111 why? How do I correct
    > this code?
    >
    > $test[0] = "one\n";
    > $test[1] = "two\n";
    > $test[2] = "three\n";
    >
    >
    > $jobcat = "";
    > $i = 0;
    > foreach(@test)
    > {
    > $jobcat .= chomp($test[$i]);
    > $i++;
    > }
    >
    > print ("jobcat=$jobcat\n");
    >
    > Thanks in advance.


    The reason it doesn't work is chomp returns the number of characters
    removed when "chomping" which is 1 (the \n character) - which is
    "stringified" into "1" which is concatenated to $jobcat so you get
    "111" - three 1's concatenated.

    You could change your solution to write

    chomp($test[$i]);
    $jobcat .= $test[$i];

    But it's not a neat "Perl" solution. In Perl you do not need the
    equivalent index variable. (the $i).

    You can use $_ - just for (@test) will give you all the elements in order
    in $_

    To index iterate the array in order (not sure if for (@arr) guarantees
    this ?) you can use for $my i (0..$#test) - $#test is the last index value
    of array @test.

    #
    # for all elements in @test chomp them. $_ becomes $test[0] through 2
    #
    chomp for (@test);
    #
    # Perls join function joins all the scalars together in a string
    # the first parameter is the seperator - in this case the empty string.
    #
    my $jobcat = join("",@test);
     
    Paul Robson, Oct 13, 2004
    #5
  6. Paul Robson wrote:
    > chomp($test[$i]);
    > $jobcat .= $test[$i];
    >
    > But it's not a neat "Perl" solution.


    Agree. A more perlish solution is

    chomp @test;
    $jobcat = join '', @test;

    jue
     
    Jürgen Exner, Oct 13, 2004
    #6
  7. Rapier

    Shawn Corey Guest

    Jürgen Exner wrote:
    >>$jobcat = "";

    >
    >
    > No need to initialize $jobcat, Perl does it automatically for you when you
    > declare the variable:
    >
    > my $jobcat;


    No it doesn't, notice the difference?
    --- Shawn

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $not_initialized;
    print "not initialized = $not_initialized\n";

    my $initialized = '';
    print "initialized = $initialized\n";

    __END__
     
    Shawn Corey, Oct 13, 2004
    #7
  8. Rapier

    Paul Lalli Guest

    "Jürgen Exner" <> wrote in message
    news:KLbbd.87$dI6.50@trnddc03...
    > Just replace the whole mess with a simple
    >
    > $jobcat = join '', chomp @test;


    This, of course, contains exactly the same error that the original did.
    chomp does not return the modified strings.

    Paul Lalli
     
    Paul Lalli, Oct 13, 2004
    #8
  9. Paul Lalli wrote:
    > "Jürgen Exner" <> wrote in message
    > news:KLbbd.87$dI6.50@trnddc03...
    >> Just replace the whole mess with a simple
    >>
    >> $jobcat = join '', chomp @test;

    >
    > This, of course, contains exactly the same error that the original
    > did. chomp does not return the modified strings.


    Yep, just like I pointed out in my own follow-up posting.

    jue
     
    Jürgen Exner, Oct 13, 2004
    #9
  10. Jürgen Exner <> wrote:
    > Paul Robson wrote:
    >> chomp($test[$i]);
    >> $jobcat .= $test[$i];
    >>
    >> But it's not a neat "Perl" solution.

    >
    > Agree. A more perlish solution is
    >
    > chomp @test;
    > $jobcat = join '', @test;



    Or if you don't want to change @test's values:

    $jobcat = join '', map { /(.*)/ } @test;
    or
    $jobcat = join '', map { substr $_, 0, -1 } @test; # better have newlines!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 14, 2004
    #10
  11. Tad McClellan wrote:
    > Jürgen Exner <> wrote:
    >> Paul Robson wrote:
    >>> chomp($test[$i]);
    >>> $jobcat .= $test[$i];
    >>>
    >>> But it's not a neat "Perl" solution.

    >>
    >> Agree. A more perlish solution is
    >>
    >> chomp @test;
    >> $jobcat = join '', @test;

    >
    > Or if you don't want to change @test's values:
    >
    > $jobcat = join '', map { /(.*)/ } @test;
    > or
    > $jobcat = join '', map { substr $_, 0, -1 } @test; # better have
    > newlines!


    Or (if you don't mind the added spaces)
    $jobcat = "@test";

    jue
     
    Jürgen Exner, Oct 14, 2004
    #11
  12. Rapier

    Anno Siegel Guest

    Scott Bryce <> wrote in comp.lang.perl.misc:
    > Paul Lalli wrote:
    >
    > > chomp @test;

    >
    > I've got the Camel book open right in front of me, and I missed that!
    >
    > I should have known that if I posted code, someone would find a better
    > way to do it!


    Well, that's the point of posting code, isn't it.

    Anno
     
    Anno Siegel, Oct 14, 2004
    #12
    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. arnuld
    Replies:
    8
    Views:
    579
    Old Wolf
    Feb 8, 2007
  2. A B
    Replies:
    15
    Views:
    1,680
    Jorgen Grahn
    Nov 11, 2010
  3. Rider
    Replies:
    6
    Views:
    197
    Charlton Wilbur
    Jul 26, 2009
  4. dewed

    stupid concatenating string question

    dewed, Nov 24, 2008, in forum: Javascript
    Replies:
    3
    Views:
    110
    Thomas 'PointedEars' Lahn
    Nov 24, 2008
  5. Replies:
    2
    Views:
    178
    GGolf
    Nov 6, 2013
Loading...

Share This Page