Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

split problem

by crusty_collins (Friar)
on Oct 02, 2014 at 18:01 UTC ( [id://1102659]=perlquestion: print w/replies, xml ) Need Help??

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

I must be brain dead today because i cant figure this out. I want to split the __DATA__ into an array wiht the | (pipe) character but it just splits it by each character. Code
#!/usr/bin/perl use strict; use warnings; while (<DATA>){ chomp; next if (/Order/); print (" string $_ \n"); $_ =~ s/\"//g; my @array = split /|/ ; print(" order $array[0] \n"); } __DATA__ "129822"|"Custom Currency"|"Living the Dream"|"400" "129823"|"Custom Currency"|"Living the Dream"|"500" "129824"|"Custom Currency"|"Living the Dream"|"600" "129825"|"Custom Currency"|"Living the Dream"|"700" "129826"|"Custom Currency"|"Living the Dream"|"800"
Output
string "129822"|"Custom Currency"|"Living the Dream"|"400" order 1 string "129823"|"Custom Currency"|"Living the Dream"|"500" order 1 string "129824"|"Custom Currency"|"Living the Dream"|"600" order 1 string "129825"|"Custom Currency"|"Living the Dream"|"700" order 1 string "129826"|"Custom Currency"|"Living the Dream"|"800" order 1 string

Replies are listed 'Best First'.
Re: split problem
by toolic (Bishop) on Oct 02, 2014 at 18:03 UTC
    You need to escape the | because it is a special character in regexes (split uses regexes):
    my @array = split /\|/ ;
      I thought i tried that but i guess i didnt because that works! Thank you !
        for safety I often use split /\s*\|\s*/ which will also remove whitespace between the quotes in case it's there.
Re: split problem
by codiac (Beadle) on Oct 03, 2014 at 10:19 UTC
    Why use regex when there is a module to do this for you?
    #!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new ({ sep_char => '|' }); while (<DATA>){ my $line = $_; chomp; next if (/Order/); print ("string $_ \n"); $_ =~ s/\"//g; my @array = split /\|/ ; print("order $array[0] , wrong sometimes $array[3]\n"); my $status = $csv->parse($line); my @columns = $csv->fields(); print("order $columns[0] , right $columns[3]\n\n"); } __DATA__ "129822"|"Custom Currency"|"Living the | Dream"|"400" "129823"|"Custom Currency"|"Living the Dream"|"500"
    Outputs:
    string "129822"|"Custom Currency"|"Living the | Dream"|"40 order 129822 , wrong sometimes: Dream order 129822 , right 400 string "129823"|"Custom Currency"|"Living the Dream"|"500" order 129823 , wrong sometimes: 500 order 129823 , right 500
    The questions isn't is someone likely to put that pipe dream in there, the question is if you should ever have to care if they do!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found