Here's an example of a some code that will take a defined "pattern" for a datafile, and then parse input from the datafile based on the pattern. It could probably be trimmed up a lot, but it may be useful!

#it definately needs a little cleanup later... if (! $in{'batchfile'} ) { die("You must select a file to be uploaded"); } else { #Any formatting information. Text or numbers are grabbed as fi +elds #This can be in almost ANY format/order as you want, so long #as text/numbers are only used as fields, and no weird need-to +-be-escaped #stuff #(note: you could modify this to grab it as the first line fro +m the uploaded file) $format=$in{'format'}; #Batchfile is the data input file $data=$in{'batchfile'}; my @records; $reg_format_in=$format; #replace any words/numbers with a regexp... $reg_format_in =~ s/([\w|\d]+)/\(\[\\w\|\\d\]\+\)/gi; #replace any spaces with a regexp for spaces $reg_format_in =~ s/\s+/\\s\+/gi; $_ = $format; my $order=0; my @matches; #go through each matched pattern, mark the index and what was +in that position while ( m/([\w|\d]+)/g) { $matcheditem=$1; $indx =index($format, $matcheditem); $matches[$order]=$matcheditem; $order++; } $matchcount=@matches; @lines = split(/\n/, $data); foreach $line(@lines) { my $pattern; $_ = $line; my @rmatch = m/^$reg_format_in/g; $reccount=@records; #this makes a $records[x]{name} per each line for (my $indx=0; $indx<$matchcount; $indx++) { $records[$reccount]{$matches[$indx]}=$rmatch[$indx]; } } }

In reply to Randomly parsed fields by darkphorm

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.