Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > First Commercial Perl Program

Reply
Thread Tools

First Commercial Perl Program

 
 
Rainer Weikusat
Guest
Posts: n/a
 
      04-03-2012
Justin C <> writes:
> On 2012-04-03, Jim Gibson <> wrote:


[...]


>>> use List::Util qw(shuffle);
>>> my $str='1234567890!@#$
>>> %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
>>> =[]\;\',./1234567890!@#$
>>> %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
>>> =[]\;\',./';

>>
>> Here is a way to build up your source string from pieces that are
>> easier to type and understand:
>>
>> my $numbers = join('',(0..9));
>> my $letters = join('',('a'..'z'));
>> my $ucletters = uc $letters
>> my $punct = q(!@#$%^&*_+{}|:"<>?~`-=[];',./) . "()";
>> my $str = $numbers . $letters. $ucletters. $punct;
>> $str .= $str;

>
> Seeing as the OP, after constructing a string, then converts it into an
> array, why not skip straight to the array... at least with the easy
> bits:
>
> my @bits = (0..9, "a".."z", "A".."Z");
> my $punct = q(!@#$%^&*_+{}|:"<>?~`-=[];',./) . "()";
> push @bits, $_ for (split(//, $punct));


Including the result of this code as literal value in the source is a
better idea than regenerating it everytime the code runs. A way to do
this with an array (assumes ASCII and relies on ord('(') < ord(')')) would be:

----------
@chars = map { $c = chr($_); $c =~ /[[:graph:]]/ ? $c : (); } (0 .. 127);
print('my @chars = qw(', join(' ', @chars, @chars), ");\n");
----------

This will print Perl-code creating and initializing the array @chars
with a suitable set of values and this code can then be included
in the script. It is also a nice example of something useful which
can't be 'enhanced' by use warning; use strict .
 
Reply With Quote
 
 
 
 
Justin C
Guest
Posts: n/a
 
      04-03-2012
On 2012-04-03, Shmuel Metz <> wrote:
> In <s3uq49->, on 04/03/2012
> at 10:56 AM, Justin C <> said:
>
>>my $punct = q(!@#$%^&*_+{}|:"<>?~`-=[];',./) . "()";
>>push @bits, $_ for (split(//, $punct));

>
> Why not push @bits, split(//, $punct);
>
> or eliminate $punct and use
>
> push @bits, split(//, q(!@#$%^&*_+{}|:"<>?~`-=[];',./) . "()");


'cos I'm not that smart, yet...

I'm working on it, though.

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      04-03-2012
Shmuel (Seymour J.) Metz <> writes:

[...]

> or eliminate $punct and use
>
> push @bits, split(//, q(!@#$%^&*_+{}|:"<>?~`-=[];',./) . "()");


[rw@sapphire]~ $perl -e '$a = q(()); print $a, "\n";'
()
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      04-06-2012
In article < >, Rainer
Weikusat <> wrote:

> Jim Gibson <> writes:
>
> [...]
>
> >> my $str='1234567890!@#$
> >> %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> >> =[]\;\',./1234567890!@#$
> >> %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> >> =[]\;\',./';

>
> [...]
>
> > their aren't any whitespace characters in your source, so their
> > shouldn't be any in your output.

>
> There are: the newlines (\n).


Ah, yes. My mistake. Thanks for the correction.

--
Jim Gibson
 
Reply With Quote
 
tbb!/fbr!
Guest
Posts: n/a
 
      04-16-2012
On Monday, April 2, 2012 1:50:46 PM UTC-7, Rainer Weikusat wrote:
> "tbb!/fbr!" <> writes:
>
> [...]
>
>
> > use List::Util qw(shuffle);
> > my $str='1234567890!@#$
> > %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> > =[]\;\',./1234567890!@#$
> > %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> > =[]\;\',./';
> > my @bits=split(//,$str);
> > my @rnd=shuffle(@bits);
> > my $stringer="@rnd";
> > $stringer=~s/\s//g;
> > $stringer=substr($stringer,0,140);

>
> $stringer = substr(join('', shuffle(grep { $_ !~ /\s/; } split(//, $str))), 0, 140);
>
> Especially, it makes more sense to throw the whitespace away before
> shuffling etc than to pass it through to the second-to-last stage of
> this 'processing pipeline' where it is deleted from the string.


it was actually a requirement that spaces be available in the output. please hold out on a reply, as I have recoded the entire routine to work exactly to specifications (as given to me) and will be posting it below.

thank you very much for the reply, as well as showing me how to combine my statements.

Ron
 
Reply With Quote
 
tbb!/fbr!
Guest
Posts: n/a
 
      04-16-2012
> 'cos I'm not that smart, yet...
> I'm working on it, though.
> Justin.
> --
> Justin C, by the sea.


tis true. I've been doing this for about a year now, and am getting better. thank you guys for the critique and help.

Ron

 
Reply With Quote
 
tbb!/fbr!
Guest
Posts: n/a
 
      04-16-2012
On Thursday, March 29, 2012 6:48:25 PM UTC-7, tbb!/fbr! wrote:
> use List::Util qw(shuffle);
> my $str='1234567890!@#$
> %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> =[]\;\',./1234567890!@#$
> %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> =[]\;\',./';
> my @bits=split(//,$str);
> my @rnd=shuffle(@bits);
> my $stringer="@rnd";
> $stringer=~s/\s//g;
> $stringer=substr($stringer,0,140);
>


That was all wrong. It wasn't randomizing properly (I was cheating with theshuffle routine). Each character had to be random and the space character needed to be there too (hence the characters needed the ability to 'repeat'if that's what randomly came up). Here's how I recoded that whole bit:

my $str='1234567890!@#$%^&*()qwertyuiopasdfghjklzxcvb nmQWERTYUIOPASDFGHJKLZXCVBNM_+{}|:"<>?~`-=[]\;\'\\,./';
my @bits=split(//,"$str");
unshift @bits, "\040";
my $range=140;
my $rndn=int(rand($range));
my @nbits;

my $count=0;
while ($count < $rndn) {
$nbits[$count]=@bits[rand @bits];
$count++;
}

my $variable;
open JERK, '>', \$variable;
print JERK @nbits;
print $variable;

The randomization is truly random (or a reasonable facsimile), each character gets it's chance, and I've allowed for the space character to show up asa space character in the string.

I couldn't figure out how to capture the output of print to a variable (I looked into sprintf, but that didn't do it either (because of the transform string to array, and then back again, without the spaces). The 'my $variable' section has some debugging code in there, but the only way I could figure out how to maintain the string properly formatted with space characters,was by doing some googling and using the file handle print to variable method.

Being a novice perl programmer, I don't have enough experience and exposureto do it 'properly' however now the code does exactly what's it's suppose to, and it's in 'production'.

As you can see up there in the $str variable, I couldn't just put a space character there and have it properly 'processed' hence the unshift @bits \040 (which is the space character).

The work is all in the while routine, and again, it works exactly the way the requirements wanted.

I look forward to further critique and learning.

Thank you everyone for not flaming another perl novice. Which I think meansI might have made a 'proper' newsgroup post.

Thanks,
Ron
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      04-17-2012
In article
<7095990.3.1334609061053.JavaMail.geo-discussion-forums@pbcva6>,
tbb!/fbr! <> wrote:

> On Thursday, March 29, 2012 6:48:25 PM UTC-7, tbb!/fbr! wrote:
> > use List::Util qw(shuffle);
> > my $str='1234567890!@#$
> > %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> > =[]\;\',./1234567890!@#$
> > %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> > =[]\;\',./';
> > my @bits=split(//,$str);
> > my @rnd=shuffle(@bits);
> > my $stringer="@rnd";
> > $stringer=~s/\s//g;
> > $stringer=substr($stringer,0,140);
> >

>
> That was all wrong. It wasn't randomizing properly (I was cheating with the
> shuffle routine). Each character had to be random and the space character
> needed to be there too (hence the characters needed the ability to 'repeat'
> if that's what randomly came up). Here's how I recoded that whole bit:
>
> my
> $str='1234567890!@#$%^&*()qwertyuiopasdfghjklzxcvb nmQWERTYUIOPASDFGHJKLZXCVBNM
> _+{}|:"<>?~`-=[]\;\'\\,./';
> my @bits=split(//,"$str");
> unshift @bits, "\040";


split(//,$str) will handle a space character just like other
characters, so you don't need to add "\040" to the array, just put it
in the assignment.

> my $range=140;
> my $rndn=int(rand($range));
> my @nbits;
>
> my $count=0;
> while ($count < $rndn) {
> $nbits[$count]=@bits[rand @bits];


That should be:

$nbits[$count] = $bits[rand @bits];

> $count++;
> }


You could also use this:

push( @nbits, $bits[rand @bits] ) for 1..$rndn;

>
> my $variable;
> open JERK, '>', \$variable;
> print JERK @nbits;
> print $variable;


my $variable = join('',@nbits);

--
Jim Gibson
 
Reply With Quote
 
tbb!/fbr!
Guest
Posts: n/a
 
      04-23-2012
On Monday, April 16, 2012 5:43:11 PM UTC-7, Ben Morrow wrote:
> Quoth Jim Gibson <>:
> > In article
> > <7095990.3.1334609061053.JavaMail.geo-discussion-forums@pbcva6>,
> > tbb!/fbr! <> wrote:
> > > my @bits=split(//,"$str");

> perldoc -q quoting


noted.

> > > unshift @bits, "\040";

> > split(//,$str) will handle a space character just like other
> > characters, so you don't need to add "\040" to the array, just put it
> > in the assignment.
> > > my $range=140;
> > > my $rndn=int(rand($range));
> > > my @nbits;
> > > my $count=0;
> > > while ($count < $rndn) {
> > > $nbits[$count]=@bits[rand @bits];

> > That should be:
> >
> > $nbits[$count] = $bits[rand @bits];

>
> ...and if you had been using 'warnings', perl would have told you so.


interesting enough, i always code under strict and warnings, and perl 5.10
on my Centos box doesn't complain with the way I've written it (does someone
know why? as I understand it's a scalar, but again, no warnings messages
pop up with: $nbits[$count]=@bits[rand @bits]; as opposed to:
$nbits[$count] = $bits[rand @bits];

> > > $count++;
> > > }

> >
> > You could also use this:
> > push( @nbits, $bits[rand @bits] ) for 1..$rndn;

> Or
> my @nbits = map $bits[rand @bits], 1..$rndn;
> Ben


You guys have been awesome here, and I am learning to code(?) a little more
efficiently in perl because of it, as well as everyone has been giving me
really good advice which I understand, and I haven't been flamed once! Very
cool.

I need to reread through this entire thread again, because there is some
learning here for me.

I'm working on another project now. It's a project where I read a line from
one file (the file has multiple lines) and then check to see if a certain
field from a second file matches. I already know how to do this with a nested
foreach reading in a line from the first file, and then a foreach (for) to
compare to all lines in the second file. That verbal explanation is how I
am doing it, and after much googling, it looks like everyone else does it that
way too. However, the datasets are HUGE (first file is 500m, the compare file
is 1tb). Is there a more efficient way to do this? For each line in the first
file, it has to go through every line in the 1tb file. And on and on. I have
been looking at references, but if I used references, it would only be a
'different' syntax for doing the same thing. How would I do some sort of
parallel processing on something like this. I did look at the threading model,
and thought that I would load say the first five lines and have a separate
thread for reading through that second file, but again, it seems like only
a workaround for the way I am already doing it. I hope this is making sense.

Anyways, thanks again for everything guys, and I still get a kick out of
Randal Schwartz replying here (comp me a Perl class?).

Thanks,
Ron

 
Reply With Quote
 
tbb!/fbr!
Guest
Posts: n/a
 
      04-23-2012
On Monday, April 2, 2012 1:50:46 PM UTC-7, Rainer Weikusat wrote:
> > use List::Util qw(shuffle);
> > my $str='1234567890!@#$
> > %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> > =[]\;\',./1234567890!@#$
> > %^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJK LZXCVBNM_+{}|:"<>?~`-
> > =[]\;\',./';
> > my @bits=split(//,$str);
> > my @rnd=shuffle(@bits);
> > my $stringer="@rnd";
> > $stringer=~s/\s//g;
> > $stringer=substr($stringer,0,140);

>
> $stringer = substr(join('', shuffle(grep { $_ !~ /\s/; } split(//, $str))), 0, 140);
>
> Especially, it makes more sense to throw the whitespace away before
> shuffling etc than to pass it through to the second-to-last stage of
> this 'processing pipeline' where it is deleted from the string.


In a post further down, you'll see I needed whitespace to be one of the random characters chosen, so needed to modify the program (which is below). But it's still an awesome piece of stringing stuff together. You've essentially turned 5 lines into 1, and I look forward to the day I can see things that way. I know, it will take time and experience, but it's still awesome tome, and why I chose this newsgroup to ask what I hope are thoughtful newbie questions.

Thanks,
Ron
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
First Commercial Perl Program tbb!/fbr! C Programming 1 03-10-2012 07:16 PM
FAQ 2.15 Where can I buy a commercial version of perl? PerlFAQ Server Perl Misc 0 04-23-2011 10:00 PM
Commercial Java program bundled with 'free' database? Korto Java 29 11-15-2006 11:23 AM
Using embedded PERL with commercial applications? Patrick Finnegan Perl Misc 13 11-26-2004 06:54 PM
Perl 'system' Creates Program That Dies When First C Program Dies Christopher M. Lusardi Perl Misc 3 10-19-2003 11:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57