What did I screw up?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wesley Groleau

    What did I screw up?


    Actually, 95% of this is copied from something else
    that supposedly works. (And from what I know of perl,
    I thought it would work, too.)

    But in a loop,

    print $_ . "\n"; # first line is "0 HEAD"
    # second is "1 SOUR hand-edited blah blah ...."

    # Parse record
    /^(\d+)\s+(@\S+@ )?\s*(\S+)\s+(. *)/;
    $level = $1; # Save the level number
    $label = $2; # Save the label (if specified)
    $tag = uc($3); # Uppercase the tag
    $text = $4; # Save everything else

    print " " x $level . "$label=$tag=$t ext\n";
    ALL the variables in this print are uninitialized on every pass!

    Am I missing something?

  • dw

    #2
    Re: What did I screw up?


    "Wesley Groleau" <wesgroleau@myr ealbox.com> wrote in message
    news:ykidnQJyJL lf3bKiU-KYuQ@gbronline. com...[color=blue]
    >
    > print $_ . "\n"; # first line is "0 HEAD"
    > # second is "1 SOUR hand-edited blah blah[/color]
    ....."[color=blue]
    >
    > # Parse record
    > /^(\d+)\s+(@\S+@ )?\s*(\S+)\s+(. *)/;
    > $level = $1; # Save the level number
    > $label = $2; # Save the label (if specified)
    > $tag = uc($3); # Uppercase the tag
    > $text = $4; # Save everything else
    >
    > print " " x $level . "$label=$tag=$t ext\n";
    > ALL the variables in this print are uninitialized on every pass![/color]

    what are the @'s in there for?

    /^(\d+)\s+(\S+)? \s*(\S+)\s+(.*)/

    However, this still doesn't quite look like what you want... since your
    first line had just the level and label....

    /^(\d+)\s+(\S+)\ s+(?:(\S+)\s+(. *))?/


    Comment

    • Wesley Groleau

      #3
      Re: What did I screw up?

      dw wrote:[color=blue]
      > "Wesley Groleau" <wesgroleau@myr ealbox.com> wrote in message
      > news:ykidnQJyJL lf3bKiU-KYuQ@gbronline. com...
      >[color=green]
      >> print $_ . "\n"; # first line is "0 HEAD"
      >> # second is "1 SOUR hand-edited blah blah[/color]
      >
      > ...."
      >[color=green]
      >> # Parse record
      >> /^(\d+)\s+(@\S+@ )?\s*(\S+)\s+(. *)/;
      >> $level = $1; # Save the level number
      >> $label = $2; # Save the label (if specified)
      >> $tag = uc($3); # Uppercase the tag
      >> $text = $4; # Save everything else
      >>
      >> print " " x $level . "$label=$tag=$t ext\n";
      >> ALL the variables in this print are uninitialized on every pass![/color]
      >
      >
      > what are the @'s in there for?[/color]

      The protocol (GEDCOM) has some records with a label or
      ID always delimited by at-signs.
      [color=blue]
      > /^(\d+)\s+(\S+)? \s*(\S+)\s+(.*)/
      >
      > However, this still doesn't quite look like what you want... since your
      > first line had just the level and label....
      >
      > /^(\d+)\s+(\S+)\ s+(?:(\S+)\s+(. *))?/[/color]

      Level is the number, first field. Label is not in either
      of the first two lines, but it is in the following line:

      0 @WWG-1954@ INDI

      But the problem was that later lines got error messages
      about using uninitialized variables. And those variables
      happened to be $level, $label, and $tag. (I haven't tried
      to use $text yet.)

      If there is something wrong with a /regexp/ does it
      just do nothing at all? It generated no error message
      or warning (I used -w on invocation) at that point.
      And the debugger stepped over it without comment.

      Comment

      • Emil

        #4
        Re: What did I screw up?

        Wesley Groleau <wesgroleau@myr ealbox.com> wrote in message news:<ykidnQJyJ Llf3bKiU-KYuQ@gbronline. com>...

        The script seem OK, except for:
        [color=blue]
        > /^(\d+)\s+(@\S+@ )?\s*(\S+)\s+(. *)/;[/color]

        are the "@" characters really suposed to be in file. If not try without them, so:

        /^(\d+)\s+(\S+)? \s*(\S+)\s+(.*)/;

        Emil

        Comment

        • Wesley Groleau

          #5
          Re: What did I screw up?

          Emil wrote:[color=blue]
          > The script seem OK, except for:
          >[color=green]
          >> /^(\d+)\s+(@\S+@ )?\s*(\S+)\s+(. *)/;[/color]
          >
          > are the "@" characters really suposed to be in file. If not try without them, so:[/color]

          OK, here's the format:

          Every line has a level number and a tag, in that order.

          1 MARR

          Some lines have a label _between_ the level and the tag.

          0 @WWG-1954@ INDI

          ALL labels are bracketed by at-signs

          Some lines have a cross-ref after the tag

          1 FAMC @WWG-MTD-1987@

          NO lines have both label and cross-ref

          Some lines have text after the tag.

          1 NAME W. Wesley /Groleau/

          The problem is not in finetuning the regexp. The problem is that
          the regexp is NOT DEFINING $1, $2, etc. even though it has sections
          in parentheses.

          The first non-space character on EVERY line is a digit.
          So with

          /^(\d+)\s+(..... .
          $level = $1;
          print $level;

          why does perl complain that $level is uninitialized,
          but NOT complain about the regexp?

          I am beginning to wonder whether there is something wrong with
          my installation of perl 5.6

          Comment

          • Eric J. Roode

            #6
            Re: What did I screw up?

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            Wesley Groleau <wesgroleau@myr ealbox.com> wrote in
            news:ykidnQJyJL lf3bKiU-KYuQ@gbronline. com:
            [color=blue]
            > # Parse record
            > /^(\d+)\s+(@\S+@ )?\s*(\S+)\s+(. *)/;[/color]

            Hi Wesley,

            I just wanted to chime in here and point out two things that nobody else
            has mentioned yet:

            First, you should *always* test the success or failure of a regex match!
            As follows:

            if (/^(\d+)\s+(@\S+@ )?\s*(\S+)\s+(. *)/)
            {
            ....it succeeded....
            }
            else
            {
            ....it failed to match....
            }

            Second, comp.lang.perl is a defunct newsgroup. You'll get a better
            response if you post to comp.lang.perl. misc, which is where general Perl
            questions should be posted.

            Good luck,
            - --
            Eric
            $_ = reverse sort qw p ekca lre Js reh ts
            p, $/.r, map $_.$", qw e p h tona e; print

            -----BEGIN PGP SIGNATURE-----
            Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

            iQA/AwUBPzDRj2PeouI eTNHoEQIXaACg+I +6/qDR6OMBCilT9csZ p+l/Vm4AoL9Z
            V3b0cWj7poriArS 3k8sFrta2
            =NGTh
            -----END PGP SIGNATURE-----

            Comment

            • Wesley Groleau

              #7
              Re: What did I screw up?

              [color=blue]
              > First, you should *always* test the success or failure of a regex match![/color]

              Good advice. Wish I'd thought of it. I know now. :-)
              [color=blue]
              > Second, comp.lang.perl is a defunct newsgroup. You'll get a better
              > response if you post to comp.lang.perl. misc, which is where general Perl[/color]

              Ah, thanks.

              Comment

              Working...