Sorting words - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sorting words

EsmebabeEsmebabe Posts: 7Member
The following code is not giving me any errors,but it does not produce anything in the ouyput file.Whats wrong here?

#!/usr/local/bin/perl -w

my $words="C:/WINNT/Profiles/Administrator/Desktop/1.txt";

my $out="c:/output.txt";

# open prompt file
open my $fh, "< $words" || die ("Unable to open prompt file $prompt");

open my $wfh, "> $out" || die ("Can't create output file!");

# Read every word into an array
my @words = <$fh>;
close $fh;

my %temp = ();
@word = sort @word;

print $wfh "$word";


close $fh;
close $wfh;






Comments

  • JonathanJonathan Posts: 2,914Member
    : The following code is not giving me any errors,but it does not
    : produce anything in the ouyput file.Whats wrong here?
    :
    A few things...

    : #!/usr/local/bin/perl -w
    :
    : my $words="C:/WINNT/Profiles/Administrator/Desktop/1.txt";
    :
    : my $out="c:/output.txt";
    :
    : # open prompt file
    : open my $fh, "< $words" || die ("Unable to open prompt file $prompt");
    :
    : open my $wfh, "> $out" || die ("Can't create output file!");
    :
    : # Read every word into an array
    : my @words = <$fh>;
    : close $fh;
    :
    : my %temp = ();
    : @word = sort @word;
    :
    You called the array @words a minute ago. You gotta be consistent...
    @words = sort @words;

    : print $wfh "$word";
    :
    Where'd $word come from? @word and $word are not related. Yes, I know you write $word[1] to reference an element of @word. The sigil represents the context rather than the type of the container, though. You most probably need to do a loop like:-

    print $wfh $_ foreach (@words);

    :
    : close $fh;
    : close $wfh;
    :

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • EsmebabeEsmebabe Posts: 7Member
    Where does this error come from?

    C:Scripts>sorted.pl
    Name "main::prompt" used only once: possible typo at C:Scriptssorted.pl line 8.
    readline() on closed filehandle $fh at C:Scriptssorted.pl line 13.


    #!/usr/local/bin/perl -w

    my $words="C:/WINNT/Profiles/Administrator/Desktop/1.txt";

    my $out="c:/output.txt";

    # open prompt file
    open my $fh, "< $words" || die ("Unable to open prompt file $prompt");

    open my $wfh, "> $out" || die ("Can't create output file!");

    # Read every word into an array
    my @words = <$fh>;
    close $fh;

    my %temp = ();

    @words = sort @words;

    print $wfh "$_" foreach (@words);
    close $wfh;



  • WeirdofreakWeirdofreak Posts: 439Member
    : Where does this error come from?
    :
    : C:Scripts>sorted.pl
    : Name "main::prompt" used only once: possible typo at C:Scriptssorted.pl line 8.
    : readline() on closed filehandle $fh at C:Scriptssorted.pl line 13.
    :
    :
    : #!/usr/local/bin/perl -w
    :
    : my $words="C:/WINNT/Profiles/Administrator/Desktop/1.txt";
    :
    : my $out="c:/output.txt";
    :
    : # open prompt file
    : open my $fh, "< $words" || die ("Unable to open prompt file $prompt");

    You want $words, $prompt is undefined. Haven't a clue why you weren't getting this error before though.

    : open my $wfh, "> $out" || die ("Can't create output file!");
    :
    : # Read every word into an array
    : my @words = <$fh>;
    : close $fh;

    That last line is where an error occurs, but I can't see a problem with it. Sometimes errors get generated from others, so maybe fixing the above will help, but it's doubtful.

    :
    : my %temp = ();

    I don't actually see the point in this line. I'm probably missing something vital, though.

    :
    : @words = sort @words;
    :
    : print $wfh "$_" foreach (@words);
    : close $wfh;

    Couldn't you just do print $wfh "@words"? Print deals with arrays, so it should work fine.

    A few other things to note:
    That'll only sort line-by-line. You'll have to change $/ (the input record separatoer) to a space to read it word-for-word, or join @words with a space and split the resulting string at spaces, which would probably be more effective.
    If you want to display the words on a new line each, you'll have to change $" or $, to the newline character.
    You ought to use strict at the beginning of every script - it's good practice and can help to pick up bad coding.
  • JonathanJonathan Posts: 2,914Member
    : : Where does this error come from?
    : :
    : : C:Scripts>sorted.pl
    : : Name "main::prompt" used only once: possible typo at C:Scriptssorted.pl line 8.
    : : readline() on closed filehandle $fh at C:Scriptssorted.pl line 13.
    : :
    : :
    : : #!/usr/local/bin/perl -w
    : :
    : : my $words="C:/WINNT/Profiles/Administrator/Desktop/1.txt";
    : :
    : : my $out="c:/output.txt";
    : :
    : : # open prompt file
    : : open my $fh, "< $words" || die ("Unable to open prompt file $prompt");
    :
    : You want $words, $prompt is undefined. Haven't a clue why you weren't getting this error before though.
    :
    : : open my $wfh, "> $out" || die ("Can't create output file!");
    : :
    : : # Read every word into an array
    : : my @words = <$fh>;
    : : close $fh;
    :
    : That last line is where an error occurs, but I can't see a problem with it. Sometimes errors get generated from others, so maybe fixing the above will help, but it's doubtful.
    :
    Actually I thing it occurs on the line with the diamond operator. I can't think of anything aside from the fact that a warning was thrown on the open line as being the reason for this one though - like you say, everything else looks clean.

    : : my %temp = ();
    :
    : I don't actually see the point in this line. I'm probably missing
    : something vital, though.
    Me either, it can probably go if there isn't more to the script that is posted here.

    : : @words = sort @words;
    : :
    : : print $wfh "$_" foreach (@words);
    : : close $wfh;
    :
    : Couldn't you just do print $wfh "@words"? Print deals with arrays,
    : so it should work fine.
    :
    Possibly, but see below...

    : A few other things to note:
    : That'll only sort line-by-line. You'll have to change $/ (the input
    : record separatoer) to a space to read it word-for-word, or join
    : @words with a space and split the resulting string at spaces, which
    : would probably be more effective.
    : If you want to display the words on a new line each, you'll have to
    : change $" or $, to the newline character.
    Actually, if you want to just sort a file line by line, what is there is fine. And remember that the diamond operator doesn't strip off
    's. So as long as there is one for the last line in the file, you don't need to worry about removing them and putting them back in place. The reason I didn't use print with an array is because I'm under the impression it'd stick the output seperator in, which in this case isn't appropriate. Of course, you could nullify or undef the output seperator, though for some reason doing things like that never take my fancy...

    : You ought to use strict at the beginning of every script - it's good
    : practice and can help to pick up bad coding.
    :
    I tend to use strict myself, but not warnings. Some would say I ought use warnings too, but there you go... :-) Strict does catch the obvious mistakes - but you do have to be well behaved.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.