Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Is there any way to make an array non-greedy?

by thor (Priest)
on Oct 14, 2005 at 16:45 UTC ( [id://500298]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I'm trying to split some data that has a series of related values in the middle. Ideally, I'd like to be able to split them right into their eventual home, which will be an array. I've come up with a toy script that hopefully demonstrates what I'd like
use strict; use warnings; my $string = join('x', 'a'x20); my (@foo, $foo); (@foo[0..18], $foo) = split('x', $string); print "Success!\n" if $foo eq "a";
I thought that explicitly supplying the indicies for the array would help, but no dice. Is there some clever way to pull this off?

thor

Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come

Update: It looks like I had an error in my test code, as most of you stated below. Just goes to show you that you can't trust yourself sometimes. Thanks for the help!

Replies are listed 'Best First'.
Re: Is there any way to make an array non-greedy?
by ikegami (Patriarch) on Oct 14, 2005 at 16:56 UTC

    You have a bug:
    my $string = join('x', 'a'x20);
    should be
    my $string = join('x', ('a')x20);
    Fix that and it works.

    If you always have one element left over, you can make the code readable as:

    use strict; use warnings; my $string = join('x', ('a')x20); my @foo = split('x', $string); my $foo = pop(@foo); print "Success!\n" if $foo eq "a";

    If you always have a variable number of elements left over, you have a problem fixed as follows:

    use strict; use warnings; my $string = join('x', ('a')x20); my (@foo, $foo); (@foo[0..18], $foo) = split('x', $string, 20); # <-- added limit. print "Success!\n" if $foo eq "a";
      If my real problem were as easy as my toy script, I'd certainly follow one of your above solutions. However, it's more like this:
      my ( $field1, $field2, #... $field25, @field26[0..25], $field27, $field28, #... ) = split(/\|/);
      This way, it makes it obvious that I'm grabbing a fixed number from the split return. I appreciate the terseness in your solutions, though. Now that I know that it was a problem with my test data and not my approach, I'll be using something like what I originally posted.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        I think you're after something like this:

        @important_fields = (split /\|/, $string)[25..50];
Re: Is there any way to make an array non-greedy?
by sk (Curate) on Oct 14, 2005 at 16:53 UTC
    Did you mean to do this?

    use strict; use warnings; my $string = join('x', ('a')x20); ### NOTE the ()'s print $string,$/; my (@foo, $foo); (@foo[0..18], $foo) = split('x', $string); print $foo[0],$/; print "Success!\n" if $foo eq "a"; __END__ axaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxa a Success!

    I don't quite understand your  join('x', 'a'x20); otherwise. Why join a scalar?

    cheers

    SK

Re: Is there any way to make an array non-greedy?
by xdg (Monsignor) on Oct 14, 2005 at 17:00 UTC

    For this case, where you only want one item, reverse your split:

    use strict; use warnings; my $string = join('x', ('a') x 20); my (@foo, $foo); ($foo, @foo) = reverse( split('x', $string) ); print "\$foo:$foo\n"; print "\@foo:@foo\n";

    Prints

    $foo:a @foo:a a a a a a a a a a a a a a a a a a a

    Note the fix in your $string line to put 'a' as a list

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Is there any way to make an array non-greedy?
by nothingmuch (Priest) on Oct 15, 2005 at 03:50 UTC
    my $foo = (split "x", $string)[19];

    -nuffin
    zz zZ Z Z #!perl
Re: Is there any way to make an array non-greedy?
by Anonymous Monk on Oct 14, 2005 at 16:54 UTC
    $string only has 1 x

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-18 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found