Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Flawless Script problems?

by AcidHawk (Vicar)
on Mar 13, 2003 at 06:41 UTC ( [id://242603]=note: print w/replies, xml ) Need Help??


in reply to Flawless Script problems?

I managed to get it working.

I changed you code a little here is what I came up with.

Firstly...
elsif ($option eq 'f') { print "Who are you looking for?\n"; &find; }
changed to
elsif ($option eq 'f') { print "Who are you looking for?\n"; chomp(my $f = <STDIN>); $f =~s/\cM//g; if (!&find($f)) { print "Name $f cannot be found in the directory.\n"; }
Then I changed you find sub from
sub find { chomp(my $f = <STDIN>); # $f=~tr/\r\z//d; #$f =~s/\r\z//; $f =~s/\cM//g; if (exists $dbm{$f}) { my($name, $number, $email, $addr1, $addr2) = split "\0", $dbm{$f} +; print "$name\n$number\n$email\n$addr1\n$addr2\n\n\n"; } else { print "Name $f cannot be found in the directory.\n"; } }
to look as follows
sub find { my $rc = 0; my $f = shift; foreach my $key ( sort keys %dbm ) { my($name, $number, $email, $addr1, $addr2) = split "\0", $ +dbm{$key}; if ($name eq $f) { print "$name\n$number\n$email\n$addr1\n$addr2\n\n\n"; $rc = 1; last; } } }

Disclaimer: I know nothing about tie and less about SDBM_File but thats why I like perl.

-----
Of all the things I've lost in my life, its my mind I miss the most.

Replies are listed 'Best First'.
Re: Re: Flawless Script problems?
by sulfericacid (Deacon) on Mar 13, 2003 at 07:22 UTC
    Thanks for your help! I still don't understand why the script would work on some computers but not others...for some people the find feature does work as it is, and it should work cause I see nothing wrong with the script anywhere (not saying I would recognize a problem anyways).

    I think I will end up trying to rewrite that entire section of code since what you did is just outside what I understand right now. (I haven't worked with $rc or shift yet). One thing I do find really interesting is if (!&find($f)) { . I understand what it's trying to say but I don't have the foggiest idea how you get from $dbm{$f} to if (!&find($f)) { .

    I will have a look into that too, lol. Thanks for your help, atleast it's working now :)

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      Perhaps a brief explination is in order.

      When you enter the option f (for Find) and ask who you want to look for. The answer is captured into $f. I have then run the &find subroutine passing it the name that was received ie: &find($f).

      This function I enclosed in an if not block to check the return of &find. So if the return, from &find, is 0(fail) then &find could not find a matching name otherwise &find returned a 1(pass).

      The &find subroutine is easy to explain...

      First you set a var to 0 (ie: $rc = 0;) this is what will be returned if NO match is found.

      Next we make a local var for &find = the name ($f) we sent to the sub-routine. This is done using shift.

      After that, all we do is, check if foreach key in the hash, equals the name we entered (now $f in &find). If it matches we set $rc to 1 and stop searching through the hash.
      &find returns the var $rc.

      Hope that doesn't comfuse you further.
      Cheers

      -----
      Of all the things I've lost in my life, its my mind I miss the most.
        Thanks for all your help AcidHawk, I'll review your code some more to ensure I know what you did. Thanks again!

        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        sulfericacid

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://242603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found