in reply to Re^2: CLI using hashes
in thread CLI using hashes

You would need to be well into the tens of thousands of entries before the hash started having problems. And at that point, the problems would be memory-related, not cpu-related, so splitting the hash wouldn't help. In short, you should never need to worry about if a hash is getting too big, up until the point at which you should have your data in a database, anyway.

Replies are listed 'Best First'.
Re^4: CLI using hashes
by bahadur (Sexton) on May 30, 2005 at 02:54 UTC
    ok now my code looks like this
    sub settime($){ print "nMetrics>You have entered the settime command\ +n"; print "nMetrics>The argument given is $_[0]\n" +;} sub showtime{ print "nMetrics>You have entered the show time c +ommand\n";} sub error{ print "nMetrics>Invalid command line options\n";} $myhash{"set time"}=\&settime; $myhash{"show time"}=\&showtime; $myhash{default}=\&error; print "Welcome to nMetrics application monitor\n"; print "Please type in a command\n"; label:print "nMetrics>"; while(<STDIN>) { chomp; if (exists $myhash{$_}) {$myhash{$_}->();} else {$myhash{default}->();} goto label; }
    i cant understand how i am going to pass arguments to my sub routines. can u just specify who i can pass arguments in this sort of a set up?

      Since you have 'commands' that are more than one 'word', you are going have trouble deciding how to split your user commands from your user arguments.

      One idea would be to build a regex from the keys of your command hash, which splits the command from the remainder, then pass that remainder to the dispatch table:

      # in init phase: my $commands = join "|", keys %myhash; my $command_regex = qr/^($commands)\s*(.*)$/i; # in input loop: while(<STDIN>) { if ( m/$command_regex/o ) { my $command = lc $1; $myhash{ $command }->( $2 ); } else { $myhash{"error"}->( $_ ); } }

      But I think that a much better idea is to make 'set' and 'show' top-level commands with a list of variables that they can set and show. Then a simple m/^(\w+)\s+(.*)$/i can split your commands from arguments, then test $1 for definedness as before, and feed in $2 as args.

      Some nits: If "default" is an error, then I would call it "error". Also, %myhash is a terrible name for a variable.

      Along with the fine comments by fishbot_v2, I'd also like to point out that your subroutines need not be named. This is the perfect situation to use anonymous subroutines. Here's how I would define your hash:

      my %commands = ( "set time" => sub { print "nMetrics>You have entered the settime command\n"; print "nMetrics>The argument given is $_[0]\n"; }, "show time" => sub { print "nMetrics>You have entered the show time command\n"; }, default => sub { print "nMetrics>Invalid command line options\n"; } );
        well the thing is that this is initial code which is going to grow in length. so i need to define sub routines clearly in case i leave the programmer leaving me can understand the code easily. plus can u just tell how u are passing the arguments to the routines.
        can u just give a snippet of how it is going to work in the while loop