Help for newbie

Discussion in 'Perl Misc' started by nicolas-laurent, Mar 24, 2005.

  1. Hi all,


    Am trying to write a Perl script that will read a line from a file and
    number each word.

    house operas ray sun doctor lilo

    1 house
    2 operas
    3 ray
    4 sun
    5 doctor
    6 lilo

    #!/usr/bin/perl
    while (<>) {
    @words = split(/\W*\s+\W*/, $_); # split into words
    foreach $word (@words) {
    $wordnumber{$word}++; # count the words
    }
    }


    print $wordnumber{$word}, $word;
    }


    I have difficulty to figure out how to do it.
     
    nicolas-laurent, Mar 24, 2005
    #1
    1. Advertising

  2. nicolas-laurent

    Jim Keenan Guest

    nicolas-laurent wrote:

    > Hi all,
    >
    >
    > Am trying to write a Perl script that will read a line from a file and
    > number each word.
    >
    > house operas ray sun doctor lilo
    >
    > 1 house
    > 2 operas
    > 3 ray
    > 4 sun
    > 5 doctor
    > 6 lilo
    >
    > #!/usr/bin/perl
    > while (<>) {
    > @words = split(/\W*\s+\W*/, $_); # split into words


    Suggestion: Express in a human language (e.g., English) what the
    regular expression in the line above means.

    You'll probably find that that regex is way too complex for what you want.

    Then: perldoc -f split

    jimk
     
    Jim Keenan, Mar 24, 2005
    #2
    1. Advertising

  3. nicolas-laurent

    Guest

    while (@words=split(' ',<STDIN>)) {
    $cnt = 0;
    print $cnt++."\t$_\n" for (@words);
    }
     
    , Mar 24, 2005
    #3
  4. nicolas-laurent

    Guest

    while (@words=split(' ',<STDIN>)) {
    $cnt = 0;
    print $cnt++."\t$_\n" for (@words);
    }
     
    , Mar 24, 2005
    #4
  5. nicolas-laurent <> wrote:

    > Subject: Help for newbie



    Please put the subject of your article in the Subject of your article.

    Have you seen the Posting Guidelines that are posted here frequently?


    > Am trying to write a Perl script that will read a line from a file and
    > number each word.
    >
    > house operas ray sun doctor lilo
    >
    > 1 house
    > 2 operas
    > 3 ray
    > 4 sun
    > 5 doctor
    > 6 lilo


    --------------------
    while (<>) {
    my @words = split(/\W*\s+\W*/, $_); # split into words
    foreach my $num ( 0 .. $#words) {
    print $num+1, " $words[$num]\n";
    }
    }
    --------------------


    > #!/usr/bin/perl
    > while (<>) {
    > @words = split(/\W*\s+\W*/, $_); # split into words
    > foreach $word (@words) {
    > $wordnumber{$word}++; # count the words
    > }
    > }



    It is insulting to be asked to look at such horridly formatted code.

    You will get more help if you make it easier to help by
    formatting your code sensibly. Indent your code blocks.


    > I have difficulty to figure out how to do it.



    Use one of Randal's rules:

    use split() if you want to say what to throw out.

    use m//g in list context if you want to say what to keep.

    I'd go with the 2nd one here:

    my @words = /(\w+)/g;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 24, 2005
    #5
  6. nicolas-laurent

    Guest

    wrote:
    > while (@words=split(' ',<STDIN>)) {
    > $cnt = 0;
    > print $cnt++."\t$_\n" for (@words);
    > }


    Minor point... the example the OP gave started the the first word
    at 1, so counter should be incremented before it is used in the
    print statement:

    my $cnt = 0; # See note below
    while (my @words = split(/\s+/, <>)) {
    print ++$cnt, "\t$_\n" for @words;
    }

    Obviously move the initialisation of $cnt within the loop if the
    numbering should start afresh for each line.

    Axel
     
    , Mar 24, 2005
    #6
  7. Tad McClellan skrev:
    > Use one of Randal's rules:
    >
    > use split() if you want to say what to throw out.
    >
    > use m//g in list context if you want to say what to keep.
    >
    > I'd go with the 2nd one here:
    >
    > my @words = /(\w+)/g;


    Another newbie question:
    Has the parentheses around '\w+' a special meaning in this particular case?
    Sverre
     
    Sverre Furberg, Mar 26, 2005
    #7
  8. Sverre Furberg wrote:
    > Tad McClellan skrev:
    >
    >> Use one of Randal's rules:
    >>
    >> use split() if you want to say what to throw out.
    >>
    >> use m//g in list context if you want to say what to keep.
    >>
    >> I'd go with the 2nd one here:
    >>
    >> my @words = /(\w+)/g;

    >
    > Another newbie question:
    > Has the parentheses around '\w+' a special meaning in this particular case?


    Not in this particular case:

    $ perl -le'$_ = q/abc&^%def ghi/; my @words = /(\w+)/g; print for @words'
    abc
    def
    ghi
    $ perl -le'$_ = q/abc&^%def ghi/; my @words = /\w+/g; print for @words'
    abc
    def
    ghi



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 26, 2005
    #8
  9. Sverre Furberg <> wrote:
    > Tad McClellan skrev:


    >> my @words = /(\w+)/g;

    >
    > Another newbie question:
    > Has the parentheses around '\w+' a special meaning in this particular case?



    No.

    my @words = /\w+/g;

    would work the same.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 26, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Bond
    Replies:
    0
    Views:
    561
    James Bond
    Aug 3, 2004
  2. Id0x
    Replies:
    4
    Views:
    1,222
    Erik Max Francis
    Jul 21, 2003
  3. JohnE

    newbie with newbie questions

    JohnE, Aug 17, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    533
    Gregory A. Beamer
    Aug 17, 2009
  4. LeTubs
    Replies:
    6
    Views:
    137
    Tad McClellan
    Jan 25, 2004
  5. Replies:
    56
    Views:
    771
    alex23
    Dec 27, 2012
Loading...

Share This Page