What's happening in this Perl foreach loop? -
i have perl code:
foreach (@tmp_cycledef) { chomp; ($cycle_code, $close_day, $first_date) = split(/\|/, $_,3); $cycle_code =~ s/^\s*(\s*(?:\s+\s+)*)\s*$/$1/; $close_day =~ s/^\s*(\s*(?:\s+\s+)*)\s*$/$1/; $first_date =~ s/^\s*(\s*(?:\s+\s+)*)\s*$/$1/; #print "$cycle_code, $close_day, $first_date\n"; $cycledef{$cycle_code} = [ $close_day, split(/-/,$first_date) ]; }
the value of tmp_cycledef
comes output of sql query:
select cycle_code,cycle_close_day,to_char(cycle_first_date,'yyyy-mm-dd') cycle_definition d order cycle_code;
what happening inside for
loop?
huh, i'm surprised no 1 fixed :)
it looks person wrote trying trim leading , trailing whitespace each field. it's odd way that, , reason overly concerned interior whitespace in each field despite anchors.
i think should same trimming whitespace around delimiter in split:
foreach (@tmp_cycledef) { s/^\s+//; s/$//; #leading , trailing whitespace on whole string ($cycle_code, $close_day, $first_date) = split(/\s*\|\s*/, $_, 3); $cycledef{$cycle_code} = [ $close_day, split(/-/,$first_date) ]; }
the key thinking split
considering parts of string want throw away, not separates fields want.
Comments
Post a Comment