set a variable with a specified element of an array...and all elements that follows

Discussion in 'Perl Misc' started by gniagnia, Feb 16, 2007.

  1. gniagnia

    gniagnia Guest

    Hi all,


    My array @array contains AT LEAST 7 elements.
    I would like to set a variable that will contain all elements of this
    array beginning from the 7th elements.

    Is it easily doable?

    thanks in advance.
     
    gniagnia, Feb 16, 2007
    #1
    1. Advertisements

  2. gniagnia

    Mirco Wahab Guest

    ...
    my @array = qw' 1 2 3 4 5 6 7 8 9 10 '; # qn{ .. } is missing :-/

    my $index = 7;

    # just copy elements above (non destructive)
    my @over7n = @array[$index-1 .. @array-1];

    # concatenate elements into string
    my $over7s = join ',', @array[$index-1 .. @array-1];

    # remove these elements from source array (destructive)
    my @over7d = splice @array, $index-1;
    ...

    please look up:

    array slice
    splice

    Regards

    Mirco
     
    Mirco Wahab, Feb 16, 2007
    #2
    1. Advertisements

  3. gniagnia

    anno4000 Guest

    Sure, that's an array slice (see perldata).

    my @tail = @array[ 6 .. $#array]

    Anno
     
    anno4000, Feb 16, 2007
    #3
  4. gniagnia

    gniagnia Guest


    It's working!
    Thanks a lot for your help.
     
    gniagnia, Feb 16, 2007
    #4
  5. gniagnia

    Mirco Wahab Guest

    [ somwhere in germany (',' is "decimal point" all over the place) ]

    use locale;
    my @array = qn{ 3,1415 2,71828 0,57721 10^4 2^16 1+1 }

    map print "$_,\n", @array;


    3.14150
    2.71828
    0.57721
    10000
    65536
    2


    Regards

    M.
     
    Mirco Wahab, Feb 16, 2007
    #5
  6. gniagnia

    Dr.Ruud Guest

    Mirco Wahab schreef:
    ITYM:
    my $over7s = join ',', @array[$index-1 .. $#array];
     
    Dr.Ruud, Feb 16, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.