barrycarlyon has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Pilgrims on the Perl Monks Monastary,

A quick question regarding a split

Given:

my ($session_id, $last_action_date, $last_action_time) = $session =~ / +^(\d\d)-(\d{10})-(\d{8})$/;

Do i Have to do anything extra to get $session_id on is own, as I cant get it to display on the page, using a standard process

My belief is that the my (xxx) is an array, if so how do I split that up?

UPDATE: Sorry guys your right i copied from the other monitor wrong

Basically, i have a session which is for example 01-2006-05-19-13:00:00

so the first two digits are the session id

2006-05-19 (10 digits) is the session date

13:00:00 (8 digits) is the session time,

As the session is initially generated by Class::Date, hence - and :

So any help would be useful as I originally got the above code line from an earlier node that I authored, and the above code line was a reply

update 2: yes iut does have {} its just a fuzzy monitor, its on a laptop i just plugged it into a fuzzy monitor, and by the way I do have glasses

Update 3: Since this is being difficult, coz Im writing my own session management software, how about a way to remove characters from a $session say remove / and the :, suggestions

Code Source

Yours
Bazza

Barry Carlyon barry@barrycarlyon.co.uk

2006-06-21 Retitled by holli, as per Monastery guidelines
Original title: 'A Question on assigning varibles from a big variable'

Replies are listed 'Best First'.
Re: A Question on assigning variables from a big variable
by Corion (Patriarch) on Jun 20, 2006 at 12:24 UTC

    I don't think the code does what you think it's doing:

    my ($session_id, $last_action_date, $last_action_time) = $session =- / ^(\d\d)-(\d(10))-(\d(8))$/;

    If you copy'n'pasted the code, then the operators in bold tags, =-, won't do a regular expression match, and depending on what's stored in $_, you should get an Use of uninitialized value ... warning.

    If you transferred the code by hand and made a typo just when typing it in, I think we'll need to get a more accurate error description of what goes wrong and how. I find it curious that your session IDs can only have two digits, and your action times have eight digits (HHMMSS), while your dates have 10 digits (YYYYMMDD) - something is very fishy in your format description.

    Please take the time to post some relevant-yet-anonymized data and what you're actually trying to do with it. As your question title mentions splitting, maybe split is what you actually want to do:

    my ($session_id, $last_action_date, $last_action_time) = split /-/, $s +ession;

    Update: liverpole has even better eyes than I have - you need curly brackets for counting repetitions - what you're typing your code in from, it has a very bad font or you really need glasses.

    Update2: Neither - nor : are what Perl considers a digit when matching against \d. The only chars allowable as digits are the following ten chars:

    0 1 2 3 4 5 6 7 8 9

    I would still use split to get at the separate fields, or use unpack or a more specific regular expression:

    # split: my @fields = split /-/, $session; die "Weird timestamp in >>$session<<" unless @fields == 5; my ($session_id) = $fields[0]; my ($last_action_date) = join "-", @fields[1,2,3]; my ($last_action_time) = $fields[4]; # unpack: my ($session_id,$last_action_date,$last_action_time) = unpack "A2xA10x +A8", $session; # regex: $session =~ /^(\d{2})-(\d{4}-\d{2}-\d{2})-(\d\d:\d\d:\d\d)$/ or die "Weird timestamp in >>$session<<" my ($session_id,$last_action_date,$last_action_time) = ($1,$2,$3);
Re: A Question on assigning variables from a big variable
by liverpole (Monsignor) on Jun 20, 2006 at 12:25 UTC
    Hi barrycarlyon,

    You're not really doing a split here.  You're assigning variables to the results from a regular expression match.

    Note a couple of things:

    1. You should change =- to =~ (the regular expression binding operator (thanks jwkrahn, below!))
    2. You shouldn't be capturing the values (10) and (8).  My guess is that you either meant "10" and "8" (without the parentheses), or (more likely) "{10}" and "{8}", to mean 10 digits and 8 digits respectively.

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      =~ is not the "regular expression operator", for example:
      $string =~ tr/a-z/A-Z/;
      Notice that there is no regular expression involved in that example. Is the binding operator.
Re: A Question on assigning variables from a big variable
by explorer (Chaplain) on Jun 20, 2006 at 12:26 UTC
    I suppose the line is:
    my ($session_id, $last_action_date, $last_action_time) = $session =~ / +^(\d\d)-(\d{10})-(\d{8})$/;
Re: A Question on assigning variables from a big variable
by johngg (Canon) on Jun 20, 2006 at 12:50 UTC
    Then try

    my ($session_id, last_action_date, $last_action_time) = $session =~ m{^(\d\d)-(\d{4}(?:-\d\d){2})-(\d\d(?::\d\d){2})$};

    Cheers

    JohnGG