Greetings Monks, I am trying to grab a bunch of variables from a fixed width file. I have only tried doing it one way so far, it seems to be working okay but is a little unpredictable. Here is how I am doing it now: First I grab a key from a work file, this key will tell me which record to grab from the control file with the variables. They key is held between two astericks at the top of a data file.
sub get_key { open (FOO_TEXT, '/tmp/foo.work')or die "Could not open work file $!\n +"; while (<FOO_TEXT>) { if (/\*([^*]+)\*/) { my $key = $1; get_foocf($key); }
Then I call the sub to grab the line matching the key from the control file:
sub get_foocf { my $key = shift; open (FOO_CF, '/tmp/foocf.dat') or die "Control file unavailable $!"; while (<FOO_CF>) { if (/$key/) { } my $foo_opts = ($'); get_foo_opts($foo_opts); }
Then I take the line grabbed from the control file and turn it into an array where I will grab my variables.
sub get_domopts { my $foo_opts=shift; my @foo_opts = split ( //, $foo_opts ); my $cvtype = $dom_opts[0]; my $cvnumber = join ( "", @foo_opts[ 1 .. 10 ] ); my $dtype = join ( "", @foo_opts[ 11, 12 ] ); my $cdir = join ( "", @foo_opts[ 13 .. 15 ] ); my $dp = join ( "", $foo_opts[16] ); my $dpform = join ( "", @foo_opts[ 17, 18 ] ); my $dpnc = join ( "", $foo_opts[19] ); my $dprntr = join ( "", @foo_opts[ 20 .. 23 ] ); my $dpfill = join ( "", @foo_opts[ 24 .. 73 ] ); my $de = join ( "", $foo_opts[74] ); my $deform = join ( "", @foo_opts[ 75, 76 ] ); my $deadd = join ( "", @foo_opts[ 77 .. 176 ] ); my $defill = join ( "", @foo_opts[ 177 .. 226 ] ); }
I have a few questions about this: 1. Is this this the best way to do this ( I doubt it is )? 2. If this is not the best way to do this , what would be the best way? I want to be ablt to have access to all of the variable from several subroutines in the program. 3. The variables are all different some text some numbers, what is the best way to reference the individual indices:
@foo[2]
or
$foo[2]
I know it really depends on the context, so lets just say all I care about is the text and will not be doing a great deal of conditionals with the indices. I hope this made sense I am just trying to get a better understanding of how to grab variables from external data sources. Thanks in advance for any help ! It is always appreciated. -Aseidas-

In reply to creating variables from a fixed width file by aseidas

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.