Re: Help with Regular Expressions and Perl
by NetWallah (Canon) on Apr 01, 2010 at 04:56 UTC
|
use strict;
use warnings;
my $variable = join("", <DATA>);
my @num = qw|zero one two three four five six seven eight nine|;
$variable =~ s/(\d)/$num[$1]/g;
print "$variable";
__DATA__
This is 1 great time 2 have a 3 way 4some.
When 5 and 6 together make 11.
Some 8 or 9 of us will see that.
Output:
This is one great time two have a three way foursome.
When five and six together make oneone.
Some eight or nine of us will see that.
Update:Thanks JavaFan(++) for the heads-up on \d - My education continues.
cdarke (See his note below) - yes - I used join because the O.P did. As you point out, it is completely unnecessary.
Syntactic sugar causes cancer of the semicolon. --Alan Perlis
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Not quite sure why you are bothering with the join, maybe because the OP tried to use it:
use warnings;
use strict;
my @num = qw(zero one two three four five six seven eight nine);
while (<DATA>) {
s/([0-9])/$num[$1]/g;
print
}
__DATA__
This is 1 great time 2 have a 3 way 4some.
When 5 and 6 together make 11.
Some 8 or 9 of us will see that.
| [reply] [Watch: Dir/Any] [d/l] |
Re: Help with Regular Expressions and Perl
by Punitha (Priest) on Apr 01, 2010 at 04:20 UTC
|
$variable = join("\n",@variable = <INFILE>);
Punitha | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Although the questioner probably does not want to be using join here at all - reading the file via <INFILE> does not remove the file's newlines, so there is probably no need to be adding more.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Help with Regular Expressions and Perl
by toolic (Bishop) on Apr 01, 2010 at 12:49 UTC
|
Unrelated to your problem, but if you find yourself doing lots of digit to word conversions, Lingua::EN::Numbers is handy:
use Lingua::EN::Numbers qw(num2en);
print num2en(4);
__END__
four
| [reply] [Watch: Dir/Any] [d/l] |
Re: Help with Regular Expressions and Perl
by kiruthika.bkite (Scribe) on Apr 01, 2010 at 07:18 UTC
|
The syntax of join is,
join EXPR,list
I have just modified your code into the following.
use strict;
use warnings;
my @variable;
my $variable;
open(INFILE, "<perlfile.txt");
@variable=<INFILE>;
$variable =join(" ",@variable);
print "$variable";
close(INFILE);
open(OUTFILE, ">perlfile.txt");
$variable =~ s/0/zero/g ;
$variable =~ s/1/one/g ;
$variable =~ s/2/two/g ;
$variable =~ s/3/three/g ;
$variable =~ s/4/four/g ;
$variable =~ s/5/five/g ;
$variable =~ s/6/six/g ;
$variable =~ s/7/seven/g ;
$variable =~ s/8/eight/g ;
$variable =~ s/9/nine/g ;
print "$variable";
print OUTFILE "$variable","\n";
close(OUTFILE);
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Help with Regular Expressions and Perl
by vc859 (Initiate) on Apr 01, 2010 at 04:27 UTC
|
Thanks. I made that correction, and tried it both with and without the \n, but I am still getting the blank file as an output. | [reply] [Watch: Dir/Any] |
|
use strict;
use warnings;
open(INFILE, "<perlfile.txt");
my @variable;
my $variable = join("\n", @variable = <INFILE>);
close(INFILE);
open(OUTFILE, ">perlfile_out.txt");
$variable =~ s/0/zero/g ;
$variable =~ s/1/one/g ;
$variable =~ s/2/two/g ;
$variable =~ s/3/three/g ;
$variable =~ s/4/four/g ;
$variable =~ s/5/five/g ;
$variable =~ s/6/six/g ;
$variable =~ s/7/seven/g ;
$variable =~ s/8/eight/g ;
$variable =~ s/9/nine/g ;
print OUTFILE "$variable";
close(OUTFILE);
Punitha | [reply] [Watch: Dir/Any] [d/l] |