Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I reset an array?

by Mission (Hermit)
on Apr 10, 2001 at 22:58 UTC ( [id://71446]=perlquestion: print w/replies, xml ) Need Help??

Mission has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a text file, where I'm substituting characters on the fly. For example I'm using an array with a split and replacing spaces of my string for '%2B'. At the end of each line (for loop) I want to reset the array so it will start over. It's doing much more than that, but I hope you understand my goal. Can someone please point out my mistake (or the obvious)?

Appreciatively,

- Mission
"Heck I don't know how to do it either, but do you think that's going to stop me?!!"

Replies are listed 'Best First'.
Re: How do I reset an array?
by Chady (Priest) on Apr 10, 2001 at 23:13 UTC

    you don't actually need to reset the array... if you are using split it will do it all by itself:

    @array = split(/-/); # will create the new array @array # later on... @array = split(/-/); # will empty the array and create a new array @ar +ray with the new values

    don't write code that doesn't do anything...

    Update: did you say "substituting spaces..." and "using an array and split" ??? are you sure you're using the right(s///) command ???


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
      Chady, I like your signature file... I feel like the five minute fool for asking such a basic question. To answer your question, yes I am using the s/// command. The file I'm trying to write has some bizarre formatting for another application. It has it's own special line counting and EOL characters, so I was splitting to rebuild the character count per line and line count per story for multiple stories. I apologize for being too brief in my initial question, and thanks for the array help... I'm very new to all of this (as you can tell.)

      - Mission

      "Heck I don't know how to do it either, but do you think that's going to stop me?!!"

Re: How do I reset an array?
by indigo (Scribe) on Apr 10, 2001 at 23:06 UTC
    Depends what you mean by reset. To clear it:
    @array = ();
    To restore to its old value:
    @save = @array; for (@array) { # Loop stuff } @array = @save;
Re: How do I reset an array?
by little (Curate) on Apr 10, 2001 at 23:27 UTC
    You might work on a splice of your array like @arry[1..@#arry] in your for loop
    but as I wonder why you are doing all lines again and again - did you consider to use Math::Matrix ? {grin}

    Have a nice day
    All decision is left to your taste

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-18 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found