in reply to Useless Program

That regexp shouldn't have the $ escaped. That makes it search for the literal string '$name', not the value of $name.

Try this:

if (/^$name$/i) {
You may want to quote any meta-characters in the name:
if (/^\Q$name\E$/i) {
Your other, bigger problem, as cds pointed out, is that you're only searching the first line. If the match isn't in the first line, you then ask whether or not to add the name. You'll want to fix this.

My suggestion, in fact, would be to use a DBM file. DB_File, perhaps.

Replies are listed 'Best First'.
RE: Re: Useless Program
by BBQ (Curate) on Jun 02, 2000 at 00:17 UTC
    You're obviously right about the regex, but would you need it in the first place? If your file looks like
    foo bar baz
    and the input is "foo\n", why would you want to:
    • chop input trailing \n
    • chop each line's trailing \n
    • regex on /^ AND $/??
    am I missing something, or isn't it just easier to say $name eq $_ and leave it at that?

    #!/home/bbq/bin/perl
    # Trust no1!
      Well...if you don't use ^ and $ it could match a name that includes the name you are searching for...for example if $name = 'bob' it would match bobbie, bobby, etc... Just a thought... --Amber
      No, you're right--in this case, that's much easier, faster, and better. I suppose I was just giving general advice (quoting meta-characters, not escaping $, etc.).

      Of course, the easiest would probably be just to use tie and a DBM, as was mentioned (and for which the code was even provided).

        Actually, you'd make a better teacher than myself. :)

        I was sticking strictly to the problem at hand, while you were expanding it and recommending good practices. I tend to have a very focused-on-the-job frame of mind when I read other people's code. I'll be as stupid as a debugger. At any rate, the DBM suggestion makes more sense anyway. I was just being picky.

        #!/home/bbq/bin/perl
        # Trust no1!