develooper Front page | perl.beginners | Postings from August 2009

Re: 'Join' query

Thread Previous | Thread Next
From:
Telemachus
Date:
August 7, 2009 08:13
Subject:
Re: 'Join' query
Message ID:
20090807151319.GA259@ilium.local
On Fri Aug 07 2009 @  4:03, jet speed wrote:
> Hi,
> 
> I would like to join the $abc with ':'  the final desired output 1:2:3:4:5
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my $abc = "1 2 3 4 5";
> my $out = join ':', $abc;
> print "$out";
The function join works on lists not scalars. This would do what you want:

  my @important_numbers = (1..5);
  my $out = join ':', @important_numbers;
  print $out, "\n";

Alternatively, if for some reason your application gets the string '1 2 3 4
5' as a single item, you could split it first (on whitespace) and then join
the list that split produces:

  my $string = '1 2 3 4 5';
  my $out = join ':', split /\s/, $string;
  print $out, "\n";

My guess is that you don't really want the data in a string to begin with,
but sometimes you are getting it from somewhere else.

Hope this helps, T

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About