Hi everyone.
Im new to perl and this is my first prac program....so please excuse me if
its seems a little basic  :)

Anyway heres the problem...

My program writes to a file form the command line using the command 'new'.

for instance :
input: new a 1         writes a:1:0   to file

input: new b 2         writes b:2:0   to file

so file is
a:1:0
b:1:0   and so on as i add new data where the 0 represents a counter.

Now i need to implement a new command called 'find', which finds the number
associated with the letter and displays it.

for instance:
input: find a            prints out 1
and
input: find b            prints out 2  and so on
also each time i display the number using the 'find' command i need to
increment the counter for that letter.

e.g.
input: find a        prints out 1 and  file becomes
a:1:1
b:1:0     and so on

Anyway...hope this is making sense....heres my program so far:

#! /usr/bin/perl

print "input: ";
chop($input_string = <STDIN>);
@compare = split(/ /, $input_string);
  if ($compare[0] =~ "new"){
  &NEW($compare[1], $compare[2]);
  }
  elsif ($compare[0] =~ "find") {
  &FIND($name, $phone, $count);
 print ("yes\n");
  }
  else{
  print ("Unknown command!");
  }

sub NEW {
 open (APPEND, ">>prac1.txt") or die "$! file error";
 print APPEND $compare[1], ":", $compare[2], ":", $i=0, "\n";
 close APPEND;
}

sub FIND {
open (FIND, "prac1.txt") or die "$! file error";

while (<FIND>) {
  ($name, $phone, $count) = split(/:/, $_);
  if ($name =~ $compare[1]) {
    print "$phone\n";
    close FIND;
    open (APPEND, ">>prac1.txt") or die "$! file error";
    $count++;
    print APPEND $count;
    close APPEND;
  }
}

The sub routine FIND is where i am having troubles.  I am able to display
the number asscoiated with the letter, however I am having trouble
substituting the counter with the new count.
Any ideas would be appreciated so much!!
thanks in advance
Patrick Bateman






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

Reply via email to