C vs Perl

R

red floyd

Noah said:
Where is the C++ version? I don't see it.

Don't know, but the original C code had an error. There was no new line, so it
would output the following;

I will not throw paper airplanes in class.I will not throw paper airplanes in
class.I will not....

Oh, here's a C++ version

#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<string> v(500,string("I will not throw paper airplanes in class."));
copy(v.begin(), v.end(), ostream_iterator(cout,"\n"));
return 0;
}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

red floyd escribió:
Oh, here's a C++ version

#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<string> v(500,string("I will not throw paper airplanes in class."));
copy(v.begin(), v.end(), ostream_iterator(cout,"\n"));
return 0;
}

Mine is better ;)

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main ()
{
fill_n (ostream_iterator <string> (cout, "\n"),
500, string ("I will not throw paper airplanes in
class") );
}

Regards.
 
J

Joona I Palaste

len v <[email protected]> scribbled the following
A recent (Oct 3) Fox Trox comic (Bill Amend ) got me thinking causing me to
edit the origional comic. Bill then had to write a patch, as most C
programers must do.( http://homepage.mac.com/billamend/images/patch.gif )
To see the comparison, go to ---> http://perl.hacker.freeservers.com/

Yes, and I could implement a language called FoxTrot that would have the
following program:

Do it

translate into code that wrote "I will not throw paper airplanes in
class" 500 times into stdout. But I wouldn't guarantee FoxTrot would be
useful for anything else.

IOW, one-off cases like these are useless for comparing elegance of
languages.
 
S

Serve La

red floyd said:
Oh, here's a C++ version

#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<string> v(500,string("I will not throw paper airplanes in class."));
copy(v.begin(), v.end(), ostream_iterator(cout,"\n"));
return 0;
}

Hmm, there's more includes than lines, what's the world coming to :)
 
L

len v

red floyd said:
Don't know, but the original C code had an error. There was no new line, so it
would output the following;

I will not throw paper airplanes in class.I will not throw paper airplanes in
class.I will not....

Oh, here's a C++ version

#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<string> v(500,string("I will not throw paper airplanes in class."));
copy(v.begin(), v.end(), ostream_iterator(cout,"\n"));
return 0;
}


The author of the comic received the same ridicule regarding the
error.... --> http://homepage.mac.com/billamend/ Half way down you will
find his patch.


umm.... I don't think I can fit the c++ version on the chalk board.
 
R

red floyd

len said:
[redacted]
umm.... I don't think I can fit the c++ version on the chalk board.

What about Julian Albo's (sorry Julian, don't know how to put the accent on in Moz...)?

His is three lines shorter:
doesn't need #include <vector>
uses the implicit return 0 from main
doesn't need to declare the vector I used

Would that fit? :)
 
L

len v

Joe Wright said:
I just hate spilling errors.


Note to self. Find spell checker for vi.

Looks like I spelled it wrong on the web site too. I've always maintained
that a programmer does not have to spell correctly, just consistent, either
right or wrong - the syntax checker does not care.
 
L

len v

red floyd said:
len said:
[redacted]
umm.... I don't think I can fit the c++ version on the chalk board.

What about Julian Albo's (sorry Julian, don't know how to put the accent on in Moz...)?

His is three lines shorter:
doesn't need #include <vector>
uses the implicit return 0 from main
doesn't need to declare the vector I used

Would that fit? :)

That just might do it. I'll try and repost the link.

I wonder if I could fit COBOL, Fortran or Java code in there too. Ouch. My
brain just started to hurt. It's been awhile.
 
L

len v

Joona I Palaste said:
len v <[email protected]> scribbled the following
ttp://homepage.mac.com/billamend/images/patch.gif )
Yes, and I could implement a language called FoxTrot that would have the
following program:

Do it

translate into code that wrote "I will not throw paper airplanes in
class" 500 times into stdout. But I wouldn't guarantee FoxTrot would be
useful for anything else.

IOW, one-off cases like these are useless for comparing elegance of
languages.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It sure is cool having money and chicks."
- Beavis and Butt-head


I see your point and that is exactly what Perl is all about. If your
FoxTrot implementation had something to offer the community then FoxTrot
would be added to Perl.

Perhaps you could help me out? I've been trying to remember my C and C++
code, it's been about 5 years. How do I swap the values of 2 strings?
 
J

Jirka Klaue

len v wrote:
....
Perhaps you could help me out? I've been trying to remember my C and C++
code, it's been about 5 years. How do I swap the values of 2 strings?

Could you define "value of string", please?

Jirka
 
L

len v

Jirka Klaue said:
len v wrote:
...

Could you define "value of string", please?

Jirka

Sure --
I was using a string (scalar) as an example, it could be any two basic types
(ex: int's, char's, float's, reference's, arrays etc.) However, in keeping
with the original question:

CString m_firstName;
CString m_lastName;

put the value of (contents of, address of) m_firstName in m_lastName and put
m_lastName in m_firstName.

Regards
Len
 
J

Jirka Klaue

len said:
....
Sure -- ....
CString m_firstName;
CString m_lastName;

There is no CString in C, C++ or Perl.

To clarify my question:

char a[] = "C", b[] = "C++";

char *a = "C", *b = "C++";

Are a and b strings? What are their values?
How would *you* swap their "values"?

Jirka
 
L

len v

Jirka Klaue said:
len said:
...
Sure -- ...
CString m_firstName;
CString m_lastName;

There is no CString in C, C++ or Perl.

To clarify my question:

char a[] = "C", b[] = "C++";

char *a = "C", *b = "C++";

Are a and b strings? What are their values?
How would *you* swap their "values"?

Jirka

Perhaps I'm not reading it correctly:
http://support.microsoft.com/default.aspx?scid=kb;en-us;111923

A string would be a scalar. In Perl it would be $a and $b
http://www.perl.com/pub/a/2000/11/begperl2.html No need to declare the
type. Scalars can be a numbers, text, references to a scalar, references to
an array, or references to a hash, or references to a sub (a function or
method). And no need to pre-declare the variable prior to its' use.

Example of strings (scalars) in Perl:

$a = 'C';
$b = 'C++';

$tmp = $a;
$a = $b;
$b = $tmp;

However this is too much work for me, so I was thinking more like:
$a = 'C';
$b = 'C++';

($a, $b) = ($b, $a);

or

($a, $b) = ('C', 'C++');
($a, $b) = ($b, $a);


Regards
Len
 
J

Jirka Klaue

len said:

CString is part of MFC which is *not* part of C++.
Ever heard of std::string?
Example of strings (scalars) in Perl:

$a = 'C';
$b = 'C++';

$tmp = $a;
$a = $b;
$b = $tmp;

However this is too much work for me, so I was thinking more like:
$a = 'C';
$b = 'C++';

($a, $b) = ($b, $a);

or

($a, $b) = ('C', 'C++');
($a, $b) = ($b, $a);

std::swap(a, b);

Jirka
 
N

Noah Roberts

len said:
Note to self. Find spell checker for vi.

Looks like I spelled it wrong on the web site too. I've always maintained
that a programmer does not have to spell correctly, just consistent, either
right or wrong - the syntax checker does not care.

My compiler really doesn't like it when I spell freind wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top