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

I have a large file with data seperated by |. I want to split the document at each | only. Its giving me error when i have a blank field such as "|abcxyz||fiuhs|" and also it is giving me error when it encounters a parantheses eg: "|adbd(cjh) | hjjds|" All i want is the split function to only split at |. Can somebody help me with this command. I really appreciate it.

Replies are listed 'Best First'.
Re: Split function
by Adam (Vicar) on Feb 08, 2001 at 00:57 UTC
    Without the benefit of your code, I can't really tell you where you went wrong.
    use strict; my $file; { local $/; $file = <>; } $file =~ s!$/!!g; my @elements = split /\|/, $file;
    above code is untested

    Update
    By the way, there is more info about split in the library. I just noticed this was your first post, and that no one pointed you there, so I thought I would mention it as a reference.

Re: Split function
by kschwab (Vicar) on Feb 08, 2001 at 01:00 UTC
    You would have to post some sample code and the specific error text. "Its giving me error" doesn't really say much.

    Just for grins:

    #!/usr/bin/perl my $foo="|abcxyz||fiuhs|"; for (split(/\|/,$foo)) { print "field is [$_]\n"; }
    Creates this output:

    field is [] field is [abcxyz] field is [] field is [fiuhs]

    Which seems reasonable to me...

Re: Split function
by lemming (Priest) on Feb 08, 2001 at 00:59 UTC
    @turkey = split(/\|/, $stuffing);
    The | is backslashed so that | isn't termed as an or condition. That splits each string ($stuffing) and places it into an array @turkey.
    Update:
    Hmm. If you want to throw away "blank" values.
    @turkey = split(/\|+/, $stuffing);

    Update for MeowChow: Well, you need to do some mixing and chopping, but I find the chop() function cuts too thin and the consistancy winds up too soft. I prefer some chunks.

      You can make a turkey by splitting stuffing? The things you learn here... :-)