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

I've got a text file that looks like
1:5 2:5 3:5 4:5 5:5
I want to get rid of the colons. What I've done is:
open (IN, "myfile.txt") || die "couldn't open $!\n"; my @array; while ( $line = <IN> ) { push @array, (split /\s+, $line )[0]; } foreach $number(@array) { $number = ($number =~ s/://g); } print "@a[0]\n"
---This will yeild "1"
---shouldn't it give me "15" Something wrong w/ the substitute command?

Replies are listed 'Best First'.
Re: Substitution all parts of an array...
by pfaut (Priest) on Mar 14, 2003 at 00:01 UTC
    $number = ($number =~ s/://g);

    should be

    $number =~ s/://g;
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      That would be it! Thanks amigo!
Re: Substitution all parts of an array...
by greenFox (Vicar) on Mar 14, 2003 at 01:47 UTC
    or the one liner: perl -pi.bak -e 's/://g' myfile.txt

    --
    Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth

Re: Substitution all parts of an array...
by Ineffectual (Scribe) on Mar 14, 2003 at 00:33 UTC
    #!/usr/bin/perl -w use strict; open IN, "myfile.txt" || die "Couldn't open myfile: $!\n"; open OUT, ">myresults.txt" || die "Couldn't open output: $!\n"; my @array; while (<IN>) { chomp; $_ =~ s/://g; print OUT $_ ."\n"; } close IN; close OUT; or while(<IN>) { chomp; push(@array, $_); } close IN; foreach my $num (@array) { $num =~ s/://g; print $num ."\n"; }
    HTH
    'Fect
      while (<IN>) { chomp; $_ =~ s/://g; print OUT $_ ."\n"; }

      you can get rid of the chomp; and then you don't need to append "\n" when printing.

      in the second option, iterating twice on the data (once for reading the file and then another time for the substitution) can get pretty inefficient if the file is big.

      just my 2 cents.