Hi Deb. Let me put you out of your misery. It's a shame because
you were soooo close!

Deb wrote:
>
> Here's the modified script.  I made some changes, as suggested, but
> there
> was no change in the output.  I've included my entire script.  My
> head is getting mighty flat from banging it against the wall.  Oh,
> and I added "use warnings;" and I haven't got a clue what I need to
> do to fix those.  <sigh>

[snip]

> #!/bin/perl -w
>
> use warnings;
> use strict;
> my %cmdLine;
> my %newHash;
> my $DBG = 1;
> my ($onceMore, $opt, $arg);
>
> while (<DATA>) {
>     chomp;
>
>     my ($adx, $rest) = (split(/:/,$_));
>     if ($DBG) {print "\$adx=\t<$adx>\n\$rest=\t<$rest>\n";}
>
>     while (defined ($rest) && $rest ne "") {

There's no need for this conditional. If $rest is undefined it
will evaluate as false, so

    while ($rest)

will do.


>         my ($opt, $arg, $newRest) = ($rest =~
/\s*([\-a-z]*)\s+([^ ]*)\s+(.*)/);

Here's your problem: the regex is wrong. It may help to use
the /x modifier so that you can use whitespace as layout
instead of meaning literal characters.

    /  \s*  ([\-a-z]*)  \s+  ([^ ]*)  \s+  (.*)  /x

Now you've got your $opt being a list of zero (!) or more
lower-case alphas or hyphens. How about one hyphen
followed by a single small alpha:

    (-[a-z])

You've got at least one space after the option. If you're
sticking to single-character options then none would do:

    \s*

Then you want to grab all non-space character for the
argument:

    (\S+)

followed by the rest of the string for $newRest:

    (.*)

Assembling these, you get:

    /  \s*  (-[a-z])  \s*  (\S+)  (.*)  /x

which handles all of the options correctly so you no longer
need the # parse the final tuple... block below.

>         $rest = $newRest;
>         $cmdLine{$opt} = $arg;
>     }
>
>     # parse the final tuple...
>     my ($opt, $arg, $newRest) = ($rest =~ /\s*([\-a-z]*)\s+([^
>     ]*)\s+(.*)/); $cmdLine{$opt} = $arg;

And that's all, it now works. Well done you!

Cheers,

Rob

>     # Build out the data structure
>     my $masterKey = $cmdLine{'-x'};
>
>     foreach my $opt (keys %cmdLine) {
>         $newHash{$masterKey}{$opt} = $cmdLine{$opt};
>     }
> }
>
> foreach my $masterKey (sort keys %newHash) {
>     print "$masterKey\n";
>     foreach my $opt (sort keys %{$newHash{$masterKey}}) {
>         print "\t$opt\t" . $newHash{$masterKey}{$opt} . "\n";
>     }
> }
>
>
> __DATA__
> ten-me-900: -r timbu -x me-900 -h ten-me-900
> hidit: -r tenbu -x networks-users -h hidit
> postal: -x direct -h postal
> pickit: -h pickit -x uncrp -r oakd





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to