YUANya

导航

 

数组到foreach

#!/usr/bin/envperl

use strict;
use warnings;


foreach (qw( a b c )) 
{
  print "$_";
}
  print "\n"; 

##abc

foreach ('a','b','c' ) 
{
  print "$_";
}
  print "\n"; 

##abc

print qw(a\\b) . '=' . 'a\\b' . "\n"; 

##a\b=a\b

my ($a,$b,$c);($a,$b,$c) = qw[4]; 

print "$a,$b,$c\n";

##Use of uninitialized value $b in concatenation (.) or string at test.pl line 29.
##Use of uninitialized value $c in concatenation (.) or string at test.pl line 29.
##4,,

my @arr = qw{apple banana peach};my @arr_nothing = ();my $arr_add = "orange";

foreach ( @arr , @arr_nothing , $arr_add , "grape" )
{
  print "$_\t";
}
print "\n";

my @arr_complete = ( @arr , @arr_nothing , $arr_add , "grape" );my @new_arr =  @arr_complete ; 

foreach ( @new_arr )
{
  print "$_\t";
}
print "\n";

##apple	banana	peach	orange	grape	
##apple	banana	peach	orange	grape

pop @new_arr;

##void context

push @new_arr, @arr_complete;

foreach ( @new_arr )
{
  print "$_\t";
}
print "\n";

##apple	banana	peach	orange	apple	banana	peach	orange	grape	

push ('a','b','c'), @arr_complete;

##push on reference is experimental at test.pl line 68.
##Type of arg 1 to push must be array (not constant item) at test.pl line 68, near "'c')"
##Execution of test.pl aborted due to compilation errors.

my @letter = ('a','b','c','d','e','f','g','h');

my @new_letter = splice @letter , 2 ;

foreach(@letter)
{
  print "$_";
}
print "\n";

foreach(@new_letter)
{
  print "$_";
}
print "\n";

##ab
##cdefgh

my @letter1 = ('a','b','c','d','e','f','g','h');

my @new_letter1 = splice @letter1 , 2 ,3 ;

foreach(@letter1)
{
  print "$_";
}
print "\n";

foreach(@new_letter1)
{
  print "$_";
}
print "\n";

##abfgh
##cde

my @letter2 = ('a','b','c','d','e','f','g','h');

my @new_letter2 = splice @letter2 , 2 ,3, ( 'x','y' ) ;

foreach(@letter2)
{
  print "$_";
}
print "\n";

foreach(@new_letter2)
{
  print "$_";
}
print "\n";

##bxyfgh
##cde

$" = "\n";print "@letter2\n";

$" = "****";print "@letter2\n";

##b
##x
##y
##f
##g
##h
##a****b****x****y****f****g****h

my $int = "4*1";print "$letter2[$int]\n";  #    $letter2 = ( b , x , y , f , g , h );

my $int = 4*1;print "$letter2[$int]\n";

my $int = '4*1';print "$letter2[$int]\n";

##f
##f
##Argument "4*1" isn't numeric in array or hash lookup at test.pl line 147.
##f

my $letter2 = "apple";

print "${ letter2 }[ 4 ]\n";

print "$letter2 \[ 4 \] \n";

print "$letter2[ 4 ] \n";

##apple[ 4 ]
##apple [ 4 ] 
##f 

my $i;

foreach $i (@new_letter2)
{
  print "$i";
}
print "\n";

##cde

$" = "";print "@new_letter2\n";

##cde

问题一:这里无论字符串或数字最后都转化为数字了,并不是书上写字符串即是数字1

my $int = "4*1";print "$letter2[$int]\n";  #    $letter2 = ( b , x , y , f , g , h );

my $int = 4*1;print "$letter2[$int]\n";

my $int = '4*1';print "$letter2[$int]\n";

##f
##f
##Argument "4*1" isn't numeric in array or hash lookup at test.pl line 147.
##f
posted on 2019-06-11 01:58  YUANya  阅读(125)  评论(0编辑  收藏  举报