in reply to Re^2: Error Undefined subroutine &main::OPEN called at C:\Perl\sku_corr2.pl line 8.
in thread Error Undefined subroutine &main::OPEN called at C:\Perl\sku_corr2.pl line 8.
I would change split (/\t/, $_, 37); to split (/\t/, $_);
As a reference see Perl Doc on split(). Split is a surprisingly complicated critter! One reason to use a limit param is when you have many, many fields on a line and you just want the first n of them. In your case, you want 37 variables, so the limit should actually be 38!! - one more than the number of variables that you want so that all the kruft gets assigned to an unseen 38th variable in this case. Right now, if there was a 38th, 39th field, they would all get crammed into your $SKU variable (the 37th variable).
As it turns out, with the syntax that you are using, if you omit the limit, Perl will count up all of the my($x,$y,$z...) variables, see that you have 37 of them, then auto-magically supply a limit of 38 to split! So the best answer is to leave this number off and let Perl do the counting!
So what happens if there isn't enough data to match up with all of these 37 variables in the my($x,$y,$z...)list, e.g. the data line is too short? You will get null strings, not undef for the variables for which there is no data. I demo this as example 1 below. If you add $h after $g, $h will get a null string from the split and there will be a total of 8 variables assigned.
Another reason to use a limit is that this overrides the default of discarding any trailing empty fields on the input line. I demo this in example 2,3 below. Here I used arrays so Perl can't count like it can with the OP's code and -1 is used as a limit to mean "huge limit".
#/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 ''
|
|---|