Concatenating an array into one string?

R

Rapier

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.
 
P

Paul Lalli

Rapier said:
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
 
A

A. Sinan Unur

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.
 
J

Jürgen Exner

Rapier said:
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
 
P

Paul Robson

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);
 
J

Jürgen Exner

Paul said:
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
 
S

Shawn Corey

Jürgen Exner said:
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__
 
P

Paul Lalli

Jürgen Exner said:
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
 
J

Jürgen Exner

Paul said:
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
 
T

Tad McClellan

Jürgen Exner said:
Paul said:
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!
 
J

Jürgen Exner

Tad said:
Jürgen Exner said:
Paul said:
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
 
A

Anno Siegel

Scott Bryce said:
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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top