Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Unsplit An Array

by virtualweb (Sexton)
on Sep 22, 2008 at 19:17 UTC ( [id://713079]=perlquestion: print w/replies, xml ) Need Help??

virtualweb has asked for the wisdom of the Perl Monks concerning the following question:

Hi: I splitted an array containig a word because I needed to change some of the elements, (some letters): $Tne_Word = 'antidisestablishmentarianism'; @splitted_word = split(//, $Tne_Word); when I print to the browser @splitted_word it reads: a n t i d i s e s t a b l i s h m e n t a r i a n i s m How do I make it look whole again, (without the spaces) Thanx VirtualWeb

Replies are listed 'Best First'.
Re: Unsplit An Array
by FunkyMonk (Chancellor) on Sep 22, 2008 at 19:19 UTC
Re: Unsplit An Array
by jwkrahn (Abbot) on Sep 22, 2008 at 20:13 UTC
    I splitted an array containig a word because I needed to change some of the elements, (some letters)

    You actually split a scalar containing a word into an array.

    Have you tried using the substitution or transliteration operators?

Re: Unsplit An Array
by hexcoder (Curate) on Sep 22, 2008 at 19:52 UTC
    Set the list seperator to the empty string
    use strict; use warnings; my $Tne_Word = 'antidisestablishmentarianism'; my @splitted_word = split(//, $Tne_Word); { local $"=''; print @splitted_word, "\n"; }
    Update (thanks to jwkrahn and ikegami)
    use strict; use warnings; my $Tne_Word = 'antidisestablishmentarianism'; my @splitted_word = split(//, $Tne_Word); { local $"=''; print "@splitted_word\n"; }

      Not quite. That should be

      { local $,=''; local $\="\n"; print( @splitted_word ); }

      or

      { local $"=''; print( "@splitted_word\n" ); }

      See $,, $\ and $".

      However, using join is less error-prone, more natural, shorter and more versatile (you can use it when you don't want to print).

      print( join( '', @splitted_word ), "\n" );
        Ikegami: Thanx for your input a variation ofyourline of code did the trick @Make_Whole = join( '', @splitted_word ); I had tried the following prior to posting for help and hadnt work: @Make_Whole = join( //, @splitted_word ); It did work with commas however thanx again VirtualWeb

      You changed the value of the $" variable but  print @splitted_word, "\n"; doesn't use the $" variable.   It does however use the $, and $\ variables.

      Thanks for the correction jwkrahn and ikegami. The above response was obviously a bit too hasty. I intended ikegami's second variant. But it is probably better to use join, since the behavior does not rely on interpolation.
      I updated the code to correct the error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://713079]
Approved by andyford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found