Usage of Perl function splice Z

Source: Internet
Author: User


From: http://blog.chinaunix.net/u/9861/showart_727153.html

Usage of Perl function Splice:

Splice has four usage options:

1. splicearray, offset, length, list

2. splicearray, offset, Length

3. splicearray, offset

4. splicearray

 

Description: removes the elements specified by offset and length in the array, and replaces the elements in the list. In the context of the list, the function returns the overflow array element. In the scalar context, return the last overflow element. If no element is removed, return UNDEF.

NOTE: If offset is a negative value, it starts from the end of the array. If length is omitted (in the third case), the elements from offset to the end are removed. If length is negative, all elements from offset to tail are removed except the length element at the end of the array. If both offset and length are empty, remove all elements. If offset is out of bounds, Perl will prompt a warning and insert list or null at the end of the array.

 

1. splicearray, offset, length, list

 

#!/usr/bin/perl -w

  use strict;
  my
@rocks
= qw(talc quartz jadeobsidian);
  my
@tmp
= qw(hell oworld);
  splice(@rocks,1,2,@tmp);
  foreach
(@rocks){
        print$_."##";
  }
  print
"\n";

[Root @ localhost ~] # Perl refs. pl

Talc ## hell ## oworld ## obsidian ##

 

2. splicearray, offset, Length

#!/usr/bin/perl -w

  use strict;
  my
@rocks
= qw(talc quartz jadeobsidian);
  my
@tmp
= qw(hell oworld);
  splice(@rocks,1,2);
  foreach
(@rocks){
        print$_."##";
  }
  print
"\n";

 [Root @ localhost ~] # Perlrefs. pl

 Talc ##

 

 

3. Splice array, offset

#!/usr/bin/perl -w

  use strict;
  my
@rocks
= qw(talc quartz jadeobsidian);
  my
@tmp
= qw(hell oworld);
  splice(@rocks,2);
  foreach
(@rocks){
        print$_."##";
  }
  print
"\n";

 [Root @ localhost ~] # Perlrefs. pl

 Talc # quartz ##

 

4. Splice Array

#!/usr/bin/perl -w

  use strict;
  my
@rocks
= qw(talc quartz jadeobsidian);
  my
@tmp
= qw(hell oworld);
  splice(@rocks);
  foreach
(@rocks){
       print$_."##";
  }
  print
"\n";

 [Root @ localhost ~] # Perlrefs. pl

 Print Blank

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.