help in array processing

B

bhooshan.dixit

I have an array which looks something like this

Bob
Bruce
Tim
Jimmy


I want to add a comma ',' to the end of each element in the array
except for the last element.
Then I want to concatenate each element into a combined string and
store it in a single variable.

I figured out how to add the comma at the end basically by doing
something like this.
foreach(@list1)
{
s/(.*)/$1,/;
}
BUT i need some guidance on how to exclude the last comma on the last
element.

So basically my array show looks something like this.
Bob,
Bruce,
Tim,
Jimmy

notice how the comma is missing after Jimmy.
After the comma's are inserted I want to concatenate and put them in a
string
something like. $t="Bob,Bruce,Tim,Jimmy";

I am kinda a new to perl..so if someone can suggest me different
methods, It would be great.

Thanks
bcd
 
S

surfer dude

[This followup was posted to comp.lang.perl.misc]

While wandering through cyberspace on 2 Jun 2006 15:23:38 -0700, said
....
I have an array which looks something like this

Bob
Bruce
Tim
Jimmy


I want to add a comma ',' to the end of each element in the array
except for the last element.
Then I want to concatenate each element into a combined string and
store it in a single variable.

I figured out how to add the comma at the end basically by doing
something like this.
foreach(@list1)
{
s/(.*)/$1,/;
}
BUT i need some guidance on how to exclude the last comma on the last
element.

So basically my array show looks something like this.
Bob,
Bruce,
Tim,
Jimmy

notice how the comma is missing after Jimmy.
After the comma's are inserted I want to concatenate and put them in a
string
something like. $t="Bob,Bruce,Tim,Jimmy";

I am kinda a new to perl..so if someone can suggest me different
methods, It would be great.

Thanks
bcd

Sounds to me like a good use for the "join" function.

my ( @list1 , $string1 );

$string1 = join(",",@list1);
 
C

Ch Lamprecht

I have an array which looks something like this

Bob
Bruce
Tim
Jimmy


I want to add a comma ',' to the end of each element in the array
except for the last element.

After the comma's are inserted I want to concatenate and put them in a
string
something like. $t="Bob,Bruce,Tim,Jimmy";

I am kinda a new to perl..so if someone can suggest me different
methods, It would be great.

Thanks
bcd

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

my @names = qw/ Bob
Bruce
Tim
Jimmy
/;
my $joined_names = join(',',@names);


See 'perldoc -f join' for documentation.

Christoph
 
M

MSG

I have an array which looks something like this

Bob
Bruce
Tim
Jimmy


I want to add a comma ',' to the end of each element in the array
except for the last element.
Then I want to concatenate each element into a combined string and
store it in a single variable.

I figured out how to add the comma at the end basically by doing
something like this.
foreach(@list1)
{
s/(.*)/$1,/;
}
BUT i need some guidance on how to exclude the last comma on the last
element.

So basically my array show looks something like this.
Bob,
Bruce,
Tim,
Jimmy

notice how the comma is missing after Jimmy.
After the comma's are inserted I want to concatenate and put them in a
string
something like. $t="Bob,Bruce,Tim,Jimmy";

I am kinda a new to perl..so if someone can suggest me different
methods, It would be great.

Thanks
bcd

Perl's join() function does exactly that in one fell swoop:

@list1 = qw( Bob Bruce Tim Jimmy );
$t = join ",", @list1;
print $t;

( I hope you will soon learn about enabling strict and warnings )

For more info on join(), see
perldoc -f join
 
B

bhooshan.dixit

Thanks

Ch said:
#!/usr/bin/perl
use strict;
use warnings;

my @names = qw/ Bob
Bruce
Tim
Jimmy
/;
my $joined_names = join(',',@names);


See 'perldoc -f join' for documentation.

Christoph
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top