SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding items to a hash?

    I am trying to iterate through a hash and then add items back into a hash with the word DONE in it. How can I get this to work?


    Code Perl:
    my $getdirs = "1254-9851";
    my @dirN = split('-', $getdirs);
    my @test = ();
    foreach my $adir (@dirN)
    {
    	@test = ($adir.'DONE');
    }
     
    print @test;

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2008
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hash in perl is defined with %
    @ is for array

  3. #3
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to use push():

    Code:
    my $getdirs = "1254-9851";
    my @dirN = split('-', $getdirs);
    my @test = ();
    foreach my $adir (@dirN)
    {
       push @test , "${adir}DONE";
    }
     
    print "$_n" for @test;
    Otherwise all your code will do is redefine the entire array within the loop instead of adding to the array.

    "${adir}DONE" (including the double-quotes) does the same thing as $adir.'DONE' just more efficiently.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •