Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Corrupt Data?

by friedo (Prior)
on Jul 14, 2008 at 19:41 UTC ( [id://697547]=note: print w/replies, xml ) Need Help??


in reply to Corrupt Data?

The reason you're getting garbage at the end of your loop is because you're pushing stuff onto @array, then shifting stuff off the front, but looping over @array sequentially. You can't alter the thing you're looping over and expect sane results.

BTW, you don't need to use a C-style for loop to iterate over an array, it's much easier to write it this way:

my @results; foreach my $addr( @array ) { my $element = lc $addr; # mess with $element here push @results, $element; } return @results;
You also don't need to reset @hold each time through the loop if you declare it with my, since it will be lexically scoped to the enclosing block. You'll catch a number of other problems if you turn on strict and warnings; put this at the top of your script:
use strict; use warnings;
and fix stuff until it stops yelling at you. Finally, the print @array statement at the end of your sub will never get executed because you return on the previous line!

I think that should be enough to get you well on your way. Good luck!

Replies are listed 'Best First'.
Re^2: Corrupt Data?
by ikegami (Patriarch) on Jul 14, 2008 at 20:32 UTC

    You can't alter the thing you're looping over and expect sane results.

    While that's true, it's not the case here. He's not looping over the array.

      But he is accessing it positionally (with the for loop) while pushing onto it and shifting it at the same time. That means he's going to end up running his transformation on output data, plus throwing away data that should have been transformed, etc.
        Yes, but the problem is that how he's accessing the array. If he had been using $array[0] instead of $array[$j], he would have been fine (aside other issues such as variable reuse).
Re^2: Corrupt Data?
by spickles (Scribe) on Jul 14, 2008 at 19:46 UTC
    Thanks for your posts, guys! Almut - I thought that the variable @_ would take whatever was passed to it, even for a subroutine without any explicit arguments? Therefore, I should be able to pass either a scalar or an array, without having to explicitly define arguments. friedo - Thanks for your help in finding out how I was corrupting my own data. Regards, Scott
      I thought that the variable @_ would take whatever was passed to it, even for a subroutine without any explicit arguments?

      Thing is that if you declare sub convert () {...} you're telling Perl that the routine takes no arguments... In other words, if you're then going to call the routine with any arguments, you'll just get the error Too many arguments for main::convert ...

      Without any prototype, however, Perl already does exactly what you want, i.e. you can pass one or several arguments...

      even for a subroutine without any explicit arguments

      I don't know where you got the term explicit or what you want it means in this context, but what almut meant was that the "()" in "sub foo () { ... }" means the subroutine takes no arguements. Drop the "()": "sub foo { ... }".

      Update: Too slow and credited the wrong monk. Fixed the latter.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://697547]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-18 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found