Populate array with split problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kelicula
    Recognized Expert New Member
    • Jul 2007
    • 176

    #1

    Populate array with split problem.

    Hello all. I have a problem which seems to make no sense to me.
    Therefore I must be doing something wrong.

    I am trying to populate an array using split with a regexe.
    Here is the code (snippet only).
    [code=perl]
    my $date = $q->param('date' ); # 20090112 format
    @dateArray = split(/(\d{4})(\d{2})( \d{2})/, $date);
    [/code]

    After this I loop through the array using print and all the values show up.
    BUT if I try to access a particular indice nothing shows up. ???

    example:
    [code=perl]

    # After above code
    for(@dateArray) {
    print $_, "\n";
    }

    # This works printing 2009 \n 01 \n 12 \n
    # BUT....

    print $dateArray[0]; # OR...
    print "$dateArray[0]";

    # Prints nothing to the screen.
    [/code]

    What am I doing wrong?

    I really need the three individual values, NOT the whole array.
    Is there a better what to accomplish this without split?

    I just need to pass a number, and be able to use the parts separately.

    thanks in advance!
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Try this:

    Code:
    for my $i (0..$#dateArray) {
       print "index $i = <$dateArray[$i]>\n";
    }
    See what gets printed. The <> brackets are so you can see if there are empty elements in the array. Keep in mind you have used capturing parentheses in the regexp so the split delimiter is also being saved in the array.

    Comment

    • Kelicula
      Recognized Expert New Member
      • Jul 2007
      • 176

      #3
      Very interesting.
      Here is the results from your code KevinADC.
      Code:
      index 0 = <>
      index 1 = <2009>
      index 2 = <01>
      index 3 = <12>
      My only guess is the capturing vars, $1, $2, etc..were used as indices?

      Thanks!

      PS- I almost didn't notice your new avatar. Nice.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        No, the capturing vars are not used as the array indexes. I think the problem is you are using split() when you should not be. Just use a matching regexp:

        Code:
        @dateArray = $date =~ m/(\d{4})(\d{2})(\d{2})/;
        if there is more than one set of dates to capture add the "g" modifier to the end of the regexp,

        Comment

        • Kelicula
          Recognized Expert New Member
          • Jul 2007
          • 176

          #5
          Ah ha! That's it.
          That works just as I expect it to.

          In fact that is exactly what I was trying to do, I had seen it before in a book and remembered it incorrectly.

          Thanks.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            You're welcome, good to see you around here.

            Comment

            Working...