quasimodem has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I wrote this simple prog but can't figure why the quotes wont go away:
open(PBXFILE, "c:\/gabi\/global_address.txt") || die "cannot open: $!" +; while (<PBXFILE>) { chomp; @datafile=<PBXFILE>; foreach $line (@datafile) { {tr/"//} print $line; } }
Can someone show me my error?

Replies are listed 'Best First'.
Re: eliminate quotes
by ysth (Canon) on Jul 29, 2008 at 00:11 UTC
    $line =~ tr/"//d;

    By default, tr, like m and s, acts on $_. It also has defaulting rules for the right side that prevent actually deleting characters unless you specify the /d modifier.

    Also, you are both reading the whole file into an array of lines and reading through it with a while loop. Do one or the other, not both. The way it is, you read the first line (into $_), then read the remaining lines into the array that you then loop over. Because you've reached the end of the file, the second time your while condition is tested, it is false.

    Update: added /d

Re: eliminate quotes
by Lawliet (Curate) on Jul 29, 2008 at 00:13 UTC

    Try:

    open(PBXFILE, '<', "c:\/gabi\/global_address.txt") || die "cannot open +: $!"; my @datafile = <PBXFILE>; foreach my $line (@datafile) { $line =~ s/"//g; #Thanks, almut print $line; }

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
      $line =~ s/"//;

      Or s/"//g, if there's more than one quote per line.

      tr/"//d  (note the d) would work just as well, though.

        If you're using s///, and there's more than one quote per line, I'd use s/"+//g;. That ought to be somewhat faster if there are many quotes in a row. Of course, typical data doesn't have many quotes in a row, but the general principle hold, if you are deleting sub patterns, s/(?:PAT)+//g is usually faster than s/PAT//g.
      thank you very much still learning here

        ;)

        Just make sure you understand the answer.

        <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>