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

Hi and sorry for the basic question but I am very to programming and perl, but trying to learn ;)

I need to extract a number from a text file (only 1 value at any time) and parse it into a perl script as a variable. I was thinking about grep but am not sure its the best way. Any ideas.

Thanks.

2003-05-20 edit ybiC: retitle from "newbie question", format with paragraphs as seen from source

Replies are listed 'Best First'.
Re: Parse text file for number
by Tanalis (Curate) on May 20, 2003 at 08:11 UTC
    Can you provide a (small) example of the text file you need to read in? Assuming it's one-value-per-line, it should just be a case of doing something like ...
    my $number; open FILE, "<", "/path/to/your/file" or die; while ($number = <FILE>) { # read in a line of the file chomp $number; # remove the newline # do something with each number } close FILE;
    If it's more complicated than that (multiple numbers per line), then you can use a split on each line you read from the file to extract numbers one at a time.

    Hope that helps ..
    -- Foxcub
    #include www.liquidfusion.org.uk

      Thanks. Thats exactly what I was after. Thanks to all the replies.
Re: Parse text file for number
by Skeeve (Parson) on May 20, 2003 at 08:11 UTC
    Is this what you need?
    use strict; my $number= undef; while (<>) { next unless /(\d+)/; $number= $1; } die "No number" unless defined $number;
Re: Parse text file for number
by arthas (Hermit) on May 20, 2003 at 08:32 UTC
    What you exactly need to do isn't so clear. ;-) Could you post an example of the file you need to parse?

    Thanks, Michele.