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. Advertising

  2. gniagnia

    Mirco Wahab Guest

    Re: set a variable with a specified element of an array...and allelements that follows

    gniagnia wrote:
    > 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?


    ...
    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. Advertising

  3. gniagnia

    -berlin.de Guest

    gniagnia <> wrote in comp.lang.perl.misc:
    > 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?


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

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

    Anno
    -berlin.de, Feb 16, 2007
    #3
  4. gniagnia

    gniagnia Guest

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

    On 16 fév, 09:41, Mirco Wahab <> wrote:
    > gniagnia wrote:
    > > 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?

    >
    > ...
    > 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



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

    Mirco Wahab Guest

    Re: set a variable with a specified element of an array...and allelements that follows

    Glenn Jackman wrote:
    > At 2007-02-16 03:41AM, "Mirco Wahab" wrote:
    >> my @array = qw' 1 2 3 4 5 6 7 8 9 10 '; # qn{ .. } is missing :-/

    >
    > By "qn" I assume you mean a quoted list of numbers.
    >
    > Is the basic list notation so unreadable?
    > @array = (1..10);
    > or
    > @arary = (2,4,6,8,10);


    [ 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:

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


    ITYM:
    my $over7s = join ',', @array[$index-1 .. $#array];

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Feb 16, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikey
    Replies:
    7
    Views:
    810
    Mikey
    Dec 12, 2003
  2. Girish
    Replies:
    1
    Views:
    941
    Fabien R
    Apr 14, 2005
  3. Hari
    Replies:
    3
    Views:
    393
    Edward Elliott
    May 12, 2006
  4. Replies:
    1
    Views:
    375
  5. Johnny
    Replies:
    4
    Views:
    138
    kaeli
    Jan 19, 2004
Loading...

Share This Page