Help for newbie

N

nicolas-laurent

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.
 
J

Jim Keenan

nicolas-laurent said:
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
 
T

Tad McClellan

nicolas-laurent said:
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;
 
A

axel

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
 
S

Sverre Furberg

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
 
J

John W. Krahn

Sverre said:
Tad McClellan skrev:


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
 
T

Tad McClellan

Sverre Furberg said:
Tad McClellan skrev:

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


No.

my @words = /\w+/g;

would work the same.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top