CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Counting Characters at at time.

 



upyeronson
Novice

Aug 5, 2005, 12:26 PM

Post #1 of 7 (5315 views)
Counting Characters at at time. Can't Post

Is there a way to count characters at a time? I am trying to count 200 characters for each string, and then split those groups into a array. I know it's possible, but wanted to first check if I should try to do a loop to count, then a split once I reached say my 200th character. I'm a perl newbie, of course!

I have the following:

Code
#!/usr/bin/perl 
$file = 'data.txt' ; # Name the file
open(INFO, "<$file") ; # Open the file
close(INFO) ; # Close the file

$str = <INFO>;
@snippet @snippet = split (/ /, $str); # split into an array of size 200 characters each


I guess the split commands splits at white space. Is there an easy way to manipulate the function to split at some nth character, say 200 characters?

Thanks bunches! I look forward to contributing myself wherever possible.


(This post was edited by upyeronson on Aug 5, 2005, 12:27 PM)


upyeronson
Novice

Aug 5, 2005, 2:15 PM

Post #2 of 7 (5309 views)
Re: [upyeronson] Counting Characters at at time. [In reply to] Can't Post

With some researching on the forums, I was able to do the following:

1. Count first 200 characters in a file.


Code
#!/usr/bin/perl 
# Testing counting the number of characters
my $length = "10";
my $filename = "test.txt";
open FILE, $filename or die "cant open $filename!\n";
read FILE, $string, $length;
close FILE;
print "the string we are looking for is $string\n";


Now, if I wanted to loop from the first 200 to the next 200 until the end of file, I would need a loop, correct?

Thanks again for any help offered.
Up


davorg
Thaumaturge / Moderator

Aug 7, 2005, 9:53 AM

Post #3 of 7 (5299 views)
Re: [upyeronson] Counting Characters at at time. [In reply to] Can't Post

If you just want to read from a file 200 characters at a time, then just tell Perl that's what you want to do.


Code
{ 
open INFO, $file or die $!;

local $/ = \200;

while (<INFO>) {
# do something with your 200 chars (which are in $_)
}
close INFO;
}


Note: This actually reads 200 _bytes_, but that's the same as 200 characters unless you're using Unicode or some other wide character encoding.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


upyeronson
Novice

Aug 8, 2005, 8:27 AM

Post #4 of 7 (5294 views)
Re: [davorg] Counting Characters at at time. [In reply to] Can't Post

Thank you! That definitely helped me refine exactly what I was trying to do. I searched some more and discovered the substring function. I was thus able to create the following:

Code
$MAX_WIDTH = 200;  #Set the max width we will use 

print "Please enter The File to split into groups of 200 characters:\n";
$filename = <STDIN>;
open FILE, $filename or die "cant open $filename!\n";

@lines = <FILE>;

foreach (@lines) {
chomp;
$_=~s/\s+/ /g; # remove white space to one single "space"
$str .= $_;
}

$length = length($str);
while ($n < $length) {
print substr($str,$n,$MAX_WIDTH)."\n";
$n = $n + $MAX_WIDTH;
}
print substr($str,$n)."\n";


Now that I have my substring of 200 characters, I am having trouble conceptualizing how Perl couuld be used to:

1. Add word to substring if $word + substring < 200
2. Else print substring
3. Loop back to add word to substring.

Would I need to change my substring approach?
Thanks again Davorg!


rork
User

Aug 11, 2005, 1:38 AM

Post #5 of 7 (5271 views)
Re: [upyeronson] Counting Characters at at time. [In reply to] Can't Post

Use a while loop, looping back isn't an option because you will start with the same substring over and over again.


Code
while ((length($word) + length($substr) < 200) { 
$substr .= $word;
# probably some code to reset $word;
}
print $substr;

--
Don't reinvent the wheel, use it, abuse it or hack it.


davorg
Thaumaturge / Moderator

Aug 12, 2005, 5:15 AM

Post #6 of 7 (5259 views)
Re: [upyeronson] Counting Characters at at time. [In reply to] Can't Post

Well, I'm not really sure how my previous message could have helped you as you seem to have completely ignored my code when writing yours :)

So, what you actually seem to want to so is to pull data from a file, one word at a time until the next word would take you over 200 characters. Which is a completely different problem.


Code
my $max_len = 200; 

my $string = '';
while (<FILE>) {
foreach my $word (split) {
if (length($string) + length($word) + 1 > $max_len) {
# stop there
last;
} else {
$string .= " $word";
}
}
}


And, of course, this has nothing to do with regexes so it's in completely the wrong section of this site :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


(This post was edited by davorg on Aug 12, 2005, 5:16 AM)


upyeronson
Novice

Aug 17, 2005, 1:18 PM

Post #7 of 7 (5222 views)
Re: [davorg] Counting Characters at at time. [In reply to] Can't Post

Thank you David. I am appreciating that there are so many ways to do the same things in perl. Thanks again for your direction.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives