Longest Word

How would I find and show the longest word in a textfile. I'm having trouble reading the individual words in the arrray that holds the textfile. Would you use the substring function or use for loops to execute this problem? I tried using the substring method, but once the substring function executes, it reads the next line in the text file.

For example:

@array = ;
(The file contains:)
I went to the store today.
The sweater was very cheap.

in a for loop
substr(@array[$i], @array[$i] +1, 5);

Output:

I wen
The s

Comments

  • : How would I find and show the longest word in a textfile. I'm having trouble reading the individual words in the arrray that holds the textfile. Would you use the substring function or use for loops to execute this problem? I tried using the substring method, but once the substring function executes, it reads the next line in the text file.
    :
    : For example:
    :
    : @array = ;
    : (The file contains:)
    : I went to the store today.
    : The sweater was very cheap.
    :
    : in a for loop
    : substr(@array[$i], @array[$i] +1, 5);
    :
    : Output:
    :
    : I wen
    : The s
    Try this:-

    [code]my $input = join('', );
    my $longestword = '';
    my $longestwordlength = 0;
    foreach my $word (split(/s+/, $input) {
    if (length($word) > $longestwordlength) {
    $longestword = $word;
    $longestwordlength = length($word);
    }
    }[/code]

    When we read a file in using the diamond operator, it's split on newlines. But we want it split on word boundaries, so we join all the lines of input, then split it on whitespace (s+). Then it's pretty simple inside the loop.

    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.");

  • : : How would I find and show the longest word in a textfile. I'm having trouble reading the individual words in the arrray that holds the textfile. Would you use the substring function or use for loops to execute this problem? I tried using the substring method, but once the substring function executes, it reads the next line in the text file.
    : :
    : : For example:
    : :
    : : @array = ;
    : : (The file contains:)
    : : I went to the store today.
    : : The sweater was very cheap.
    : :
    : : in a for loop
    : : substr(@array[$i], @array[$i] +1, 5);
    : :
    : : Output:
    : :
    : : I wen
    : : The s
    : Try this:-
    :
    : [code]my $input = join('', );
    : my $longestword = '';
    : my $longestwordlength = 0;
    : foreach my $word (split(/s+/, $input) {
    : if (length($word) > $longestwordlength) {
    : $longestword = $word;
    : $longestwordlength = length($word);
    : }
    : }[/code]
    :
    : When we read a file in using the diamond operator, it's split on newlines. But we want it split on word boundaries, so we join all the lines of input, then split it on whitespace (s+). Then it's pretty simple inside the loop.
    :
    : 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.");
    :
    :
    I tried running your example, and it doesn't run. It states that there is a $ missing in the foreach statement. How would you fix this?
  • : : [code]my $input = join('', );
    : : my $longestword = '';
    : : my $longestwordlength = 0;
    : : foreach my $word (split(/s+/, $input)[red])[/red] {
    : : if (length($word) > $longestwordlength) {
    : : $longestword = $word;
    : : $longestwordlength = length($word);
    : : }
    : : }[/code]
    :
    : I tried running your example, and it doesn't run. It states that
    : there is a $ missing in the foreach statement. How would you fix
    : this?
    Ooops! I missed a bracket.

    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.");

  • : : : [code]my $input = join('', );
    : : : my $longestword = '';
    : : : my $longestwordlength = 0;
    : : : foreach my $word (split(/s+/, $input)[red])[/red] {
    : : : if (length($word) > $longestwordlength) {
    : : : $longestword = $word;
    : : : $longestwordlength = length($word);
    : : : }
    : : : }[/code]
    : :
    : : I tried running your example, and it doesn't run. It states that
    : : there is a $ missing in the foreach statement. How would you fix
    : : this?
    : Ooops! I missed a bracket.
    :
    : 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.");
    :
    :
    the code you posted still does not work. It still says that there is a $ missing in the foreach statement
  • : : : : [code]my $input = join('', );
    : : : : my $longestword = '';
    : : : : my $longestwordlength = 0;
    : : : : foreach my $word (split(/s+/, $input)[red])[/red] {
    : : : : if (length($word) > $longestwordlength) {
    : : : : $longestword = $word;
    : : : : $longestwordlength = length($word);
    : : : : }
    : : : : }[/code]
    : : :
    : : : I tried running your example, and it doesn't run. It states that
    : : : there is a $ missing in the foreach statement. How would you fix
    : : : this?
    : : Ooops! I missed a bracket.
    : :
    : : 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.");
    : :
    : :
    : the code you posted still does not work. It still says that there is
    : a $ missing in the foreach statement
    What version of Perl are you running? Looks like it's not one of the latest, 'cus what I posted works completely fine here (5.8.0).

    [code]my $input = join('', );
    my $longestword = '';
    my $longestwordlength = 0;
    my $word;
    foreach $word (split(/s+/, $input)) {
    if (length($word) > $longestwordlength) {
    $longestword = $word;
    $longestwordlength = length($word);
    }
    }[/code]

    I moved the variable declaration out of the foreach construct.

    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.

Howdy, Stranger!

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

Categories