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

How do I split a string into an array when there is a | involved ?

I know | is a special character so I have tried this:

@code =split(/\|/,$id);
Where id is a string such as: lcl|CHLMU1894 and I am wanting to retrieve the information CHLMU1894 in $code1

Thanks

Replies are listed 'Best First'.
Re: split at pipe character
by strat (Canon) on Mar 04, 2002 at 17:21 UTC
    That looks alright; I've just run it under Win2k, and it says just, what you want it to say:
    D:\work\perl\Net\ldap>perl -w use strict; my $id = 'lcl|CHLMU1894'; my @code = split(/\|/,$id); print $code[1]; ^Z CHLMU1894

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: split at pipe character
by Juerd (Abbot) on Mar 04, 2002 at 17:18 UTC
    You're doing it the right way. Are you sure $id exists? (use strict!!)

    ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
    Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
    -- vs lbh hfrq OFQ pnrfne ;)
        - Whreq
    

Re: split at pipe character
by gellyfish (Monsignor) on Mar 04, 2002 at 17:18 UTC

    That should be right, what is happening when you do this ?

    /J\

Re: split at pipe character
by Chady (Priest) on Mar 04, 2002 at 17:57 UTC

    you could always single quote and save yourself the trouble of escaping:

    @code = split '|', $id;

    Update: I stand corrected. Thanx Juerd.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      Wrong.
      Many people think that, but any string that is not a single space (chr(32)) will implicitly be used as a regex, so split '|' will still be split /|/ and thus equal split //.
      See also Perl6 RFC 361.

      ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
      Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
      -- vs lbh hfrq OFQ pnrfne ;)
          - Whreq
      

Re: split at pipe character
by Parham (Friar) on Mar 04, 2002 at 19:19 UTC
    #!/usr/bin/perl use strict; my $id = 'lcl|CHLMU1894'; my @code = split(/\|/,$id); print $code[1];
    that will print CHLMU1894