Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

incrementing array name

by Lhamo_rin (Friar)
on Jun 23, 2003 at 11:55 UTC ( [id://268121]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

I'm trying to increment the name of an array so that each time through my sub routine the data I'm saving we'll be saved to a different array. Unfortunately I have not had success. Can you please offer some advice to a novice?

sub separate_data { @a = split / /, $data[$a]; }
Each time through I would like to save to a new array.

Llamo_rin

update (broquaint): added formatting + <code> tags

Replies are listed 'Best First'.
Re: incrementing array name
by Skeeve (Parson) on Jun 23, 2003 at 12:02 UTC
    sub seperate_data {
      push(@{$arrays}, split / /,$data[$a]);
    push(@{$arrays}, [split(/ /,$data[$a])]); }
    This will give you an array of arrays. $array[0][0] being the first element of your first array and $array[1][0] being the first element of your second array...

    Update: How could I miss the brackets? Thanks gjb for showing it.

    Update 2 (see below):$array->[0][0] being the first element of your first array and $array->[1][0] being the first element of your second array...

      Close, but no cookie ;-) I think you want something like this:

      sub seperate_data { push(@{$arrays}, [split(/ /,$data[$a])]); }
      since otherwise you'll end up with a long list rather than a list of lists.

      Hope this helps, -gjb-

        When I attempt to: print "$arrays[0][0]"; I'm getting an error that I have an uninitialized value in that line. Any suggestions? Llamo_rin
        No cookie too ;-) because you missed my second mistake
        .oO( I should never post untested code. At least I'm not Micro$chrott )
        which is: $array->[0][0] will give the first element of the first array...
Re: incrementing array name
by particle (Vicar) on Jun 23, 2003 at 12:50 UTC

    since you're accessing a global variable in a subroutine, @a will be appended to each time. you need to create a lexical variable, and return it from the subroutine. something like:

    sub separate_data { my( @important_data )= @_; my @important_results= split / /, @important_data; return @important_results; } ## call it like my @answer= separate_data( $data[$a] ); ## or in a loop as: for my $a ( 0..10 ) { my @answer= separate_data( $data[$a] ); }

    ~Particle *accelerates*

Re: incrementing array name
by Zaxo (Archbishop) on Jun 23, 2003 at 14:02 UTC

    This is a terribly dirty thing to do, combining symbolic references and global variables, but I'm lowdown enough to show you how:

    { my $name = 'a'; sub separate_data { @{$name++} = split / /, $_[0]; } }

    You'll need to do that under no strict qw(vars refs); . Can you think of a better design for what you're doing? Hint: hash.

    After Compline,
    Zaxo

Re: incrementing array name
by hardburn (Abbot) on Jun 23, 2003 at 14:00 UTC

    A twist on particle's solution. Instead of saving to an array, you pass in a referance to that array:

    sub separate_data { my $array = shift; $array = [ split / /, $data[$a] ]; } my (@array1, @array2, @array3); seperate_data(\@array1); seperate_data(\@array2); seperate_data(\@array3);

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

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

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

    No recent polls found