Perl

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • print file

    5 answers - 375 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi
    open FILE, "file.txt" or die $!;
    while (<f:/file.txt>)
    Sombody please tell me why do i get the message as below if i want to print
    the contents of text file
    Name "main::FILE" used only once: possible typo at nl.plx line3.
    1:f:/file.txt
    and my file is not executed (i did not get the output)
    regards
    Badrinath
  • No.1 | | 696 bytes | |

    badrinath chitrala wrote:
    Hi

    Hello,

    open FILE, "file.txt" or die $!;

    open() creates the filehandle FILE which is associated with the contents of
    the file "file.txt".

    while (<f:/file.txt>)

    You are using a file glob to get a list of file names which is the same as doing:

    while ( defined( $_ = glob 'f:/file.txt' ) )

    You want to use the newly created filehandle instead:

    while ( <FILE)

    Which is the same as:

    while ( defined( $_ = <FILE) )

    Which reads from the contents of the file.

    perldoc -f open
    perldoc perlopentut
    perldoc -f readline
    perldoc perlop (I/ section)

    John
  • No.2 | | 774 bytes | |

    Mon, May 08, 2006 at 08:07:56AM -0700, Xuning Wang wrote:

    Hi:

    I have a question about replacing $ sign when it is
    before a digit. For example:

    $str = "$1.12";

    I want to extact the dollar amount 1.12 from $str.

    If I do this:
    $str =~ s/\$//;

    $str then become .12. The "$1" in "$1.12" is
    interpreted first before substitution is applied. How
    do I prevent this? I can't use this:
    $str ='$1.12' because the $1.12 is read from a file.

    Does anyone have a suggestion?

    Something else is going on, but I can't say exactly where.

    $ echo '$1.12' | perl -ple 's/\$//'
    1.12

    Perhaps you have some other interpolation going on where you don't need
    it?
  • No.3 | | 1082 bytes | |

    Mon, May 08, 2006 at 08:35:52AM -0700, Xuning Wang wrote:

    Thanks for reply. The question is easily solvable if
    it is in the same file as the perl script. You can use
    single quotes or backslash to modify the string.

    But as I stated in the message, the "$1.12" was in a
    text file along with other things, say you have a
    sentence like this in a file: Apple $1.12. You can't
    modify the file itself. So it's hard to utilize "\" or
    use single quotes because you have to first read the
    sentence from a file.

    And my example, which you will find below since you top posted and I
    can't be bothered to rearrange things, shows the equivalent of reading
    from a file with nary a backslash in sight, save the one you used in the
    substitution.

    Paul Johnson <paul (AT) pjcj (DOT) netwrote:

    Something else is going on, but I can't say exactly where.

    $ echo '$1.12' | perl -ple 's/\$//'
    1.12

    Perhaps you have some other interpolation going on where you don't
    need it?
  • No.4 | | 1411 bytes | |

    5/8/06, Xuning Wang <jasonxw (AT) yahoo (DOT) comwrote:

    Subject: Unsolvable? How to extract

    "Unsolvable?" There are many unsolvable problems in the world. Few of
    them deserve to be posted to a beginners' discussion group.

    I have a question about replacing $ sign when it is
    before a digit.

    It's done the same way as replacing a $ sign that isn't before a digit.

    For example:

    $str = "$1.12";

    If you mean a real dollar sign, you have to backslash it. As you've
    written that, it uses Perl's $1 variable. I assume you mean that $str
    is the five-character string normally written in Perl as '$1.12'.

    I want to extact the dollar amount 1.12 from $str.

    You don't have to modify $str in order to do that.

    my($dollar_amount) = ($str =~ /\$(\d+\.\d\d)/); # maybe?

    If I do this:
    $str =~ s/\$//;

    $str then become .12.

    Not if it had been '$1.12' before the substitution.

    I can't use this:
    $str ='$1.12' because the $1.12 is read from a file.

    You seem to be under the impression that dollar signs in a data file
    will be meaningful to Perl. No; data is data. Unless you're doing
    something silly like using eval on a string, of course. Don't do that.

    Hope this helps!

    Phoenix
    Stonehenge Perl Training
  • No.5 | | 345 bytes | |

    Xuning Wang schreef:

    $str = "$1.12";

    $str = '$1.12';

    or

    $str = q($1.12);

    #!/usr/bin/perl
    use strict;
    use warnings;

    while (<DATA>)
    {
    print "$_\n" for /\$(\d+\.\d+)/g
    }

    __DATA__
    Buy when the price goes from $1.12 to $0.98.
    $12.345
    $6
    $7.8
    $9.01

Re: print file


max 4000 letters.
Your nickname that display:
In order to stop the spam: 9 + 9 =
QUESTION ON "Perl"

EMSDN.COM