SHIFT not shuffling ?

S

stu7

+ Perl's SHIFT function seems to say that it both
reads, and removes, the first element of whatever array
is being used with it... shift or unshift for the first
or last array element is a handy idea, although I'm not
too sure why a separate function is needed for this...
...thats not my problem though :)

I tried using shift to erase the first element of an
array repeatedly, until it was empty... nice for a counting
program maybe...

$a = shift(@oneTOfive) ;
print $a ;

...here is where I got stuck... how do you print the first
shifted element, and then get to the next ? I actually tried
"next", which perl didnt accept.

I then tried a GOTO loop... and I know this function has
many fans in the perl community :)... odd, with a goto, the
first element seemed to reprint, like it never got erased ?
Also, I tried my first big plate of perl spaghetti :)

So finally... after copying the above two lines for each
element, I was able to erase every element in the array, but
isn't there a simple [next type of thing] to use with SHIFT...
or is it just one of those simple functions that were never
intended to do fancy work ?
 
A

A. Sinan Unur

(e-mail address removed) (stu7) wrote in @posting.google.com:
+ Perl's SHIFT function seems to say that it both
reads, and removes, the first element of whatever array
is being used with it... shift or unshift for the first
or last array element is a handy idea, although I'm not
too sure why a separate function is needed for this...
...thats not my problem though :)

perldoc -f shift explains what shift does very clearly, IMHO.
I tried using shift to erase the first element of an
array repeatedly, until it was empty... nice for a counting
program maybe...

$a = shift(@oneTOfive) ;
print $a ;

...here is where I got stuck... how do you print the first
shifted element, and then get to the next ? I actually tried
"next", which perl didnt accept.

the while statement comes to mind.
I then tried a GOTO loop... and I know this function has
many fans in the perl community :)... odd, with a goto, the
first element seemed to reprint, like it never got erased ?
Also, I tried my first big plate of perl spaghetti :)

I am not sure what a GOTO loop is and you do not show what you did so it
is hard to figure out how to fix it.
So finally... after copying the above two lines for each
element, I was able to erase every element in the array, but
isn't there a simple [next type of thing] to use with SHIFT...
or is it just one of those simple functions that were never
intended to do fancy work ?

Here's my newbie way of doing it:

#! /usr/local/bin/perl

use strict;
use warnings;

use diagnostics;

my @a = qw(one two three four five six);

while(my $a = shift @a) {
print "$a\n" ;
}

You might want to read about looping and conditionals.
 
J

John W. Krahn

stu7 said:
+ Perl's SHIFT function seems to say that it both
reads, and removes, the first element of whatever array
is being used with it... shift or unshift for the first
or last array element is a handy idea,

shift() for the first element and pop() for the last.
although I'm not
too sure why a separate function is needed for this...

shift() removes an element and unshift() adds an element.
...thats not my problem though :)

I tried using shift to erase the first element of an
array repeatedly, until it was empty... nice for a counting
program maybe...

$a = shift(@oneTOfive) ;
print $a ;

...here is where I got stuck... how do you print the first
shifted element, and then get to the next ? I actually tried
"next", which perl didnt accept.

Try removing the elements to a list and then printing from the list.

for my $a ( splice @oneTOfive ) {
print $a;
}


John
 
S

stu7

*** ah yes, thank you martien for clearing this problem up for
*** me... unfortunately, I am out of practice, and my use of
*** "while", and other loop functions is limited to simpler examples.
*** All of your suggestions look good, and are much appreciated.


Martien Verbruggen said:
+ Perl's SHIFT function seems to say that it both
reads, and removes, the first element of whatever array
is being used with it... shift or unshift for the first
or last array element is a handy idea,

You misunderstand. shift() removes the first element, and unshift()
puts a new element at the beginning of an array. The functions pop()
and push() do the same at the end of the array.
although I'm not
too sure why a separate function is needed for this...
...thats not my problem though :)

I'm not sure what you're saying here. Perl has many functions that are
not strictly necessary. shift(), unshift(), pop() and push() can all
be removed, which would mean everyone would just use splice().
However, the four former functions make code just so much more
readable than having splice() everywhere with the larger number of
arguments.

pop could be implemented with two reverse() calls and a shift(), but
do you really want to code that way?

Or are you asking why it should be possible to remove elements from an
array from both ends? Many data structures need that sort of thing. A
FIFO (first in, first out) queue can be implemented with an array and
a combination of unshift/pop or push/shift for adding elements to the
queue and removing them. Sure, it can be implemented in many other
ways that don't require these functions, but having them makes the
code easier to write, understand and maintain. And it prevents you
from having to write them yourself all the time.

Have you ever programmed in more "sparse" languages, like C?
I tried using shift to erase the first element of an
array repeatedly, until it was empty... nice for a counting
program maybe...

$a = shift(@oneTOfive) ;
print $a ;

...here is where I got stuck... how do you print the first
shifted element, and then get to the next ? I actually tried
"next", which perl didnt accept.

while (@oneTOfive)
{
my $element = shift @oneTOfive;
print $element;
}

If you want to preserve the array:

for my $element (@oneTOfive)
{
print $element;
}

Or shorter:

print for @oneTOfive;
I then tried a GOTO loop... and I know this function has
many fans in the perl community :)... odd, with a goto, the
first element seemed to reprint, like it never got erased ?
Also, I tried my first big plate of perl spaghetti :)

Why a goto when there are so many other loop constructions available?
I wouldn't go as far as saying that goto has no place in any
language, Perl specifically, I would say that its use should be
limited as much as possible [1].
So finally... after copying the above two lines for each
element, I was able to erase every element in the array, but
isn't there a simple [next type of thing] to use with SHIFT...
or is it just one of those simple functions that were never
intended to do fancy work ?

This really has little to do with shift(). It has to do with your
apparent lack of knowledge of the normal looping constructs. Have a
look at the perlsyn documentation for a thorough discussion of them.
And while all those looping constructions can be rewritten using goto,
I would simply not go there.

Martien

[1] I am only talking about goto LABEL and goto EXPR. goto &SUB is a
different sort of beast, called by the same keyword.
 
S

Sam Holden

Is there some subtle reason for the "my" in the while loop?

To limit the scope of the variable.

Not that I'd use $a or $b (even my'd versions of them) in anything
but a sort comparison function, but that's a different matter...
 
A

A. Sinan Unur

(e-mail address removed) (Sam Holden) wrote in
To limit the scope of the variable.

Not that I'd use $a or $b (even my'd versions of them) in anything
but a sort comparison function, but that's a different matter...

You are right. I used the names the OP was using without thinking about it
at all. Thanks for pointing that out.

Sinan.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top