Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

splitting pipe into new line

by PerlSufi (Friar)
on Jun 14, 2013 at 17:18 UTC ( #1039005=perlquestion: print w/replies, xml ) Need Help??
PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks
I have some data from a config file parameter that is delimited by a pipe symbol |
At each pipe symbol, I need to split it into a new line. Here is what I have so far:
use Config::Simple; my $cfg = new Config::Simple('/path/to/.file.ini'); $cfg = Config::Simple->import_from('/path/to/.file.ini', \%Config); my $description = $cfg->param("$ARGV[0]".".description"); my (@new) = split (/\|/, $description, -1); print @new;
It is not making each pipe a new line. Does it have something to do with the limit number I passed to split? I thought negative would allow for as many possbile fields to be produced?
Thanks, PerlSufi

Replies are listed 'Best First'.
Re: splitting pipe into new line
by toolic (Bishop) on Jun 14, 2013 at 17:23 UTC
    It's not clear to me what you want. If you want to replace all pipes with a newline, you can use s///:
    use warnings; use strict; my $description = 'a|b|c|d|e'; $description =~ s/\|/\n/g; print "$description\n"; __END__ a b c d e

    Or, if you really want an array, you can add newlines using map

    my $description = 'a|b|c|d|e'; print "$description\n"; my (@new) = map {"$_\n"} split (/\|/, $description); print @new;
      Ah, silly me- thanks toolic. s/// is what I needed.
Re: splitting pipe into new line
by hippo (Canon) on Jun 14, 2013 at 18:23 UTC

    Looks like toolic has solved your immediate problem. However, I thought it polite to point out that your line using the 'import_from' method seems to be a no-op here (unless you are doing something else which you are not showing us of course).

    However, if I'm wrong and you are genuinely using it for something, then may I suggest a piece of good practice? If you ever find that you are using the same literal string more than once (eg. '/path/to/.file.ini'), just pop it in a scalar variable and use that instead. It has been my experience that this saves hours of bughunting only to find a typo as the culprit.

      Thanks, hippo! Good advice that I will try to put into practice ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039005]
Approved by toolic
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-08-20 10:27 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (191 votes). Check out past polls.