Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Array question

by peleus (Initiate)
on Jun 14, 2008 at 02:37 UTC ( [id://692045]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I am complete noob still learning Perl(and will attempt my best to trancend my noobness). However this should be a simple matter; how would I define the elements in an array variable if the number of elements are user defined? Basically I hope to use an array variable to create a number of text files that the user specifies.

Any help is always greatly appreciated
Thanks!

Update Thank you all, it makes so much sense. I was not sure if it made a lot of sense, but thank you all for responding, it works like a charm.

20080617 Janitored by Corion: Restored original text

Replies are listed 'Best First'.
Re: Array question
by runrig (Abbot) on Jun 14, 2008 at 03:28 UTC
    You don't need to define the number of elements, just create the array (empty or with the elements you want) and/or push items into it until you are done.
    my $file1 = "foo.txt"; my $file2 = "bar.txt"; my @array1 = ($file1, $file2); # or my @array2; print "Enter a file: "; while (<>) { chomp; push @array2, $_; print "Enter a file: "; }
    Does that help?
Re: Array question
by sgifford (Prior) on Jun 14, 2008 at 03:30 UTC
    You can add items to the end of an array with push or to the beginning with unshift. You can also add elements anywhere in the array by just storing something at the new index.

    In some languages, like C and Java, you have to define the size of the array when it is created. But Perl arrays will simply grow as needed.

      This is either "picky" or "iggerant" but the statement (above) that...

      "You can also add elements anywhere in the array by just storing something at the new index."

      ... seems to me to be, at once, true, and "possibly confusing."

      In short, I don't know any function that will add an element to an array at any arbitrary index nor any technique (aside from the one below) for doing so.

      #!usr/perl/bin use strict; use warnings; use Data::Dumper; my $file0 = "foo.txt"; my $file1 = "bar.txt"; my $file2 = "blivitz.txt"; my @array1 = ($file0, $file1, $file2); print "Initial array:\n"; my $elementindex = 0; for my $file(@array1) { print "\$elementindex $elementindex is $file\n"; $elementindex++; } print "\n"; # Now adding an interior element to the array, in this case, arbitrari +ly inserting "new element" # after "foo.txt" and before "bar.txt" #(to add to beginning or end, see push and unshift) my @temp_array = @array1; # copy all elements to new array @temp_array = ($array1[0], "new element", $array1[1], $array1[2]); @array1 = @temp_array; # copy revisions back to @array1 $elementindex = 0; for my $file(@array1) { print "After revisions, \$elementindex $elementindex is $file\n"; $elementindex++; } print "\n"; # This REPLACES the latest content of $array1[1], "new element", with +$addelement.txt # BUT note it's NOT "add"(ing) an element "anywhere" my $addelement = "addelement.txt"; $array1[1] = $addelement; # $elementindex = 0; for my $file(@array1) { print "now \$elementindex $elementindex is $file\n"; $elementindex++; }

      Output:

      $elementindex 0 is foo.txt $elementindex 1 is bar.txt $elementindex 2 is blivitz.txt After revisions, $elementindex 0 is foo.txt After revisions, $elementindex 1 is new element After revisions, $elementindex 2 is bar.txt After revisions, $elementindex 3 is blivitz.txt now $elementindex 0 is foo.txt now $elementindex 1 is addelement.txt now $elementindex 2 is bar.txt now $elementindex 3 is blivitz.txt

      Perhaps splice() ( perldoc -f splice ) is also an option but thus far /me does not see how to implement it.

        Perhaps I was less than careful with my use of the word "add". I simply meant you can do this:
        my @arr; $arr[78] = "foo";

        But it is actually pretty straightforward to insert elements into an array, shifting all later elements, using splice:

        my @a = ('foo','baz'); splice(@a,1,0,'bar'); print "@a\n";

Log In?
Username:
Password:

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

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

    No recent polls found