[coldsync-hackers] Ok, what am I doing wrong???

1 view
Skip to first unread message

Izzy Blacklock

unread,
Jun 10, 2003, 9:01:42 PM6/10/03
to coldsync...@googlegroups.com
I'm trying to pass two arrays to a function like this:

# Run the dump function the user specified.
$DumpFunction{$HEADERS{'Target'}}->(\@NamesArray, \@parsedData );

My function works when I do it this way:

sub textDump
{
my ( $names, $data) = @_;
foreach ( @$data )
{


But I have no need for $names in this target function, so I'd like to do
something like this:

sub textDump
{
shift;
foreach ( @$_ )
{

This doesn't work as I expected. My understanding of shift is that it should
take the first value off the list (in this case \@NamesArray) and return it
(in this case drop it), leaving the rest of the list. Shouldn't that leave
$_ = \@parsedData?

Have I missed/missunderstood something?

...Izzy

PS: I hope no one minds me asking for perl help on the coldsync list. I
could/should sign up for a perlmonk account.
--
This message was sent through the coldsync-hackers mailing list. To remove
yourself from this mailing list, send a message to majo...@thedotin.net
with the words "unsubscribe coldsync-hackers" in the message body. For more
information on Coldsync, send mail to coldsync-ha...@thedotin.net.

Andrew Arensburger

unread,
Jun 12, 2003, 12:05:03 PM6/12/03
to coldsync...@googlegroups.com
On Tue, 10 Jun 2003, Izzy Blacklock wrote:
> I'm trying to pass two arrays to a function like this:
>
> # Run the dump function the user specified.
> $DumpFunction{$HEADERS{'Target'}}->(\@NamesArray, \@parsedData );
>
> My function works when I do it this way:
>
> sub textDump
> {
> my ( $names, $data) = @_;
> foreach ( @$data )
> {
>
>
> But I have no need for $names in this target function, so I'd like to do
> something like this:
>
> sub textDump
> {
> shift;
> foreach ( @$_ )
> {
>
> This doesn't work as I expected. My understanding of shift is that it should
> take the first value off the list (in this case \@NamesArray) and return it
> (in this case drop it), leaving the rest of the list. Shouldn't that leave
> $_ = \@parsedData?

No. Within a function, 'shift' modifies @_, not $_. Those are two
separate variables. @_ is an array, and $_ is a scalar. Yes, Perl allows
you to have the same name for a scalar, an array, a hash, ...
What you want is not $_, but the first element of @_, so use

foreach (@{$_[0]})

Just in passing: as a matter of style, I prefer to declare 'my'
variables at the top of a function, rather than using @_ directly. So I'd
write &textDump as:

sub textDump
{
my $names = shift; # Ref-to-array of names. Ignored
my $data = shift; # Ref-to-array of data.
my $foo; # Some other variable used later

foreach my $item (@{$data})
{
...

I find that a block of "= shift"s helps to clarify what arguments the
function expects. YMMV. HTH.

--
Andrew Arensburger Actually, these _do_ represent the
are...@ooblick.com opinions of ooblick.com!
Generic Tagline V 6.01

Izzy Blacklock

unread,
Jun 12, 2003, 2:58:47 PM6/12/03
to coldsync...@googlegroups.com
On June 12, 2003 10:05 am, Andrew Arensburger wrote:
> On Tue, 10 Jun 2003, Izzy Blacklock wrote:
> > I'm trying to pass two arrays to a function like this:
> >
> > # Run the dump function the user specified.
> > $DumpFunction{$HEADERS{'Target'}}->(\@NamesArray, \@parsedData );
> >
> > My function works when I do it this way:
> >
> > sub textDump
> > {
> > my ( $names, $data) = @_;
> > foreach ( @$data )
> > {
> >
> >
> > But I have no need for $names in this target function, so I'd like to do
> > something like this:
> >
> > sub textDump
> > {
> > shift;
> > foreach ( @$_ )
> > {
> >
> > This doesn't work as I expected. My understanding of shift is that it
> > should take the first value off the list (in this case \@NamesArray) and
> > return it (in this case drop it), leaving the rest of the list.
> > Shouldn't that leave $_ = \@parsedData?
>
> No. Within a function, 'shift' modifies @_, not $_. Those are two
> separate variables. @_ is an array, and $_ is a scalar. Yes, Perl allows
> you to have the same name for a scalar, an array, a hash, ...
> What you want is not $_, but the first element of @_, so use
>
> foreach (@{$_[0]})

Oh, I understand now (I think). So to do what I was trying to do above, I
should be able to so this:

sub textDump
{
shift; # Drop Ref-to-arry of names; not needed
my $data = shift;
foreach ( @$data )
{

> Just in passing: as a matter of style, I prefer to declare 'my'
> variables at the top of a function, rather than using @_ directly. So I'd
> write &textDump as:
>
> sub textDump
> {
> my $names = shift; # Ref-to-array of names. Ignored
> my $data = shift; # Ref-to-array of data.
> my $foo; # Some other variable used later
>
> foreach my $item (@{$data})
> {
> ...
>
> I find that a block of "= shift"s helps to clarify what arguments the
> function expects. YMMV. HTH.

I like the my ( $names, $data) = @_; approach myself. Probably because it
reminds me of the way you define parameter names in C functions. :)

...Izzy

Reply all
Reply to author
Forward
0 new messages