Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Character Count
Hello,
I am writing a simple perl script to count the # of characters in a given
field.  At the command line I can write >>wc -m somefile.txt, and get the
total character count.  But in the script I am splitting a text file and
then doing a count on an element of the array.

code snippet:
**************************
while (<FILE> )
{
chomp;
(@fields) = split(/\t/);
$count = wc -m $fields[12];
print "$count\n";
}
**************************

Here is the error I receive when I check the syntax.
>perl -c count.pl
Unrecognized file test: -m at count.pl line 20.


I've tried many different ways of writing this, but can't seem to get it
working.  Any ideas/help?
Thanks,
Rob

 ________________________________________
_________________________
Check out MSN PC Safety & Security to help ensure your PC is protected and
safe. http://specials.msn.com/msn/security.asp


Report this thread to moderator Post Follow-up to this message
Old Post
Rob Torres
03-27-04 04:14 AM


Re: Character Count
On Mar 23, 2004, at 3:18 PM, Rob Torres wrote:

> Hello,
> I am writing a simple perl script to count the # of characters in a
> given
> field.  At the command line I can write >>wc -m somefile.txt, and get
> the
> total character count.  But in the script I am splitting a text file
> and
> then doing a count on an element of the array.
>
> code snippet:
> **************************
> while (<FILE> )
> {
>    chomp;
>    (@fields) = split(/\t/);
>    $count = wc -m $fields[12];

In Perl, we spell that:

$count = length $fields[12];

Hope that helps.  ;)

James

P.S.  Looks like you might not be using strict and probably not
warnings either.  I definitely don't recommend continuing on that
way...

>    print "$count\n";
> }
> **************************
>
> Here is the error I receive when I check the syntax. 
> Unrecognized file test: -m at count.pl line 20.
>
>
> I've tried many different ways of writing this, but can't seem to get
> it
> working.  Any ideas/help?
> Thanks,
> Rob
>
>  ________________________________________
_________________________
> Check out MSN PC Safety & Security to help ensure your PC is protected
> and safe. http://specials.msn.com/msn/security.asp
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
James Edward Gray II
03-27-04 04:14 AM


Re: Character Count
On Tuesday 23 March 2004 11:18 am, Rob Torres wrote:
> Hello,
> I am writing a simple perl script to count the # of characters in a given
> field.  At the command line I can write >>wc -m somefile.txt, and get the
> total character count.  But in the script I am splitting a text file and
> then doing a count on an element of the array.
>
> code snippet:
> **************************
> while (<FILE> )
> {
>     chomp;
>     (@fields) = split(/\t/);
>     $count = wc -m $fields[12];
>     print "$count\n";
> }
> **************************
>
> Here is the error I receive when I check the syntax.
> 
>
> Unrecognized file test: -m at count.pl line 20.
>
>
> I've tried many different ways of writing this, but can't seem to get it
> working.  Any ideas/help?
> Thanks,
> Rob

Rob -

You're mixing Xs and Os. 'wc' is an external program that must be
run under a 'shell' (bash, sh, etc.) - NOT perl. Although you can
execucte external commands within perl ( via 'system' or '`...`', this
script should use perl internal functions like:

**************************

use strict; # get in this habit
use warnings;

while (<FILE> )
{
chomp;
my @fields = split(/\t/);
my $count = 0;
for my $field( @fields ) {
$count += length $field;
}
print "$count\n";
}
**************************

Aloha => Beau;



Report this thread to moderator Post Follow-up to this message
Old Post
Beau E. Cox
03-27-04 04:14 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:50 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.