Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PERL Spliting

  • 07-07-2006 7:27pm
    #1
    Closed Accounts Posts: 1,541 ✭✭✭


    Hi,

    I want to split $input1 into 2 separate variables for each part of the time:

    $in1 = '5.30';

    I want a new var call $part1 to be equal to 5 and $part2 to be equal to 30. I want to split the time up into 2 separate variables.

    Is there a way of doing this without using the split function cause the split function did not seem to work for me.

    I got code like the following on here before for splitting up something else but how do I do it for the above?:


    if ($inputexample =~ /(\d+):(\d+) (.*)/) {
    $hour = $1;
    $minute = $2;


    }


Comments

  • Registered Users, Registered Users 2 Posts: 3,886 ✭✭✭cgarvey


    changing your example slightly ...
      if( $inputexample =~ /(\d+)\.(\d+)/) {
        $hour = $1;
        $minute = $2;
      }
    

    Breaking the bits between the // ..
    (\d+) Match any numeric digit, or digits (i.e. 1 or more digits), and store them in $1
    \. Match a full stop
    (\d+) Match any numeric digit, or digits (i.e. 1 or more digits), and store them in $2

    .cg


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Many thanks.

    I also amnaged to get the split function going, I put in square brackets around the dot like [.] and it did the trick but thanks again anywya for your help. :)


  • Registered Users, Registered Users 2 Posts: 6,525 ✭✭✭daymobrew


    I profiled three variations of the code here. I always believed that completeness in a regular expression was best, but the benchmark results for this don't comfirm this.

    /(\d+)\.(\d+)/ - Match explicit dot (.). No ^$ anchors. Called DotNoAnchor in the code below.
    /(\d+)[.](\d+)/ - Match class of chars (only a dot here). No anchors. ClassNoAnchor.
    /^(\d+)\.(\d+)$/ - Match dot. Use anchors. DotWithAnchor.
    I felt that the last one would be best because it tells the regex engine where to start looking for matches thus saving it some work.

    The code below uses the Benchmark module (a standard module) to compare the three regular expressions.

    [PHP]#!/usr/bin/perl -w

    use strict;
    use Benchmark 'timethese';

    my $in = '5.30';
    my ( $hr, $min );
    # Run the 3 subroutines 10 million times.
    timethese( 10_000_000,
    { 'DotNoAnchor' => sub { if( $in =~ /(\d+)\.(\d+)/) { $hr = $1; $min = $2; } },
    'ClassNoAnchor' => sub { if( $in =~ /(\d+)[.](\d+)/) { $hr = $1; $min = $2; } },
    'DotWithAnchor' => sub { if( $in =~ /^(\d+)\.(\d+)$/) { $hr = $1; $min = $2; } } } );[/PHP]
    $ perl -w split-time.pl
    Benchmark: timing 10000000 iterations of ClassNoAnchor, DotNoAnchor, DotWithAnchor...
    ClassNoAnchor: 22 wallclock secs (22.04 usr +  0.01 sys = 22.05 CPU) @ 453473.61/s (n=10000000)
    DotNoAnchor: 21 wallclock secs (22.04 usr +  0.00 sys = 22.04 CPU) @ 453679.34/s (n=10000000)
    DotWithAnchor: 24 wallclock secs (24.80 usr +  0.00 sys = 24.80 CPU) @ 403290.85/s (n=10000000)
    
    So, the DotNoAnchor is slighly faster than ClassNoAnchor. I was surprised that DotWithAnchor was slowest.


Advertisement