Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

splitting pipe into new line

by PerlSufi (Pilgrim)
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

Comment on splitting pipe into new line
Download Code
Re: splitting pipe into new line
by toolic (Chancellor) 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 (Curate) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-08-28 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls