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

How do I split a string without using a junk variable. For example normally one would do this:
my $string="part of string to keep|part of string to toss"; my ($keep,$junk)=split /\|/, $string; print "$keep\n";
I would like to be able to access the part of split that goes into $keep directly and avoid the $junk variable all together. Any thoughts? Additional info: I am running ActivePerl 5.8.1 Build 807 on Win2K.
Any help that you can provide is greatly appreciated!

Replies are listed 'Best First'.
Re: using split without $junk
by dragonchild (Archbishop) on Nov 24, 2003 at 18:33 UTC
    TMTOWTDI - access the list with a slice
    my $string = "keep;junk;keep"; my @keepers = (split ';', $string)[0,2];

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Perfection! This is precisely what I was looking for. Thanks!
Re: using split without $junk
by LTjake (Prior) on Nov 24, 2003 at 18:31 UTC

    Just ignore it...

    $string = 'keep|throw'; my( $keep ) = split( /\|/, $string) ; print $keep;

    gives:

    keep

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Re: using split without $junk
by Limbic~Region (Chancellor) on Nov 24, 2003 at 18:28 UTC
    Anonymous Monk,

    This was discussed recently, but here is one way that doesn't involve split at all.

    #!/usr/bin/perl -w use strict; my $string = 'part of string to keep|part of string to toss'; my $keep = substr($string , 0 , index($string , '|'));
    Cheers - L~R
Re: using split without $junk
by davido (Cardinal) on Nov 24, 2003 at 18:37 UTC
    Maybe you don't want split at all. How about:

    my $string = "Keep this | toss this"; my ( $keep ) = $string =~ /^(.*?)\|/; print $keep, "\n";

    Update: On the other hand, if you really do need split (for example, because your list of things you're keeping is pretty long), you should just constrain split with the 3rd argument, and do something like this:

    my $string = "keep this | keep that too | toss this | toss this too"; my ( $this, $that ); ( $this, $that, undef ) = split /\|/, $string, 3;

    You don't have to explicitly list "undef", but in some cases, it makes the code more understandable if you have to go back and look at it again in a month.


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
      my ( $keep ) = $string =~ /^(.*?)\|/;
      Because you know where you want to stop matching, you really should drop .* and use a negated character class instead:
      my ( $keep ) = $string =~ /^([^\|]+)/;

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        I agree, and actually originally had it as [^|]+ ( | isn't special inside character classes, IIRC). But after hitting "preview" decided that the negated character class method might seem unclear to the OP, so I changed it to the more ubiquitous .*?.

        But your point is valid, that .* and .+ are usually the lazy, slower, and more prone to mistakes ways to do things in pattern matching.


        Dave


        "If I had my life to live over again, I'd be a plumber." -- Albert Einstein