Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. need help with arrays in Perl

need help with arrays in Perl

Scheduled Pinned Locked Moved Web Development
perldatabasedata-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lnong
    wrote on last edited by
    #1

    I want to create an array of arrays of strings. Here's what I tried doing: my (@arrayOfArrays, @array); This is inside a FOR Loop { @array = (@array, "myString"); @arrayOfArrays = (@arrayOfArrays, \@array); } That should create an array of array references, where each reference points to an array of increasing size (i.e. one more element than the previous array). Now, what if I want to add a string value directly to one of the subarrays, say the 5th array (index=4). How would I do it? Is this how? $arrayOfArrays->[4] = ($arrayOfArrays->[4], "2ndString"); Probably not, because it's not working. Please help with this. Thanks.

    Z 2 Replies Last reply
    0
    • L lnong

      I want to create an array of arrays of strings. Here's what I tried doing: my (@arrayOfArrays, @array); This is inside a FOR Loop { @array = (@array, "myString"); @arrayOfArrays = (@arrayOfArrays, \@array); } That should create an array of array references, where each reference points to an array of increasing size (i.e. one more element than the previous array). Now, what if I want to add a string value directly to one of the subarrays, say the 5th array (index=4). How would I do it? Is this how? $arrayOfArrays->[4] = ($arrayOfArrays->[4], "2ndString"); Probably not, because it's not working. Please help with this. Thanks.

      Z Offline
      Z Offline
      ZoogieZork
      wrote on last edited by
      #2

      (Assuming that @arrayOfArrays is still in the scope): $arrayOfArrays[4] = [ @{$arrayOfArrays[4]}, "2ndString" ]; Basically, @{$arrayOfArrays[4]} dereferences the arrayref, then the [] creates a new arrayref using the contents of @{$arrayOfArrays[4]} and "2ndString". - Mike

      1 Reply Last reply
      0
      • L lnong

        I want to create an array of arrays of strings. Here's what I tried doing: my (@arrayOfArrays, @array); This is inside a FOR Loop { @array = (@array, "myString"); @arrayOfArrays = (@arrayOfArrays, \@array); } That should create an array of array references, where each reference points to an array of increasing size (i.e. one more element than the previous array). Now, what if I want to add a string value directly to one of the subarrays, say the 5th array (index=4). How would I do it? Is this how? $arrayOfArrays->[4] = ($arrayOfArrays->[4], "2ndString"); Probably not, because it's not working. Please help with this. Thanks.

        Z Offline
        Z Offline
        ZoogieZork
        wrote on last edited by
        #3

        Oh yeah, if you want to modify the array directly instead of creating a new array: push(@{$arrayOfArrays[4]},"2ndString"); - Mike

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups