Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Help with Regular Expressions and Perl

by vc859 (Initiate)
on Apr 01, 2010 at 04:00 UTC ( [id://832195]=perlquestion: print w/replies, xml ) Need Help??

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

Does anyone know what the error with this code is? The output is a blank file. Also, if anyone knows how to combine those regexs that would be wonderful too.
open(INFILE, "<perlfile.txt"); $variable = join(@variable = <INFILE>, "\n"); 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 OUTFILE "$variable"; close(OUTFILE);

Replies are listed 'Best First'.
Re: Help with Regular Expressions and Perl
by NetWallah (Canon) on Apr 01, 2010 at 04:56 UTC
    Here you go --
    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

      There are hundreds of characters on which your program will fail. Please do realize that /\d/ is not a short cut for /[0-9]/. In fact, /\d/ is a short cut (in 5.10.1, 5.12 is likely to have more characters) for:

      /[0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૫૬૭૮૯୦୧୨୩୪୫୬୭୮୯௦௧௨௩௪௫௬௭௮௯౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩၀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙᥆᥇᥈᥉᥊᥋᥌᥍᥎᥏᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙᭐᭑᭒᭓᭔᭕᭖᭗᭘᭙᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩꣐꣑꣒꣓꣔꣕꣖꣗꣘꣙꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉꩐꩑꩒꩓꩔꩕꩖꩗꩘꩙0123456789𐒠𐒡𐒢𐒣𐒤𐒥𐒦𐒧𐒨𐒩𝟎𝟏𝟐𝟑𝟒𝟓𝟔𝟕𝟖𝟗𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡𝟢𝟣𝟤𝟥𝟦𝟧𝟨𝟩𝟪𝟫𝟬𝟭𝟮𝟯𝟰𝟱𝟲𝟳𝟴𝟵𝟶𝟷𝟸𝟹𝟺𝟻𝟼𝟽𝟾𝟿]/

      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.
Re: Help with Regular Expressions and Perl
by Punitha (Priest) on Apr 01, 2010 at 04:20 UTC

    Hi vc859,

    The syntax for 'join' is:

    join EXPR,LIST

    So your code should be

    $variable = join("\n",@variable = <INFILE>);

    Punitha

      Just a small note: Using slurp mode is usually far more efficient than join()ing a temporary array or list.

      $variable=do { local $/=undef; <INFILE> };

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      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.
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
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);
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.

      Hi,

      You are processing a single file (reading and writing in the same file), so please check the input file content in every execution (may be you are reading the blank file) or try link this,

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found