What did I screw up?

W

Wesley Groleau

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?
 
D

dw

Wesley Groleau said:
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+(.*))?/
 
W

Wesley Groleau

dw said:
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.
 
E

Emil

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
 
W

Wesley Groleau

Emil said:
The script seem OK, except for:


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
 
E

Eric J. Roode

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

# 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-----
 
W

Wesley Groleau

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top