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

Hi monks,

I am trying to get the rest of the array once a certain element has been matched, however, I am struggling to then split the rest of the array into individual elements so I can access each individually.

I was wondering if there was a way to capture the rest of the array (as i have done below) but to also add a whitespace character after every subsequent element is found??? Thanks a lot!

for (my $i=0; $i < @start_incline; $i++) { if ($start_incline[$i] == $sorted_nums[0]) { @incline = @start_incline [$i+1 .. $#start_incline]; } }

Replies are listed 'Best First'.
Re: saving rest of array after a match
by broquaint (Abbot) on Jun 27, 2003 at 09:32 UTC
    Just use a simple map
    @incline = map "$_ ", @start_incline [$i+1 .. $#start_incline];
    You might also be interested in the first function from List::Util.
    HTH

    _________
    broquaint

Re: saving rest of array after a match
by ViceRaid (Chaplain) on Jun 27, 2003 at 09:30 UTC

    Not sure I quite understand the second part of your question, but if the code you've posted does what you want already in selecting the right bits of the array, maybe just do a map afterwards to add the whitespace?

    @incline = map { "$_ " } @incline;

    cheers
    ViceRaid

    update: posted too quick, amended code. sorry
Re: saving rest of array after a match
by Tomte (Priest) on Jun 27, 2003 at 09:41 UTC

    I don't understand your question, you access array-elements via the subscript @array[$pos] or as $_ within an iterating construct...

    Let me rephrase your code a bit:

    # splice of the residue array after the first matching element # note the last, we finish the loop once we found $sorted_nums[0] my $pos = 0; foreach(@start_incline) { (@inline = @start_incline[$pos+1 .. $#start_incline], last) if ($_ + == $sorted_nums[0]); ++$pos; } # do work with it foreach(@incline) { print $_ . " "; }

    If you intend to splice of the residue after the last matching element:

    my $pos = $#start_incline; foreach(reverse @start_incline) { (@inline = @start_incline[$pos+1 .. $#start_incline], last) if ($_ + == $sorted_nums[0]); --$pos; }
    I hope this helps a bit, as I don't understand the question, I doubt it ;-)

    regards,
    tomte


    Edit: fixed comment in snippet ($#start_incline became $sorted_nums[0])
    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: saving rest of array after a match
by thinker (Parson) on Jun 27, 2003 at 10:25 UTC
    Hi,

    If the reason you want the spaces is simply to print the array, perhaps it would help to note that this can be done simply by surrounding the array in the print statement by double quotes. ie
    print "@incline";
    hope this helps

    thinker

      That works in the assignment operator too:

      use Data::Dumper; @a=(1..10); $b="@a"; print Dumper(\@a,$b); print "Array: @a\n"; print "Scalar: $b\n"; ## OUTPUT ######################## $VAR1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; $VAR2 = '1 2 3 4 5 6 7 8 9 10'; Array: 1 2 3 4 5 6 7 8 9 10 Scalar: 1 2 3 4 5 6 7 8 9 10