免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 1955 | 回复: 14
打印 上一主题 下一主题

有关数组 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-11 23:52 |只看该作者 |倒序浏览
我想问个问题,比如我有一个数组@a=(aa,bb,cc,dd,ee,ff,ad,fe,ag,hh,ssd,ffg,h,j,k,l,asdfag)可能有上百个,现在我这么操作
for($i=0;$i<=$#a;$i++)
{
   if($a[$i]=~/ee/)
  {
     $a = $i;
  }
  elsif($a[$i]=~/j/)
{
   $b = $i;
}  
}
大致是这样。我想要的是提取dd到k之间的所有元素放到新的数组中(包括dd和k)并且将他们在元数组中删除。。我应该怎么做啊?
我最初的想发是push(@number,$a[$a-1..$b+1]);不过好像这样操作是无效的。。
谁会的话请指教下。谢谢

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 2012-12-12 00:56 |只看该作者
回复 1# zlzty
  1. perl -le '
  2. @a=(aa,bb,cc,dd,ee,ff,ad,fe,ag,hh,ssd,ffg,h,j,k,l,asdfag);
  3. map{$i++;if(/dd/../k/){push @b,$_;splice(@a,--$i,1)}}@a;
  4. print "@b","\n","@a"'
  5. dd ee ff ad fe ag hh ssd ffg h j k
  6. aa bb cc l asdfag
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-12-12 02:26 |只看该作者
  1. #!/usr/bin/perl

  2. use strict;
  3. use warnings;

  4. my @arr = qw(aa bb cc dd ee ff ad fe ag hh ssd ffg h j k l asdfag);
  5. my @filtered = grep { /^dd$/../^k$/ } @arr;

  6. my %final;
  7. @final{@arr} = ();
  8. delete @final{@filtered};
  9. my @output = keys %final;
  10. print "@output";
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-12-13 14:34 |只看该作者
3Q回复 3# iLRainyday


   

论坛徽章:
0
5 [报告]
发表于 2012-12-13 14:34 |只看该作者
3Q回复 2# yinyuemi


   

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
6 [报告]
发表于 2012-12-13 15:56 |只看该作者
yinyuemi 发表于 2012-12-12 00:56
回复 1# zlzty

请问下怎么把握$i的变化,我重写成foreach的形式就不对了

论坛徽章:
0
7 [报告]
发表于 2012-12-13 17:15 |只看该作者
请问splice(@a,--$i,1)这里的1是做什么用的。我记得splice(@a,--$i)这个应该是$i是多少,就删除@a中第几个元素是吗?那1是做什么用的。。我删了1,好像1到最后的元素就都没了回复 2# yinyuemi


   

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
8 [报告]
发表于 2012-12-13 17:20 |只看该作者
就是一次取出1个元素回复 7# zlzty


   

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
9 [报告]
发表于 2012-12-13 17:22 |只看该作者
yinyuemi 发表于 2012-12-12 00:56
回复 1# zlzty
  1. for(@a){
  2.        $i++;
  3.     if(/dd/../k/){
  4.         
  5.           #say "$i";
  6.         splice(@a,--$i,1);
  7.            push @b,$_;
  8.         say "@a","\n","@b";
  9.     }
  10. }
复制代码
哪里不对了。。

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
10 [报告]
发表于 2012-12-14 00:59 |只看该作者
回复 6# 只是一个红薯


    [code]The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. This implicit localization occurs only in a foreach loop.

The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.

If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the foreach loop index variable is an implicit alias for each item in the list that you're looping over.

If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that.

foreach probably won't do what you expect if VAR is a tied or other special variable. Don't do that either.[/code]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP