Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hiya. This is what I want to do:
I want to write a Perl script that takes web site alphanumeric submissions. For each alphanumeric submission, I want the script to separate the initial alphanumeric submission into strings that:
-When combined, they equal the initial alphanumeric submission
-Do not exceed 100 characters per string.
For example, consider the following:
$initial = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwerty +uiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcv +bnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdf +ghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm";

$initial has 234 characters. I would like to write a script that chops up $initial into the following:
$chop1 = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyui +opasdfghjklzxcvbnmqwertyuiopasdfghjklzxc"; # (contains first 100 char +acters) $chop2 = "vbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwer +tyuiopasdfghjklzxcvbnmqwertyuiopasdfghjk"; # (contains second 100 cha +racters) $chop3 = "lzxcvbnmqwertyuiopasdfghjklzxcvbnm"; # (contains remaining 3 +4 characters)

such that $initial = "$chop1$chop2$chop3";

Then, I would like the amount of emails as there are chops. For example, since there were three divisions of $initial, I would want to send each division in a private email (three emails would be total since there are three divisions in existance). I was thinking about doing something that implements foreach. The only problem is that I don't know how to incorporate the divisions into an array.

Help would be VERY much appreciated.
Thanks,
Tony

2002-04-22 Edit by Corion : Added CODE tags.

Replies are listed 'Best First'.
Re: Parsing Form Input
by abstracts (Hermit) on Apr 22, 2002 at 04:05 UTC
    which part of the problem are you stuck with? or do you not know where to start?

    I'd suggest starting with buying a good book: Learning Perl should be a good choice. Pick it up, read it, read it again, try to solve your problem. After doing that, if you're still stuck, I'd be more happy to help.

    Update: Seems like fellow monks prefer spoon-feeding replies even for such a question. Well so be it :-)

    Update2: Please don't allow people to submit something like linux-2.4.xx.tar.gz to your cgi script :-)

    #!/usr/bin/perl -w use strict; use Mail::Sendmail; #put your smtp server instead of localhost in the next line unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'localhost'; my $initial = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwe +rtyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklz +xcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopa +sdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm"; while(length $initial){ # while line is not empty my $msg = substr $initial, 0, 100, ""; # chop 100 chars my %mail = ( "To" => 'abstract@mirage', # the address you want to send to "From" => 'abstract@mirage', # your email address "Subject" => 'This is a test message!', # the subject "Message" => $msg); # attempt to send the message sendmail(%mail) or die "Error sending email: $Mail::Sendmail::error\n"; }
Re: Parsing Form Input
by Kanji (Parson) on Apr 22, 2002 at 04:30 UTC

    substr can help with carving up a string, and if you use it coupled with an array, you could easily tell how many chunks you split the string into by seeing how big the array is...

    my @chunks; while ( 1 ) { my $chunk = substr( $initial, 0, 100, "" ); last unless length $chunk; # was it empty? push @chunks, $chunk; } print scalar(@chunks), " chunks\n";

        --k.


      you can simplify that loop by:
      sub LIMIT () { 100 } my @chunk; while( my $chunk = substr( $initial, 0, LIMIT, '') ) { push @chunks, $chunk; }
      actually i threw a constant in there as well. it won't simplify the loop, but it should ease maintenance.

      ~Particle ;Þ

Re: Parsing Form Input
by demerphq (Chancellor) on Apr 22, 2002 at 10:41 UTC
    Its probably not the most efficient way to do this, but its probably the easiest
    my @blocks=($text=~m/\G.{0,100}/gs);
    HTH

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.