[Melbourne-pm] Melbourne-pm Digest, Vol 66, Issue 4

ajthornton jdthornton at ozemail.com.au
Sun Oct 4 17:37:18 PDT 2009


That is great! Because the next tutorials do teach things like loop and map
and arrays!

Oh what a feeling! Perl!

John  

-----Original Message-----
From: melbourne-pm-bounces+jdthornton=ozemail.com.au at pm.org
[mailto:melbourne-pm-bounces+jdthornton=ozemail.com.au at pm.org] On Behalf Of
melbourne-pm-request at pm.org
Sent: Monday, 5 October 2009 6:01 AM
To: melbourne-pm at pm.org
Subject: Melbourne-pm Digest, Vol 66, Issue 4

Send Melbourne-pm mailing list submissions to
	melbourne-pm at pm.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.pm.org/mailman/listinfo/melbourne-pm
or, via email, send a message with subject or body 'help' to
	melbourne-pm-request at pm.org

You can reach the person managing the list at
	melbourne-pm-owner at pm.org

When replying, please edit your Subject line so it is more specific than
"Re: Contents of Melbourne-pm digest..."


Today's Topics:

   1. Scaling perl (ajthornton)
   2. Re: Scaling perl (ajthornton)
   3. Re: Scaling perl (Daniel Pittman)
   4. Re: Scaling perl (Jacinta Richardson)


----------------------------------------------------------------------

Message: 1
Date: Sun, 4 Oct 2009 14:53:21 +1100
From: "ajthornton" <jdthornton at ozemail.com.au>
Subject: [Melbourne-pm] Scaling perl
To: <melbourne-pm at pm.org>
Message-ID: <B3F21CC3D05A4012A47BDA8328D55018 at homepc>
Content-Type: text/plain;	charset="us-ascii"


         Hello
              OK, I kept my promise to try to enjoy perl. I looked up some
tutorials on text processing and cut and paste ane xample, before playing
with it and coming up with:

             #!/usr/bin/perl

$str = "The 34567744444444 foxes jumped over the ripe yellow pumpkin";
$str2= "The spaceman is a mutt in a spiderman suit"; $str3= "Melbourne
Storm"; $comma = ','; # returns ?
print length ($str);
print $comma;
print length ($str2);
print $comma;
print length ($str3);



IN Padre this runs; it's a correct program. It returns 60,42,15 

The original example had only str and did not put output between commas; I
nutted those things out. But imagine if there were more $str say up to
$str20000000000000 or whatever. Then it becomes bad to write print $comma;
that many times!!!!!!!!! What a waste. Also better, come to think of it,
would be to put the string lines in what I call a series [once a msths grad,
always a maths grad] , say print length ($str...+ 1...$str2000000) to give a
series of strings to be printed. [I expect that that example is nonsense;
just made it up - I was trying to do the same thing that from memory is done
in Python but in that you use i= or something] Then we could run a huge
program with very little sourcecode to write.

In short, for "real world" size tasks as above would the sourcecode be
scalable? If so , how? Am I on the right track?

I am enjoying perl! :)  

John   




------------------------------

Message: 2
Date: Sun, 4 Oct 2009 15:24:29 +1100
From: "ajthornton" <jdthornton at ozemail.com.au>
Subject: Re: [Melbourne-pm] Scaling perl
To: "'ajthornton'" <jdthornton at ozemail.com.au>
Cc: melbourne-pm at pm.org
Message-ID: <1F2A9396F03846A18D08DFEE243DA1A1 at homepc>
Content-Type: text/plain;	charset="us-ascii"

 
P.s pasting from padre to perl to here messed up the code; but it still
works in padre. Perl. Maybe it has grown on me like a song that you don't
like until you've heard it a few times.  

-----Original Message-----
From: ajthornton [mailto:jdthornton at ozemail.com.au]
Sent: Sunday, 4 October 2009 2:53 PM
To: 'melbourne-pm at pm.org'
Subject: Scaling perl


         Hello
              OK, I kept my promise to try to enjoy perl. I looked up some
tutorials on text processing and cut and paste ane xample, before playing
with it and coming up with:

             #!/usr/bin/perl

$str = "The 34567744444444 foxes jumped over the ripe yellow pumpkin";
$str2= "The spaceman is a mutt in a spiderman suit"; $str3= "Melbourne
Storm"; $comma = ','; # returns ?
print length ($str);
print $comma;
print length ($str2);
print $comma;
print length ($str3);



IN Padre this runs; it's a correct program. It returns 60,42,15 

The original example had only str and did not put output between commas; I
nutted those things out. But imagine if there were more $str say up to
$str20000000000000 or whatever. Then it becomes bad to write print $comma;
that many times!!!!!!!!! What a waste. Also better, come to think of it,
would be to put the string lines in what I call a series [once a msths grad,
always a maths grad] , say print length ($str...+ 1...$str2000000) to give a
series of strings to be printed. [I expect that that example is nonsense;
just made it up - I was trying to do the same thing that from memory is done
in Python but in that you use i= or something] Then we could run a huge
program with very little sourcecode to write.

In short, for "real world" size tasks as above would the sourcecode be
scalable? If so , how? Am I on the right track?

I am enjoying perl! :)  

John   




------------------------------

Message: 3
Date: Sun, 04 Oct 2009 15:20:22 +1100
From: Daniel Pittman <daniel at rimspace.net>
Subject: Re: [Melbourne-pm] Scaling perl
To: melbourne-pm at pm.org
Message-ID: <87eipj6b95.fsf at rimspace.net>
Content-Type: text/plain; charset=utf-8

"ajthornton" <jdthornton at ozemail.com.au> writes:

> OK, I kept my promise to try to enjoy perl. I looked up some tutorials on
> text processing and cut and paste ane xample, before playing with it and
> coming up with:
>
> #!/usr/bin/perl
> $str = "The 34567744444444 foxes jumped over the ripe yellow pumpkin";
> $str2= "The spaceman is a mutt in a spiderman suit";
> $str3= "Melbourne Storm";
> $comma = ',';
> # returns ?
> print length ($str);
> print $comma;
> print length ($str2);
> print $comma;
> print length ($str3);

[...]

> The original example had only str and did not put output between commas;

If you have hand-written the name of each string variable then, yeah, life
sucks.  Assuming something sensible like an array of 20,000 strings, though,
your code would be roughly:

    my @strings = qw{one two three ... twenty-thousand};
    print join(", ", map { length } @strings), "\n";

[...]

> In short, for "real world" size tasks as above would the sourcecode be
> scalable? If so, how? Am I on the right track?

Hand-naming variables?  As many as any other language, which means that by
the
time you are getting to ten or so you should look at using better data
structures or algorithms.

In my example, the "join" and "map" functions are the better algorithms:

join pastes together an array of strings with a bit of text between them,
which is exactly what you want to do to your three strings.[1]

map runs the same code on all the values in an array; in this case we
transform from the string to the length of the string.

It might be clearer to explain that map is, essentially, a loop like this:

    my @input = (...);
    my @output;
    foreach my $_ (@input) {
        # whatever goes in the middle is run here
        my $result = ...;
        # in this case = length($_);
    }
    @input = @output;

Regards,
        Daniel

Footnotes: 
[1]  print join(', ', $str, $str2, $str3);  works too.

-- 
? Daniel Pittman            ? daniel at rimspace.net            ? +61 401 155
707
               ? made with 100 percent post-consumer electrons
   Looking for work?  Love Perl?  In Melbourne, Australia?  We are hiring.


------------------------------

Message: 4
Date: Mon, 05 Oct 2009 02:11:57 +1100
From: Jacinta Richardson <jarich at perltraining.com.au>
Subject: Re: [Melbourne-pm] Scaling perl
To: melbourne-pm at pm.org
Message-ID: <4AC8BB3D.3000404 at perltraining.com.au>
Content-Type: text/plain; charset=utf-8; format=flowed

Daniel Pittman wrote:

>     my @strings = qw{one two three ... twenty-thousand};
>     print join(", ", map { length } @strings), "\n";


The only change I'd make to the above, is to show explicitly how to do this 
using the strings John was using (and be explicit about the argument to
length()):

	my @strings = (
		"The 34567744444444 foxes jumped over the ripe yellow
pumpkin",
		"The spaceman is a mutt in a spiderman suit",
		"Melbourne Storm",
	);

	print join( ", ", map { length $_ } @strings ), "\n";

This should print "60, 42, 15" followed by a newline.

The above uses a lot of short-hand and can be written several ways.  For
example 
  we can remove the map, using a foreach loop as Daniel mentioned:

	my @strings = (
		"The 34567744444444 foxes jumped over the ripe yellow
pumpkin",
		"The spaceman is a mutt in a spiderman suit",
		"Melbourne Storm",
	);

	my @lengths;
	foreach my $string (@strings) {
		push @lengths, ( length $string );
	}

	print join( ", ", @lengths ), "\n";

we could also generate the string before printing it, if that would be
useful:

	my $str_of_lengths = join( ", ", @lengths );
	print "$str_of_lengths\n";

but I'd only do this if we weren't planning to print it, but instead wanted
to 
return it from a subroutine or put it into an email etc.


The advantage of using an array for this kind of thing is that the code 
automatically works if we add a few more strings to our array.  We don't
have to 
create new variables or do any extra work.

All the best,

	J


------------------------------

_______________________________________________
Melbourne-pm mailing list
Melbourne-pm at pm.org
http://mail.pm.org/mailman/listinfo/melbourne-pm

End of Melbourne-pm Digest, Vol 66, Issue 4
*******************************************



More information about the Melbourne-pm mailing list