Ask a Question related to PERL Beginners, Design and Development.
-
Trent Rigsbee #1
Counting (easy!)
I'm sure this is easy but I'm a newbie. I was doing control statements (for,
while,etc.) like this:
for ($count = 1; $count <= 5; $count++) {
print "$count\n";
}
What I wanted to do was to make each number appear in sequence like you see
in a countdown (or up, in this case) instead of all on the screen at once.
I've tried using sleep but I'm not getting anywhere with it. Any ideas?
Also, any methods or ideas on how to approach creating code in general? For
example, what's the thought process for trying to do what I want to do? I've
tried pseudo code and it's helped some but then I hit the wall and don't how
to go further. Thanks!
__________________________________________________ _______________
Frustrated with dial-up? Get high-speed for as low as $26.95.
[url]https://broadband.msn.com[/url] (Prices may vary by service area.)
Trent Rigsbee Guest
-
Counting (easy!) - select???
Christiane Nerz wrote: It's used in this case for sleeping for less than one second. The sleep() function only sleeps for a whole number of... -
search for email address in file, was Counting (easy!)
Please choose a subject that is reflective of your post and start a new thread when appropriate... would I The @ in the above does need to be... -
Counting (easy!) (YES!!)
I think I figured it out! A FIRST!! for ($i = 1; $i <= 5; $i++){ sleep 1; print "$i\n"; } I prints out like this: 1...2...3...4...5 -
Easy question = easy answer?
Well, so I'm pretty new with Freehand, at the mo running with MX and havin' a problem (not big, but anyway)... I'm creating some cards and they... -
Easy Question/Easy Answer
Ok, this is all i want to know how to do, i made a text link, now when someone rolls over it with their pointer i want it to change color. Simple? -
Tim Johnson #2
RE: Counting (easy!)
Try using the "\b" character to erase your output.
-----Original Message-----
From: Trent Rigsbee [mailto:sweetdaddysiki@hotmail.com]
Sent: Wednesday, November 12, 2003 5:06 PM
To: [email]beginners@perl.org[/email]
Subject: Counting (easy!)
I'm sure this is easy but I'm a newbie. I was doing control statements
(for,
while,etc.) like this:
for ($count = 1; $count <= 5; $count++) {
print "$count\n"; }
What I wanted to do was to make each number appear in sequence like you
see in a countdown (or up, in this case) instead of all on the screen at
once.
I've tried using sleep but I'm not getting anywhere with it. Any ideas?
Also, any methods or ideas on how to approach creating code in general?
For example, what's the thought process for trying to do what I want to
do? I've tried pseudo code and it's helped some but then I hit the wall
and don't how to go further. Thanks!
__________________________________________________ _______________
Frustrated with dial-up? Get high-speed for as low as $26.95.
[url]https://broadband.msn.com[/url] (Prices may vary by service area.)
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email] For additional
commands, e-mail: [email]beginners-help@perl.org[/email]
Tim Johnson Guest
-
David #3
Re: Counting (easy!)
Trent Rigsbee wrote:
like count down from 5 to 1 slowly in a single row? try:> I'm sure this is easy but I'm a newbie. I was doing control statements
> (for, while,etc.) like this:
>
> for ($count = 1; $count <= 5; $count++) {
> print "$count\n";
> }
>
> What I wanted to do was to make each number appear in sequence like you
> see in a countdown (or up, in this case) instead of all on the screen at
> once. I've tried using sleep but I'm not getting anywhere with it. Any
> ideas? Also, any methods or ideas on how to approach creating code in
> general? For example, what's the thought process for trying to do what I
> want to do? I've tried pseudo code and it's helped some but then I hit the
> wall and don't how to go further. Thanks!
[panda]# perl -e '$|=1; print " @{[6-$_]}\r" and sleep(1) for(1..5)'
david
--
$_='015001450154015401570040016701570162015401440'
,*,=*|=*_,split+local$";map{~$_&1&&{$,<<=1,$#.=qq~
/63968e72w28@_[$_..$||3])=>~}}0..s,.,,g-01;*_=*#,s
[[s/eval+w/.`9`/e]]n\\xng.print+eval(eval"qq`$_`")
David Guest
-
Kevin Old #4
Re: Counting (easy!)
On Wed, 2003-11-12 at 20:05, Trent Rigsbee wrote:
Try this code....either of these might be what you're looking for> I'm sure this is easy but I'm a newbie. I was doing control statements (for,
> while,etc.) like this:
>
> for ($count = 1; $count <= 5; $count++) {
> print "$count\n";
> }
>
> What I wanted to do was to make each number appear in sequence like you see
> in a countdown (or up, in this case) instead of all on the screen at once.
> I've tried using sleep but I'm not getting anywhere with it. Any ideas?
> Also, any methods or ideas on how to approach creating code in general? For
> example, what's the thought process for trying to do what I want to do? I've
> tried pseudo code and it's helped some but then I hit the wall and don't how
> to go further. Thanks!
>
#!/usr/bin/perl
#
use warnings;
use strict;
print "printing 1thru5 with buffering\n";
select undef, undef, undef, 0.25 or print $_
for 1 .. 5;
print "\nflush forced\n";
$|++;
print "printing 1thru5 without buffering\n";
select undef, undef, undef, 0.25 or print $_
for 1 .. 5;
print "\nflush forced\n";
Hope this helps,
--
Kevin Old <kold@kold.homelinux.com>
Kevin Old Guest
-
Drieux #5
Re: Counting (easy!)
On Wednesday, Nov 12, 2003, at 18:07 US/Pacific, david wrote:
[..]Minor Nit, that "\r" will not actually go out>
> like count down from 5 to 1 slowly in a single row? try:
>
> [panda]# perl -e '$|=1; print " @{[6-$_]}\r" and sleep(1) for(1..5)'
>
> david
54321
rather pleasantly returns the cursor to the first position
since you were polite enough to leave a space out in front!
Also a lovely way of helping the OP move from 'c style'
for( my $i=1; $i <= 5; $i++)
into the more natural perlish
for(N..M)
construct, but you really should have forewarned him a
bit about our habit of the compound statement before
the interative, vice the more easy to 'fix'
my $max = 6;
my $sleeptime=1;
for(1..5)
{
print " @{[$max-$_]}\r" ;
sleep($sleeptime);
}
but props for the golf tip.....
Any help with my slice, I keep putting the ball
way off to the other fairway....
ciao
drieux
---
Drieux Guest
-
Tore Aursand #6
Re: Counting (easy!)
On Thu, 13 Nov 2003 01:05:37 +0000, Trent Rigsbee wrote:
This is very C'ish. In Perl we tend to:> for ($count = 1; $count <= 5; $count++) {
> print "$count\n";
> }
for ( 1..5 ) {
print $_ . "\n";
# sleep( 1 );
}
Uncomment the sleep() thing if you want Perl to sleep for 1 second for
each iteration. Try 'perldoc -f sleep' for more information on sleep().
I'm not sure what you really mean, but if you want to replace the previous> What I wanted to do was to make each number appear in sequence like you
> see in a countdown (or up, in this case) instead of all on the screen at
> once.
number with the new one, you need to send some backspaces (\b) to STDOUT;
my $from = 1;
my $to = 60;
my $prev = 0;
$| = 1; # We don't want to buffer the output
for ( $from .. $to ) {
print $_;
sleep( 1 );
print "\b" x length($prev);
$prev = $_;
}
If you really need your application to display some information on its
progress, you really should check out Term::ProgressBar on CPAN:
[url]http://www.cpan.org/[/url]
1. Learn Perl.> Also, any methods or ideas on how to approach creating code in general?
2. Try to do something.
3. If #2 fails, read any documentation you can find.
4. comp.lang.perl.misc
--
Tore Aursand <tore@aursand.no>
Tore Aursand Guest
-
John W. Krahn #7
Re: Counting (easy!)
Trent Rigsbee wrote:
In Perl that is usually written as:>
> I'm sure this is easy but I'm a newbie. I was doing control statements (for,
> while,etc.) like this:
>
> for ($count = 1; $count <= 5; $count++) {
> print "$count\n";
> }
for $count ( 1 .. 5 ) {
print "$count\n";
}
In most consoles/terminals standard output (STDOUT) is line buffered.> What I wanted to do was to make each number appear in sequence like you see
> in a countdown (or up, in this case) instead of all on the screen at once.
man 3 stdout
[snip]
The stream stderr is unbuffered. The stream stdout is line-buffered
when
it points to a terminal. Partial lines will not appear until
fflush(3) or
exit(3) is called, or a newline is printed. This can produce
unexpected
results, especially with debugging output.
man 3 setbuf
[snip]
The three types of buffering available are unbuffered,
block buffered, and line buffered. When an output stream
is unbuffered, information appears on the destination file
or terminal as soon as written; when it is block buffered
many characters are saved up and written as a block; when
it is line buffered characters are saved up until a new*
line is output or input is read from any stream attached
to a terminal device (typically stdin).
Since you are printing a newline after $count the value of $count should
appear on your screen as soon as it is available. You can force STDOUT
to automatically flush its buffer by setting the autoflush variable to a
non-zero value before using STDOUT.
$| = 1; # turn on autoflush
for $count ( 1 .. 5 ) {
print "$count\n";
}
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Christiane Nerz #8
Re: Counting (easy!) - select???
quite interesting chunk of code - but what the hell does select does here?
Yeah - I rtfm - but didn't understand it - maybe one could explain it in
more simple words?
Jane
Kevin Old wrote:
..........>>What I wanted to do was to make each number appear in sequence like you see
>>in a countdown (or up, in this case) instead of all on the screen at once.
> Try this code....either of these might be what you're looking for
>
> #!/usr/bin/perl
> #
> use warnings;
> use strict;
>
> print "printing 1thru5 with buffering\n";
> select undef, undef, undef, 0.25 or print $_
> for 1 .. 5;
> print "\nflush forced\n";
>
> $|++;
> print "printing 1thru5 without buffering\n";
> select undef, undef, undef, 0.25 or print $_
> for 1 .. 5;
> print "\nflush forced\n";
>
>
> Hope this helps,
>
Christiane Nerz Guest
-
Dan Muey #9
RE: Counting (easy!)
Or even easier:> In Perl that is usually written as:
>
> for $count ( 1 .. 5 ) {
> print "$count\n";
> }
for(1..5) { print; }
Or if you nee the newline:
for(1..5) { print "$_\n"; }
HTH
DMuey
>Dan Muey Guest
-
Rob Dixon #10
Re: Counting (easy!)
Dan Muey wrote:
If we're competing, then there's>>> >
> > In Perl that is usually written as:
> >
> > for $count ( 1 .. 5 ) {
> > print "$count\n";
> > }
> Or even easier:
> for(1..5) { print; }
> Or if you nee the newline:
> for(1..5) { print "$_\n"; }
print for 1..5
;)
Rob
Rob Dixon Guest
-
Tore Aursand #11
RE: Counting (easy!)
On Thu, 13 Nov 2003 09:05:45 -0600, Dan Muey wrote:
>> In Perl that is usually written as:
>>
>> for $count ( 1 .. 5 ) {
>> print "$count\n";
>> }Or _even_ easier;> Or even easier:
> for(1..5) { print; }
print 1..5;
Hah! :-)
--
Tore Aursand <tore@aursand.no>
Tore Aursand Guest
-
Rob Dixon #12
Re: Counting (easy!)
Tore Aursand wrote:
>
> On Thu, 13 Nov 2003 09:05:45 -0600, Dan Muey wrote:>> >> In Perl that is usually written as:
> >>
> >> for $count ( 1 .. 5 ) {
> >> print "$count\n";
> >> }>> > Or even easier:
> > for(1..5) { print; }
> Or _even_ easier;
>
> print 1..5;
For the subscribers who don't already know,
what are the differences between my
print for 1..5
and Tore's
print 1..5
? (Deep magic here!)
Rob
Rob Dixon Guest
-
John W. Krahn #13
Re: Counting (easy!)
Rob Dixon wrote:
for iterates over the list 1..5 and sets $_ with each value and then>
> Tore Aursand wrote:>> >
> > On Thu, 13 Nov 2003 09:05:45 -0600, Dan Muey wrote:> >> > >> In Perl that is usually written as:
> > >>
> > >> for $count ( 1 .. 5 ) {
> > >> print "$count\n";
> > >> }> >> > > Or even easier:
> > > for(1..5) { print; }
> > Or _even_ easier;
> >
> > print 1..5;
> For the subscribers who don't already know,
> what are the differences between my
>
> print for 1..5
print is called for each item and print out the value in $_.
print is passed the list 1..5 and prints it out.> and Tore's
>
> print 1..5
Not really. :-)> ? (Deep magic here!)
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Steve Grazzini #14
Re: Counting (easy!)
On Thu, Nov 13, 2003 at 12:09:24PM -0800, John W. Krahn wrote:
Not exactly -- it iterates over the *range*, and the range operator> Rob Dixon wrote:>> > For the subscribers who don't already know,
> > what are the differences between my
> >
> > print for 1..5
> for iterates over the list 1..5 and sets $_ with each value and then
> print is called for each item and print out the value in $_.
doesn't need to generate a temporary list here.
print 1 .. $BIGNUM; # creates list of $BIGNUM scalars
print for 1 .. $BIGNUM; # creates/discards one scalar at a time
This is a documented optimization w/r/t foreach() loops, but the same
thing applies to the foreach() modifier.
--
Steve
Steve Grazzini Guest
-
Rob Dixon #15
Re: Counting (easy!)
John W. Krahn wrote:
Hi John.>>> >
> > For the subscribers who don't already know,
> > what are the differences between my
> >
> > print for 1..5
> for iterates over the list 1..5 and sets $_ with each value and then
> print is called for each item and print out the value in $_.
>>> > and Tore's
> >
> > print 1..5
> print is passed the list 1..5 and prints it out.
>>> > ? (Deep magic here!)
> Not really. :-)
I would count you amongst those who already knew, so your
entry doesn't qualify!
And no, it's just Perl magic, not Perl Magic :)
Rob
Rob Dixon Guest
-
John W. Krahn #16
Re: Counting (easy!)
Steve Grazzini wrote:
It depends on which version of Perl you are using. :-) Older Perls>
> On Thu, Nov 13, 2003 at 12:09:24PM -0800, John W. Krahn wrote:>> > Rob Dixon wrote:> >> > > For the subscribers who don't already know,
> > > what are the differences between my
> > >
> > > print for 1..5
> > for iterates over the list 1..5 and sets $_ with each value and then
> > print is called for each item and print out the value in $_.
> Not exactly -- it iterates over the *range*, and the range operator
> doesn't need to generate a temporary list here.
>
> print 1 .. $BIGNUM; # creates list of $BIGNUM scalars
> print for 1 .. $BIGNUM; # creates/discards one scalar at a time
>
> This is a documented optimization w/r/t foreach() loops, but the same
> thing applies to the foreach() modifier.
would generate the list first and then iterate over it so that using a C
style for loop would be more efficient.
When I used the phrase "iterates over the list" I was refering to the
concept, not the actual implementation. :-)
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Rob Dixon #17
Re: Counting (easy!)
Steve Grazzini wrote:
What the code is optimised to is a touch OT, but I've never seen this>
> On Thu, Nov 13, 2003 at 12:09:24PM -0800, John W. Krahn wrote:>> > Rob Dixon wrote:> >> > > For the subscribers who don't already know,
> > > what are the differences between my
> > >
> > > print for 1..5
> > for iterates over the list 1..5 and sets $_ with each value and then
> > print is called for each item and print out the value in $_.
> Not exactly -- it iterates over the *range*, and the range operator
> doesn't need to generate a temporary list here.
>
> print 1 .. $BIGNUM; # creates list of $BIGNUM scalars
> print for 1 .. $BIGNUM; # creates/discards one scalar at a time
>
> This is a documented optimization w/r/t foreach() loops, but the same
> thing applies to the foreach() modifier.
documented Steve. Can you direct me? What do you think is happening
under the hood for something like
print for (0..0, 1, 2..5, func(6)..func(99))
?
Rob
Rob Dixon Guest
-
Jimstone77@aol.com #18
Re: Counting (easy!)
I have a list of email addresses in a text file one to a line. How would I
seach for a particular email address?
$email = "myname@aol.com";
while <FILE> {
if ($email eq $_) {
$OK = 1;
}
}
It seems the @ symbol somehow doesn't work in this search. What would be a
better (or right) way to do this? thanks in advance.
Jimstone77@aol.com Guest
-
Roy Johnson #19
Re: Counting (easy!)
[email]dmuey@infiniplex.com[/email] (Dan Muey) wrote in message news:<D6D77DB239A2004BB08A240E2E71460D12C019@LSLOF FICE.infiniplex.infiniplex.com> Or even easier:
or even> for(1..5) { print; }
print(1..5);
or> Or if you nee the newline:
> for(1..5) { print "$ \n"; }
print "$_\n" for 1..5;
Roy Johnson Guest
-
Roy Johnson #20
Re: Counting (easy!)
[email]dzhuo@looksmart.net[/email] (David) wrote in message news:<20031113014613.26981.qmail@onion.perl.org>.. .
or> like count down from 5 to 1 slowly in a single row? try:
>
> [panda]# perl -e '$|=1; print " @{[6-$_]}\r" and sleep(1) for(1..5)'
perl -e '$|=1; sleep print "$_\r" for (reverse 1..5)'
or, if you're feeling evil:
perl -e 'sleep($|=print "$_\r") for (reverse 1..5)'
Roy Johnson Guest



Reply With Quote

