On Mon, 3 Mar 2003, 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> > > I'd *LOVE* to be able to use GetOpt::Std - but I wasn't able to get it to > look at something other than @ARGV - I need to process files on the fly > that have the lines as delineated in __DATA__, below. > > My goals are to see if the -x option exists, and if it does get its > argument. I also need to do certain things based on -h and -r and their > respective arguments. This would all tied to the argument of -x (I guess > this would be the key to everything). -x arg always exists on these lines, > while the others may or may not. The argument to -x is the root of > everything else. > > I can see how I'd do this if I could use GetOpt::Std - is this possible > given that it is not actually coming from the commandline? > > Here's my script... as it stands now: > > #!/bin/perl -w > > use warnings;
You can have either the -w flag or use warnings, you don't have to turn them both on :-) > use strict; > my %cmdLine; Move the above declaration inside the while loop > my %newHash; > my $DBG = 1; > my ($onceMore, $opt, $arg); You are not using $onceMore anywhere in the script. You can also move the declaration of $opt and $arg after the while loop. > > while (<DATA>) { > chomp; > > my ($adx, $rest) = (split(/:/,$_)); > if ($DBG) {print "\$adx=\t<$adx>\n\$rest=\t<$rest>\n";} > > while (defined ($rest) && $rest ne "") { > my ($opt, $arg, $newRest) = ($rest =~ /\s*([\-a-z]*)\s+([^ ]*)\s+(.*)/); The problem is with the last \s+, when you are parsing the final tuple $rest contains this '-h ten-me-900'. Due to the final \s+ the above regex will not match. This leaves $opt, $arg and $newRest as undefined. Change the final \s+ to \s* and you will be fine. The above regex can actually be written as $rest =~ /^\s*(-[a-z])\s*(\S+)(.*)$/ Anchor your regexes > $rest = $newRest; > $cmdLine{$opt} = $arg; An undefined $opt would mean assigning to an undefined hash key. This throws the 'Uninitialized ....' warning (thanks to -w or use warnings) > } > You can comment out the following three line > # parse the final tuple... > my ($opt, $arg, $newRest) = ($rest =~ /\s*([\-a-z]*)\s+([^ ]*)\s+(.*)/); > $cmdLine{$opt} = $arg; > > # 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 Getopt::Std is still the safest and easiest solution :-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]