Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Splitting on a pattern once

by perlpal (Scribe)
on Mar 03, 2010 at 18:09 UTC ( [id://826479]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks,

I need to split a string which has multiple repitititions of a pattern only on the first occurrence of the pattern.

Case in point being the string mentioned below :

Aggregate: f3050-184-160:EmptyAggr

I need to split the string on ":" on the first occurrence only i.e when it appears after Aggregate.

Is this possible via split in a single step or do i need to post-process the array after split.

Thanks in advance!

Replies are listed 'Best First'.
Re: Splitting on a pattern once
by almut (Canon) on Mar 03, 2010 at 18:19 UTC

    You can specify the number pieces you want:

    #!/usr/bin/perl -l my $s = "Aggregate: f3050-184-160:EmptyAggr"; my ($first, $rest) = split /:/, $s, 2; # ^ print "\$first: '$first'"; print "\$rest: '$rest'"; __END__ $first: 'Aggregate' $rest: ' f3050-184-160:EmptyAggr'
Re: Splitting on a pattern once
by ikegami (Patriarch) on Mar 03, 2010 at 18:20 UTC

      Is there any way to split the string from last occurrence.?
      I tried with positive values and negative value in 3rd argument of split().
      --sugumar--
        Is this you are expecting.
        Example-1
        my $str="1 2 3 4 590"; my ($a,$b)=split(/ ([^ ]+)$/, $str); print "a=$a\nb=$b\n";

        Output
        a=1 2 3 4
        b=590

        Example-2
        my $str="1:2:3:4:590"; my ($a,$b)=split(/:([^:]+)$/, $str); print "a=$a\nb=$b\n";

        Output
        a=1:2:3:4
        b=590
        Use the following way to achieve your requirement.
        my $str="1:2:3"; my $name=(split(':',$str))[-1];
        -1 will return the value of last index.
        You could play with reverse, but this is simpler:
        my ($k,$v) = /^(.*:)?(.*)/s;
        You should read the documentation for split as a negative limit has special meaning.
Re: Splitting on a pattern once
by toolic (Bishop) on Mar 03, 2010 at 18:24 UTC
    If you want to just split on the 1st :, use split with a LIMIT of 2:
    use strict; use warnings; my $s = ' Aggregate: f3050-184-160:EmptyAggr'; my ($first, $rest) = split /:/, $s, 2; print "$first\n"; print "$rest\n"; __END__ Aggregate f3050-184-160:EmptyAggr
Re: Splitting on a pattern once
by ikegami (Patriarch) on Mar 18, 2010 at 15:05 UTC
    Alternative:
    my ($name, $val) = split /:\s*/; my @val = split /:/, $val;
    Then just check $vals[1]. The advantage is that you can also check if you're looking at the right field (by checking $name

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found