#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0

    Trying to split('-', @_, 3) yyyy-mm-dd


    Hi
    I am trying to do something really simple. I am trying to split a string into components. Below is the code within a subroutine.
    The code is intended to take a date string in the format
    yyyy-mm-dd
    and split it into the parts.
    Below is the debugger output for the code.
    The first line is what I think should work.
    I also tried a variation with the same results.
    I have also tried other varients not shown.

    The result is the same. I just get a "1" and not a split string.


    Code:
      DB<1>
    main::GetWeighting(daily_budget.pl:125):
    125:      my ($yr,$mth,$dy) = split('-',@_ ,3); # split the components of the input
      DB<1>
    main::GetWeighting(daily_budget.pl:127):
    127:      my @z  = split('-',@_ ,3); # split the components of the input
      DB<1> p @_
    2013-04-16
      DB<2> p $yr
    1
      DB<3> p $mth
    
      DB<4> p $dy
    
      DB<5> n
    main::GetWeighting(daily_budget.pl:128):
    128:      ($yr,$mth,$dy) = @z;
      DB<5> p @z
    1
      D
    B<6>
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    @_ is an array that contains one element: the string '2013-04-16'. However, you're using it as a scalar. When an array is used like that (in scalar context), it returns the number of elements (i.e. 1), meaning that it's the string '1' that you're trying to split (hency why $yr has the value 1 afterwards).

    To refer to the first element in that array, use @_[0] instead, i.e.:
    Code:
    my ( $d, $m, $y ) = split( '-', $_[0] );

    Comments on this post

    • Laurent_R agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Hi

    That worked, thanks.

IMN logo majestic logo threadwatch logo seochat tools logo