Since FORTRAN is my mother tongue (so to speak) I felt honour bound to make an attempt.

NOTE: I am by no means fluent in regular expressions, but I think this will work.

Provisos:
(1) It assumes that the 'real' or 'integer' statements will be properly ignored, either by having no spaces or commas preceding it in the string, or by explicitly junking anything that looks like a declaration.
(2) It assumes no continuation lines. (I leave that to you).
(3) I haven't tested it thoroughly with spaces etc.

Here it is.

#!/usr/bin/perl -w # # Scenario: Given lines of f77 code, # parse out the comma delimited var+iables. # use Data::Dumper; # A real application will read in f77 source, # this is just a sample. my $string = "real*8 Eparams(0:maxParam),". "Emvm(0:3),". "YourArray(12,(j,k),m),". "MyArray(row,col)"; my $open = 0; my $close = 0; my @fields = (); my $a; # --- get rid or 'real*8' $string =~ /(\w+)(\*[0-9]*)?/gc; # --- keep looping until entire string is processed while (not $string =~ /\G\z/gc) { # --- get variable name if ((not $open) and ($string =~ /\G(?:\s*|,)(\w+)/gc)) { push @fields, $1; print "$1\t"; } # --- find opening brackets, and keep track of level elsif ($string =~ /\G([^\)]*\()/gc) { $a = $1; $open++; } # --- find closing brackets, and print info elsif ($open and ($string =~/\G\s*([^\(]*\))/gc)) { print "-> $a$1\n"; $open--; } } #print "Fields:",Dumper(\@fields); exit(0);
Sandy

In reply to Re: f77 variable list parsing by Sandy
in thread f77 variable list parsing by SlugMass

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.