Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

How do I use Split

by fastkeys (Novice)
on Feb 14, 2001 at 04:52 UTC ( #58268=perlquestion: print w/replies, xml ) Need Help??
fastkeys has asked for the wisdom of the Perl Monks concerning the following question:


OK, It's nearly midnight and this is really annoying me. I want to use split to change American date format to British

ie. yyyy-mm-dd to dd-mm-yyyy

code goes

sub changedate { my $year=0; my $month=0; my $day=0; ($year, $month, $day) = split(/-/, $_, 3); return $day."-".$month."-".$year; }

Problem is all I get back from it is --

I've tried escaping the - in the pattern match but to no avail. What have I missed?



Replies are listed 'Best First'.
Re: Split
by btrott (Parson) on Feb 14, 2001 at 04:55 UTC
    The arg to your sub isn't in $_, it's in @_. To be specific it's the first element in @_:
    ($year, $month, $day) = split(/-/, $_[0], 3);
      Works now. Thanks for your help :)

Re: Split
by dws (Chancellor) on Feb 14, 2001 at 06:00 UTC
    Or, without split:
    sub changedate { my $date = shift; $date =~ s/^(\d{4})-(\d{2})-(\d{2})$/$3-$2-$1/; return $date; }
Re: Split - Not using arguments
by lemming (Priest) on Feb 14, 2001 at 04:54 UTC
    You missed reading in the argument for your subroutine. hint: @_
      Just tried that, now get "-- 1".
      sub changedate { my $year=0; my $month=0; my $day=0; ($year, $month, $day) = split(/-/, @_, 3); return $day."-".$month."-".$year; }

      It's something but it still aint right :)

      Thanks, Alex
        Just to clarify something here, you got the correct answer for what you asked perl to do. split() expects a scalar, which caused @_ to be evaluated in scalar context. In this case that was 1, since you where sending but one arguement to the subroutine. split() applied your regex and returned (1,undef,undef).

        I might suggest you read up on -w and use strict; as well - it would have at least flagged a warning on that line. It will also save you a lot of grief sooner or later.


Re: Split
by MeowChow (Vicar) on Feb 14, 2001 at 17:21 UTC
    Here's a delimiter-neutral solution:
    sub changedate { join '', reverse split /\b/, shift; }
    This has the nice bonus of turning "2000/2/13" and "2000-2-13" and "2000 2 13" into their corresponding eurodates, keeping the originally used delimiters intact.
                   s aamecha.s a..a\u$&owag.print
(dkubb) Re: (2) Split
by dkubb (Deacon) on Feb 14, 2001 at 11:18 UTC

    Converting yyyy-mm-dd to dd-mm-yyyy format, also using a regex:

    sub changedate { join '-', reverse shift =~ /\d+/g }
      I ran this and it works, but I am not sure I understand why.
      1. 'd+' is one or more digits. The 'g' option causes the regex to find all greedy matches of d+, which is, say, '2001', '02' and '14'.
      2. The regex returns a list of these finds.
      3. 'shift =~' causes the regex to read as input the first parameter sent to the changedate function.
      4. join and reverse are as usual.
      5. Finally, even though there is no explicit return in the sub, it by default returns the last value calculated, which is the the value returned by join.
      Wow! Is Perl powerful or what? I am in awe of Larry and the Perl 5 Porters who wrote it.
        I promise this will be the last "clarification" I make in this thread.

        You are close. You are very close. I would make these notes

        1. The 'g' option causes a global match - ie, find them all
        2. The regex, in this case, returns the list of all matches. In other cases, it will return how many matches were found. The difference is the context in which it is used. Since reverse() expects a list, it forces the regex to be evaluated in list context and, hence, returns the list.
        In a clarifying mood,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://58268]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2017-05-25 02:47 GMT
Find Nodes?
    Voting Booth?