What did I screw up?

Discussion in 'Perl' started by Wesley Groleau, Aug 5, 2003.

  1. 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=$text\n";
    ALL the variables in this print are uninitialized on every pass!

    Am I missing something?
    Wesley Groleau, Aug 5, 2003
    #1
    1. Advertising

  2. Wesley Groleau

    dw Guest

    "Wesley Groleau" <> wrote in message
    news:...
    >
    > 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=$text\n";
    > ALL the variables in this print are uninitialized on every pass!


    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+(.*))?/
    dw, Aug 5, 2003
    #2
    1. Advertising

  3. dw wrote:
    > "Wesley Groleau" <> wrote in message
    > news:...
    >
    >> 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=$text\n";
    >> ALL the variables in this print are uninitialized on every pass!

    >
    >
    > what are the @'s in there for?


    The protocol (GEDCOM) has some records with a label or
    ID always delimited by at-signs.

    > /^(\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+(.*))?/


    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.
    Wesley Groleau, Aug 5, 2003
    #3
  4. Wesley Groleau

    Emil Guest

    Wesley Groleau <> wrote in message news:<>...

    The script seem OK, except for:

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


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

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

    Emil
    Emil, Aug 5, 2003
    #4
  5. Emil wrote:
    > The script seem OK, except for:
    >
    >> /^(\d+)\s+(@\S+@)?\s*(\S+)\s+(.*)/;

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


    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
    Wesley Groleau, Aug 6, 2003
    #5
  6. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Wesley Groleau <> wrote in
    news::

    > # Parse record
    > /^(\d+)\s+(@\S+@)?\s*(\S+)\s+(.*)/;


    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/AwUBPzDRj2PeouIeTNHoEQIXaACg+I+6/qDR6OMBCilT9csZp+l/Vm4AoL9Z
    V3b0cWj7poriArS3k8sFrta2
    =NGTh
    -----END PGP SIGNATURE-----
    Eric J. Roode, Aug 6, 2003
    #6

  7. > First, you should *always* test the success or failure of a regex match!


    Good advice. Wish I'd thought of it. I know now. :)

    > 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


    Ah, thanks.
    Wesley Groleau, Aug 7, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelmen Wong
    Replies:
    0
    Views:
    337
    Kelmen Wong
    May 4, 2004
  2. Gandalf
    Replies:
    12
    Views:
    515
    Attila Feher
    Sep 16, 2003
  3. Widgeteye

    How did I screw this up?

    Widgeteye, Jul 23, 2004, in forum: Python
    Replies:
    3
    Views:
    354
    Christopher T King
    Jul 23, 2004
  4. Connected
    Replies:
    2
    Views:
    816
    Liquid Technologies
    Oct 22, 2007
  5. Michael Guterl

    gem cleanup screw up...

    Michael Guterl, Jan 1, 2009, in forum: Ruby
    Replies:
    9
    Views:
    139
    Michael Guterl
    Jan 3, 2009
Loading...

Share This Page