in reply to Re: To Separate alphanumeric
in thread To Separate alphanumeric

I just tried out using your guidelines.., it is as follow.. <CODE> #!/usr/bin/perl -w use warnings; use strict; open(FH,"<input") or die "cannot open"; open(FI,">alpha") or die "cannot open"; open(FL,">numeric") or die "cannot open"; $i=0; $a=0; while(<FH>) { @a=split //; for $a(@a) { print $b$i=$a; } if($b$i eq /A-Za-z/) { print FI $bi; } elsif($b$i eq /0-9/) { print FL $bi; } else { } } close FH; close FI; close FL; But it didn't work can you just look into whats the error

Replies are listed 'Best First'.
Re^3: To Separate alphanumeric
by Utilitarian (Vicar) on Oct 08, 2010 at 08:52 UTC
    Hi sowmya,

    You need to close your code tags, this should have looked mangled in the preview.
    You must have received errors when you tried to run this program, you should learn to read them ;) You could use diagnostics if the error messages seem cryptic
    I've modified your program to work and improved the coding style, however you need to work through errors to learn, so these are the changes required.

    General issues
    Use the three argument form of open, it prevents errors
    You should also include $! (check it out in perlvar) in your die message as it's good to know what went wrong
    Indirect filehandles are better practice and will allow you to pass them around your program when you're writing programs with sub-routines
    eg. open(my $input_file, '<', 'input') or die "cannot open input: $!";
    You must declare your variables when you use strict (see my)

    Logic issues
    What is the point of @b, you should process the elements of @a as you go
    You process the loop without doing anything and then examine only the first character of the array @b
    The way to make a regex test is =~ , eqis for string equality
    eg. if($a =~ /[A-Za-z]/) {
    You may want to print a place holder to mark the fact that a character was printed to the other file.
    To get the line of a file you are processing, take a look at $. in perlvar you could use this to generate a useful error message in your else block

    Try to make the changes above and next time close your code tags, look at the preview and copy in the errors you are seeing when describing your problem

    Happy learning, Utilitarian

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."