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

hi im trying to parse a text file to manage the numeric valor on the file, whith
my $file = <STDIN>; chomp($file); if ($file=~/[a-zA-Z0-9_]\.dat/){ open(DATOS, "$file") || die "No se puede abrir el archivo, o no existe + en el directorio"; while (<DATOS>){ # print "$_\n" if m/^\d/; my @lines; my @line = @lines if m/^\d/; + + print "@lines";
and split each lines on de array , but i get an hexadecimal valor, any ideas? cheers erick

Replies are listed 'Best First'.
Re: reg exp
by TVSET (Chaplain) on Oct 27, 2003 at 21:19 UTC
    It is a bit difficult to see what you are trying to do. Maybe, if I will tell you what you are doing you will see the error yourself? :) Here we go:
    # Save input from STDIN into $file my $file = <STDIN>; # If $file matches one letter or number, then underscore # and then .dat, then do the block. By the way, you are # missing the closing bracket for the block. :) if ($file=~/[a-zA-Z0-9_]\.dat/){ # Open file or die with some error. open(DATOS, "$file") || die "No se puede abrir el archivo, o no existe + en el directorio"; # Read file line by line while (<DATOS>){ # Initialize array @lines my @lines; # Initialize array @line and make it equal to an # empty array @lines, if the line from file starts with # the digit. my @line = @lines if m/^\d/; # Print empty array @lines print "@lines";
    Anyway, after I went through your code and annotated it (grin), I think I know what you are trying to do. ;) If you want to read the file and print out only those lines which start with digit (like you did in your commented out print statement), then you are looking for something like:
    # Initialize array @lines my @lines; while (<DATOS>) { # Add line from file to array @lines if line starts with # digit push @lines, $_ if m/^\d/; } # Print array @lines after all processing has been done. print @lines;
Re: reg exp
by hardburn (Abbot) on Oct 27, 2003 at 21:46 UTC
    if ($file=~/[a-zA-Z0-9_]\.dat/){

    The [a-zA-Z0-9_] can be shortened to \w. Guessing from your error message and some of your variable names, English isn't your first language, and I bet your system will allow characters in filenames that aren't on an English keyboard, in which case the regex you have above won't match all valid filenames (in fact, it won't even on an English-native system, since almost everyone these days allows spaces in filenames). \w will probably respect your locale, though I haven't checked it.

    Actually, you can get around all of this by using -e instead of that regex, which will check for the existance of a file. So that if statement becomes:

    if( -e $file ) {

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    :(){ :|:&};:

    Note: All code is untested, unless otherwise stated

Re: reg exp
by ChrisS (Monk) on Oct 27, 2003 at 21:09 UTC
    The following code will split each line that begins with one or more digits, assuming the elements are separated by spaces.
    my $file = <STDIN>; chomp($file); if ($file =~ /\w+\.dat/) { open(DATOS, "$file") or die "error message"; while (<DATOS>) { push(@lines, $_) if /^\d/; } close DATOS; foreach my $line (@lines) { my @fields = split(/\s+/, $line); print "@fields"; # or whatever else you want to do } }
Re: reg exp
by sauoq (Abbot) on Oct 27, 2003 at 21:08 UTC

    Your @lines and @line arrays will always be empty with that code. I don't understand where you think you are getting a hexadecimal value from.

    It may be easier if you ask your question in your native language. It is likely that someone here will be able to answer you or at least translate your question.

    -sauoq
    "My two cents aren't worth a dime.";