Split a scalar to create a list : Array Scalar « Array « Perl






Split a scalar to create a list

    

$string= "ABC:11/12/2009:10 St.:CA, value:012345";
@line=split(":", $string);    # The string delimiter is a colon
print @line,"\n";
print "The guy's name is $line[0].\n";
print "The birthday is $line[1].\n\n";

@str=split(":", $string, 2);
print $str[0],"\n";  # The first element of the array
print $str[1],"\n";  # The rest of the array because limit is 2
print $str[2],"\n";  # Nothing is printed

@str=split(":", $string);  # Limit not stated will be one more than total number of fields
print $str[0],"\n";
print $str[1],"\n";
print $str[2],"\n";
print $str[3],"\n";
print $str[4],"\n";
print $str[5],"\n";

( $name, $birth, $address )=split(":", $string);
print $name , "\n";
print $birth,"\n";
print $address,"\n";

   
    
    
    
  








Related examples in the same category

1.Convert array to scalar
2.Assigning An Array To A Scalar
3.Use a scalar variable as our subscript:
4.Assign array variable to scalar variable
5.Assign array value to a list of scalar variables
6.Assigning an array value to a scalar variabe
7.Reference single array element by using the $ not @
8.Scalar Data in an Array
9.Place scalar variables in lists and arrays.