Hi

I have a text file in the format:

ID1 > 66 ID2 < 7 ID3 between 6 10
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.

I have tried several different ways of building the strings, but always get stuck and lost. For example

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.='|'; } } }
But this outputs the following:
$parameters = ID1|ID2|ID3 $operators = >|<|between $values1 = 66|7|10 $values2 = 10|
It doesn't loop the required times for $values2 and if a between is in a different place, things really get screwed.

There must be a simpler and cleaner way to do this type of manipulation.

Thanks - sorry for the length!

-----

Eschew obfuscation, espouse elucidation!


In reply to Building up a string from a text file by joec_

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.