LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-21-2006, 12:33 PM   #1
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Rep: Reputation: 15
Question Perl char frequency in a string doesn't work


Hi guys,

Do you know know how to find the char frequency in a given string using perl? The method Im using sometimes work. Maybe my regular expression is wrong.

I was using:

Code:
   my $mystring = ", Hello, you, boy and, gurl";
   my $columns=0;
   $columns = split(/\,/,$mystring);
Sometimes it is not counting well when Im modifying the string.

I got this error message also:
PHP Code:
Use of implicit split to @_ is deprecated 
Regards
 
Old 07-21-2006, 12:59 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

It's basically advising you to "split()" into an *array*, not a scalar.

This link might help:
http://www.uni-kassel.de/hrz/server/...unc/split.html
 
Old 07-21-2006, 01:01 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Try this instead (using an array for columns instead of a scalar):

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $mystring = ", Hello, you, boy and, gurl";
my @columns = split(/\,/,$mystring);

print @columns . "\n";
Hope this helps.
 
Old 07-21-2006, 01:49 PM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You also won't need the backslash. If you're afraid of defining another array, you can just put it into an anonymous one.
Code:
my $columns = scalar @{[ split(/,/,$mystring) ]};
This solution goes a major step into the direction of making the code as unreadable as Perl code is supposed to be.
 
Old 07-21-2006, 03:54 PM   #5
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna
Hi,

Try this instead (using an array for columns instead of a scalar):

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $mystring = ", Hello, you, boy and, gurl";
my @columns = split(/\,/,$mystring);

print @columns . "\n";
Hope this helps.
Thanks Druuna.

Althouhg it gave me a 5. It must to be 4.
I dont know why it is not working well
 
Old 07-21-2006, 03:57 PM   #6
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by spirit receiver
Code:
my $columns = scalar @{[ split(/,/,$mystring) ]};
Thanks Spirit,

I got this other message with this one:
Code:
$./c
Global symbol "@columns" requires explicit package name at ./c line 10.
Execution of ./c aborted due to compilation errors.
$
 
Old 07-21-2006, 04:07 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Quote:
Originally Posted by richikiki
Thanks Druuna.

Althouhg it gave me a 5. It must to be 4.
I dont know why it is not working well
5 is the correct answer: If you split this on a , (comma) , Hello, you, boy and, gurl, you end up with 5 parts. The first part (before the very first comma) is empty, 2 is Hello, 3 is you, 4 is boy and, 5 is gurl

Hope this clear things up a bit.
 
Old 07-21-2006, 04:41 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi again,

Quote:
Originally Posted by richikiki
Thanks Spirit,

I got this other message with this one:
Code:
$./c
Global symbol "@columns" requires explicit package name at ./c line 10.
Execution of ./c aborted due to compilation errors.
$
Ok, I'm not spirit receiver but I don't think he minds helping out here

The message perl is printing tells you that @columns is not declared (my @columns).

But after saying that, you don't need @columns if you use Spirit Receivers example. You need $column. Here's a working version using Spirit Receivers example:

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $mystring = ", Hello, you, boy and, gurl";
my $columns = scalar @{[ split(/,/,$mystring) ]};

print $columns . "\n";
Hope this also clears this up.
 
Old 07-21-2006, 04:49 PM   #9
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Quote:
Originally Posted by druuna
Ok, I'm not spirit receiver but I don't think he minds helping out here
Hope this also clears this up.
Thanks a lot.
 
Old 07-21-2006, 05:50 PM   #10
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
you can also do something like
Code:
my $mystring = ", Hello, you, boy and, gurl";
my $columns = () = $mystring =~ /,/g;
this always works to count the frequency of a substring

Last edited by spooon; 07-21-2006 at 05:52 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Check if "$str" is of type [A-Z]<char string>[0-9][0-9] introuble Programming 2 06-02-2006 11:33 AM
How to convert string to char? twirl Programming 27 10-13-2005 08:11 AM
c++ : replacing a char in a string cb951303 Programming 17 06-06-2005 06:48 PM
Convert C++ string to C char* nyk Programming 3 06-17-2004 09:15 AM
Perl : Changing a single char in a string richhill Programming 2 09-17-2003 05:31 PM


All times are GMT -5. The time now is 04:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration