Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple but annoying problem

Status
Not open for further replies.

tujchl

Technical User
Sep 7, 2008
7
CN
Hi everyone:

my trouble is like this:
for example:
some words like below in my text file:

NM * * NM *
.......

and I want to caculate the sum of NM and *, and find the position of last right NM(both NM and * were taken into count), I write code like this:
$read="NM * * NM * ";
while($read=~/(NM)|(\s\*\s)/g){
$target=$1;
}
but () can only capture NM not *, please tell me how to modify it.

PS: I come from china, and my English is not very good, but I practise it, if you consider some sentence which I write is unsuitable, could you please help me and write down the correct expression?

thanks :)
 
To find the position of the most right 'NM' you can use the function rindex(), e.g.:
Code:
  my $last_NM_column = rindex($line,'NM') + 1;
  if ($last_NM_column > 0) {
    print ".... the most right 'NM' is in Column $last_NM_column\n";
  }
  else{
    print ".... 'NM' not in line !\n";
  }

counting words in Perl is simple, e.g.:
Code:
# counting words in Text file.
# Usage:
# perl wordcount.pl myfile.txt
use strict;

my %word_count=(); #hash of counts

while ( my $line = <> ) {
  chomp($line);
  #print " $line\n";
  # split line into words (delimited by /\s+/
  my @line_list=split(/\s+/,$line);

  # process each line
  foreach my $word (@line_list){
    #print "'$word'\n";
    if ($word_count{$word}){
      $word_count{$word} += 1;
    }
    else {
      $word_count{$word} = 1;	    
    } 
  }
}

# print statistics
foreach my $word (keys(%word_count)){
  print "Count of '$word' = $word_count{$word}\n";
}
 
I tried the sycoogtit's idea:
Code:
# counting ' NM ' and ' * ' in Text file.
# Usage:
# perl NM_count.pl myfile.txt
use strict;

my $NM_count = 0;
my $asterisk_count = 0;
while ( my $line = <> ) {
  chomp($line);
  #print " $line\n";
  # split line into words (delimited by /\s+/
  my @line_list=split(/\s+/,$line);

  # process each line
  while($line=~/(NM|\s\*\s)/g){
    my $word = $1;
    #print "'$word'\n";
    if ($word eq 'NM') {
      $NM_count += 1;
    }
    elsif ($word eq ' * ')
    {
      $asterisk_count += 1;	    
    }
  }
}

# print statistics
print "Count of 'NM'   = $NM_count\n";
print "Count of ' * '  = $asterisk_count\n";
 
There is no need for the if/else block. All you need is:

Code:
  foreach my $word (@line_list){
      $word_count{$word}++;        
  }





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Good remark KevinADC, very interesting .. I tried it and it's true :)

But I don't understand this Perl's behaviour:
If I try
Code:
my $myvar='xaz';
print "\$myvar = $myvar\n";
$myvar += 1;
print "\$myvar = $myvar\n";
I get this
Code:
$myvar = xaz
$myvar = 1

It's OK, but if I use ++ instead of += 1, i.e I modify the above code so:
Code:
my $myvar='xaz';
print "\$myvar = $myvar\n";
$myvar ++;
print "\$myvar = $myvar\n";

I would expect the same result, but what a surprise .. I get this
Code:
$myvar = xaz
$myvar = xba

Could you explain why it is so???
 
special thanks to everyone :), resolved!!!!

Hi everyone:

my trouble is like this:
for example:
some words like below in my text file:

NM * * NM *
.......

and I want to caculate the sum of NM and *, and find the position of last right NM(both NM and * were taken into count), I write code like this:
$read="NM * * NM * ";
while($read=~/(NM)|(\s\*\s)/g){
$target=$1;
}
but () ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top