Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

another way to split with limit

by Anonymous Monk
on Aug 19, 2001 at 23:53 UTC ( [id://106066]=perlquestion: print w/replies, xml ) Need Help??

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

This looks ugly to me; the syntax :)
$_ = p:e:r:l ($p,$e,$r,$l) = split(/:/,$_,4); print "$p$e$r$l\n";
is there another way to make it look better? or rewrite it?

Replies are listed 'Best First'.
Re: another way to split with limit
by larsen (Parson) on Aug 20, 2001 at 00:04 UTC
    Maybe you should rephrase your question, but if your needs is to re-write $_ without separators, you could use:

    tr /://; print;

    Or, to avoid the third argument in split, you could use slices:

    $_ = 'p:e:r:l:p:e:r:l'; @a[0..3] = split /:/; print @a;

    But I'm not sure I've understood your question :))

      # give stdin a:b or hello:world or etc:etc while (<STDIN>){ ($a,$b) = split(/:/,$_,2); print "$a$b\n"; }
      I want to get those two values, how else can I do that with or without using split :) can I do
      (split(/:/)[0..1]? (split(/:/)[0,1]?
        Ok so I guess your print statement here isn't related to your question and that you only want to match both side of `:'
        You could do so like that:
        ($a, $b) = /(\w+):(\w+)/;
        The regex at the right remembers the matches due to the parenthesis, and $a get the first value and $b the second.
        Guillaume
Re: another way to split with limit
by Zaxo (Archbishop) on Aug 20, 2001 at 01:31 UTC

    I think split is exactly what you want to use. Regexes are inefficient way to do what split does.

    To my mind, the better cleanup would be to get rid of the named variables and use array manipulation. Since you say in a followup that you want to read colon-delimited pairs from STDIN and keep the results, you probably want to make a whole array of them. This little slug places the pairs in an array with the first of each in even-numbered position:

    my @pairs = map {chomp; split ':', $_, 2} <STDIN>;

    If one element of the pairs is suitable (no repetition), you can also make a hash, my %pairs from the map output.

    After Compline,
    Zaxo

    Update: Added a chomp, assuming that's desirable.

Re: another way to split with limit
by dga (Hermit) on Aug 20, 2001 at 01:55 UTC

    I seem to remember that split quits splitting when it runs out of places to put things.

    $_= "p:e:r:l"; ($p,$e)=split(/:/);

    split would assign p to p e to e read in r and then stop scanning the string at that point.

    So putting in the limit is not needed since split will quit scanning after filling all the requested space. Of course if you split into an array the entire input string would be scanned and split as expected.

    And you don't need to tell split to split $_ since that is the default thing to split.

Re: another way to split with limit
by guillaume (Pilgrim) on Aug 20, 2001 at 00:06 UTC
    What exactly do you want to do?
    If all that you want is to remove the `:' and print the string then just do:
    $_ = "p:e:r:l"; $_ =~ s/://g; # substitue all the `:' by nothing print "$_\n";

    Guillaume
      I want to actually use the individual values I was thinking maybe substitute the split part with ($a,$b) = /(.*):(.*)/;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-19 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found