#/usr/bin/perl -w use strict; #### SPLIT DEMO ##### ######## example 1 # spliting to list of variables my $y = 'a:b::'; print "***test string is \'$y\'\n"; my @tok = my ($a,$b,$c,$d,$e,$f,$g) = split(/:/, $y); print "number of created fields, with a-g variables: ".@tok."\n"; print "g=\'$g\'\n"; my $cnt=1; foreach (@tok) { print $cnt++," \'$_\'\n"; } my $x = '::a :b::c:::'; print "***test string is \'$x\'\n"; ####### example 2 splitting to an array #this will split into 6 things, because trailing null fields #are omitted with this form of split my @list = split(/:/,$x); # two arg from of split print 'split(/:/,$x)' ." total tokens are: ". @list. "\n"; ####### example 3 splitting to an array #this will split into 10 fields @list = split (/:/,$x, -1); # 3 arg form of split, -1 "huge limit" print 'split (/:/,$x, -1)'." total tokens are: ". @list . "\n"; print "-1 limit includes the trailing null fields...\n"; $cnt=1; foreach (@list) { print $cnt++," \'$_\'\n"; } __END__ Print out: ***test string is 'a:b::' number of created fields, with a-g variables: 7 g='' 1 'a' 2 'b' 3 '' 4 '' 5 '' 6 '' 7 '' ***test string is '::a :b::c:::' split(/:/,$x) total tokens are: 6 split (/:/,$x, -1) total tokens are: 9 -1 limit includes the trailing null fields... 1 '' 2 '' 3 'a ' 4 'b' 5 '' 6 'c' 7 '' 8 '' 9 ''