Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

To Separate alphanumeric

by Anonymous Monk
on Oct 07, 2010 at 04:37 UTC ( [id://863901]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: To Separate alphanumeric
by Utilitarian (Vicar) on Oct 07, 2010 at 09:13 UTC
    As mentioned above, this is not a homework outsourcing service. show us what you've tried and we will point you in the right direction if we can

    You need to write a program that will carry out the following steps.

    • Get a file name from the command line, look at @ARGV in perlvar
    • open 3 files, one to read from and two to write to.
    • Read from the file supplied while there are lines in the file (perlsyn Loop control, while).
    • split each line into individual characters
    • For each character, if the character is alphabetic write it to the alpha file, if numeric to the numeric file, otherwise display an error (possibly with line and position of character). A simple regular expression perlrewould determine which action should be taken.
    Have a look at the documentation linked above, try and code up the algorithm outlined and come back to us with specific Perl problems rather than a request to solve the entire problem.

    Part of learning to code is learning a language and part of it is splitting up problems into sub-problems until each sub-problem can be written as a statement in the language you are using.

    This is a discussion and education forum/community, mailing you a solution adds nothing to the community and offers an insufficient challenge to be worth doing for the satisfaction of solving by itself (perhaps try and get your end of term project written for you in under 160 chars and see if people will mail responses to you)

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      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
        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."
Re: To Separate alphanumeric
by AnomalousMonk (Archbishop) on Oct 07, 2010 at 09:50 UTC

    The humble monks really like to see supplicants at the monastery offer up some effort, some work.

    Try this:

    1. Write a program that will open (see also perlopentut) a file for input, and open another file for output. Check the status of each open to be sure it succeeds. Read each line from the input file and write it to the output file.
    2. When Step 1 works, alter the program to open a file for input, and two other files for output. Read each line from the input file and output the line to both output files.
    3. When Step 2 works, alter the program to write alternating lines to the two output files, e.g., even lines to one file, odd lines to the other.
    4. When Step 3 works, you have a basic framework to read a line from a file and decide to which of two other files it should be written. Now you can think about splitting the line into individual characters after it is read (say, maybe with split), deciding if each character is alpha or numeric (perhaps with a regex; see perlrequick, perlreref, perlre), and writing each character to the appropriate file.

    Get as far as you can on your own, then come back with what you have and where you are stuck and post it here (i.e., in this thread; please do not start another thread) and you may get more forthcoming replies – in fact, like that of Utilitarian, who rather beat me to the punch.

Re: To Separate alphanumeric
by Anonymous Monk on Oct 07, 2010 at 06:38 UTC
    I'm sorry, but there is no homework.perl.org
Re: To Separate alphanumeric
by JavaFan (Canon) on Oct 07, 2010 at 09:13 UTC
    #!/usr/bin/sh sed -e 's/[^0-9]//g' input | tr -d '\n' > out.numeric sed -e 's/[^a-zA-Z]//g' input | tr -d '\n' > out.alphabetic

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found