Forum Moderators: coopster & phranque

Message Too Old, No Replies

how to split a parameter into variables

         

kelly_boyce

3:12 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



hello everyone, i have a script that receives a parameter called date like this: script.pl?date=02-08-2003. i want to split the value of the parameter where the hyphens are. I want to make 02 a variable called $day 08 into $month and 2003 into $year. that way i can print the result throughout my script in whatever format.

thanx in advance

jatar_k

4:46 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld kelly_boyce,

what about using split [perldoc.com]?

[edited by: jatar_k at 5:11 pm (utc) on Aug. 2, 2003]

kelly_boyce

5:03 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



i dont know exactly how to use split

jatar_k

5:13 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



given that perl is not my forte but I believe the syntax is very similar to php

there is this example on that page
($login, $passwd, $remainder) = split(/:/, $_, 3);

so it may work for you to use

($day,$month,$year) = $split(/-/,$date);

though I am not 100% sure

kelly_boyce

5:16 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



thanx, i will try and see if it works

moltar

5:17 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



my ($day, $month, $year) = split('-', $var, 3);

jatar_k

5:22 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



thx moltar ;)

ShawnR

10:02 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jatar's syntax should work just as well ;) Perl allows you to delimit your regular expression with whatever you like, pretty much, so /-/ will work just as well as '-' or {-} or (-) or #-# etc. The third parameter sets the limit and is optional

Shawn

moltar

10:19 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ya but perl doesn't allow $split() and limiting it to just 3 parts is more efficient.

<edit>wrong statement</edit>

ShawnR

11:54 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops. You're right. I didn't notice the extra $