Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
 Post subject:
PostPosted: Sun Jan 27, 2008 4:09 pm 
Offline
Junior Member

Joined: Mon Oct 29, 2007 10:12 am
Posts: 33
OK, printline could be a function/method, eg:

Code:
my $s = "This is a string";
while ($s =~ s/^(\w+ ?)//) {printline($1);}

sub printline {
  my $word = shift;
  print $word, "\n"; # or, less clear but all on one line: print $_[0], "\n";
}


In that case your example would indeed have done as you suggested, and printed one word per line. The bracket - printline() - just helps make it a bit clearer and less ambiguous, and means printline doesn't need to be pre-declared.

And of course I need to point out that
Quote:
print join '\n', split / /, "This is a string";

should read
Code:
print join "\n", split / /, "This is a string";

otherwise \n is interpreted literally - This\nis\na\nstring - which I'm sure was originally a typo and you knew that already :)


Top
   
 Post subject:
PostPosted: Sun Jan 27, 2008 5:14 pm 
Offline
Senior Member
User avatar

Joined: Tue Apr 13, 2004 6:54 pm
Posts: 833
Of course, most of the time you're not dealing with a hard coded string but a variable, so

$var =~ tr/ /\n/;

_________________
Rgds
Stephen
(Linux user since kernel version 0.11)


Top
   
 Post subject:
PostPosted: Sun Jan 27, 2008 7:04 pm 
Offline
Senior Newbie

Joined: Sat Jan 19, 2008 8:37 pm
Posts: 19
jti wrote:
OK, printline could be a function/method, eg:

Code:
my $s = "This is a string";
while ($s =~ s/^(\w+ ?)//) {printline($1);}

sub printline {
  my $word = shift;
  print $word, "\n"; # or, less clear but all on one line: print $_[0], "\n";
}


In that case your example would indeed have done as you suggested, and printed one word per line. The bracket - printline() - just helps make it a bit clearer and less ambiguous, and means printline doesn't need to be pre-declared.

And of course I need to point out that
Quote:
print join '\n', split / /, "This is a string";

should read
Code:
print join "\n", split / /, "This is a string";

otherwise \n is interpreted literally - This\nis\na\nstring - which I'm sure was originally a typo and you knew that already :)


oh, what i was thinking for printline(str) was more something like: sub printline(str) {print str; print "\n";} and by "exercise for the reader" i just meant i was too lazy to type out the extra parts or think of a good way to put that newline character at the end of the string and so just fudged it.

EDIT oh nevermind thats the same as what you did, i just was having problems reading and thinking on that day i guess


Last edited by cz9qvh on Mon Jan 28, 2008 9:13 pm, edited 1 time in total.

Top
   
 Post subject:
PostPosted: Mon Jan 28, 2008 4:17 pm 
Offline
Senior Member

Joined: Sun Nov 30, 2003 2:28 pm
Posts: 245
cz9qvh wrote:
Python would be similar to #1 yes? I would provide a python example but i'm sure it would suck.


Just for the record the obvious way to do it in python (which is usually the best), is
Code:
s = "This is a string"
for w in s.split():
    print w

_________________
The irony is that Bill Gates claims to be making a stable operating system and Linus Torvalds claims to be trying to take over the world.
-- seen on the net


Top
   
 Post subject:
PostPosted: Thu Jan 31, 2008 1:02 pm 
Offline
Senior Member

Joined: Fri Feb 13, 2004 11:30 am
Posts: 140
Location: England, UK
You can also do that last example in Perl too:

Code:
$s = "This is a string";
foreach $w (split(' ', $s)) {
    print "$w\n";
}


Or a bit less verbose:

Code:
$s = "This is a string";
for (split(' ', $s)) {
    print "$_\n";
}


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group