joec_ has asked for the wisdom of the Perl Monks concerning the following question:
I have a text file in the format:
These values need to be put into strings in such as way as to be inserted into Oracle. So for example, i would have a string $parameters which would contain ID1|ID2|ID3. A second string would be $operators containing >|<|between. A third string would be $values1 containing 66|7|6, and the final string would be $values2 containing ||10. The $values2 string must contain the same number of fields as the others even if the fields are empty (as above). The text file can be in any order i.e. a between may be on the first line.ID1 > 66 ID2 < 7 ID3 between 6 10
I have tried several different ways of building the strings, but always get stuck and lost. For example
But this outputs the following:while ($line=<TEXTFILE>){ @params = split(/\s/,$line); $id = shift @params; $op = shift @params; $parameters.= $id."|"; $operators .= $op."|"; if ($op eq 'between'){ $v = shift @params; $values1 .= $v.'|'; if ($.>1){ $v2 = shift @params; $values2 .= '|'.$v2; }elsif($.==1){ $v2 = shift @params; $values2 .= $v2.'|'; } $values2 .='|'; $between = 1; } foreach $value (@params){ $values .= $value."|"; } unless ($between){ for (@params){ $v2.='|'; } } }
It doesn't loop the required times for $values2 and if a between is in a different place, things really get screwed.$parameters = ID1|ID2|ID3 $operators = >|<|between $values1 = 66|7|10 $values2 = 10|
There must be a simpler and cleaner way to do this type of manipulation.
Thanks - sorry for the length!
-----
Eschew obfuscation, espouse elucidation!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Building up a string from a text file
by linuxer (Curate) on May 22, 2009 at 14:36 UTC | |
|
Re: Building up a string from a text file
by graff (Chancellor) on May 23, 2009 at 04:03 UTC | |
by CountZero (Bishop) on May 23, 2009 at 18:29 UTC |