white-fox has asked for the wisdom of the Perl Monks concerning the following question:

hello.... i have problem with my little code.....
i want print a text but one by one alphabet..for example....
ifor "Welcome" i want first print "W" and after a little delay print "e" and go on...
but my little code...can't do this please help me.

==================================================
#!/usr/bin/perl -w use strict; use warnings; my $text="this is a sample"; my @word=split(//, "$text"); for (0 .. $#word){ print "$word[$_]"; sleep 2; } print "\n";

Replies are listed 'Best First'.
Re: problem with delay(my little code)
by Corion (Patriarch) on Mar 24, 2005 at 12:45 UTC
Re: problem with delay(my little code)
by manav (Scribe) on Mar 24, 2005 at 12:53 UTC
    use strict; use warnings; $|++ ; my $text="this is a sample"; my @word=split(//, "$text"); for (@word){ print "$_"; sleep 2; } print "\n";


    Manav
Re: problem with delay(my little code)
by Random_Walk (Prior) on Mar 24, 2005 at 14:48 UTC

    Hi there, the answer to your question is the buffering issue but here are a coulpe more points that may be of interest.

    #!/usr/bin/perl -w # -w is the old way to do use warnings, you can drop it # as you use warnings anyway use strict; use warnings; my $text="this is a sample"; # no need to make the following intermediate array # no need to use the brackets for split unless to disambiguate # no need to "quote" $text my @word=split(//, "$text"); # if we loose @word we can just loop directly on the split for (0 .. $#word){ # no need to quote $word[$_] here either print "$word[$_]"; sleep 2; } print "\n"; ###################### # new shorter code.. # ###################### #!/usr/bin/perl use strict; use warnings; $|++; my $text="this is a sample"; for (split //, $text) { print; # defaults to print $_, some don't like this style sleep 2; } print "\n";

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: problem with delay(my little code)
by white-fox (Novice) on Mar 24, 2005 at 12:59 UTC
    thanks for your help...my problem was solve!..
Re: problem with delay(my little code)
by ambrus (Abbot) on Mar 24, 2005 at 17:48 UTC