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

Hi,
I have a string variable like
$filename='XX-ZZ-YYYYMMDDHH'
All characters there are digits, apart from the minuses. I want to extract the numbers XX,ZZ,YYYY,MM,DD,HH (all 2 digits apart from YYYY that is four, i.e. they have fixed positions) and assign them to variables. How can i do that?
thanks, roberto

Replies are listed 'Best First'.
Re: extracting numbers from a string
by jeroenes (Priest) on Feb 07, 2001 at 18:26 UTC
    This is really basic knowledge, and I hope you can gain some understanding by reading the docs. There are various methods, depending on what you like (or want to learn). I suggest you take a look at perlre and unpack, you can use both. Or use substr, like modred says, it's faster than m//.

    But to give you a start, try this: ($x,$z) = $filename =~ /(\d\d)-(\d\d)/;for the first two numbers. The others are left as an exercise.... or just fill in modred's regex.

    Jeroen
    "We are not alone"(FZ)

    Update: Thought you might as well have a look at 7 Stages of Regex Users

Re: extracting numbers from a string
by Albannach (Monsignor) on Feb 07, 2001 at 19:08 UTC
    As jeroenes mentioned, TIMTOWTDI, and I favour unpack for cases like this:
    $filename = '11-22-2001012004'; ($x,$z,$y,$m,$d,$h) = unpack('a2xa2xa4a2a2a2', $filename)

    --
    I'd like to be able to assign to an luser

Re: extracting numbers from a string
by modred (Pilgrim) on Feb 07, 2001 at 18:22 UTC
    If they are all in fixed positions you can use substr repeatedly to extract the values or you can use a regular expression and assign from the $1-$9 variable.

    perldoc -f substr perldoc perlre

    the regular expression code would look something like

    $filename='XX-ZZ-YYYYMMDDHH' $filename =~ /^(\d{2})-(\d{2})-(\d{4})(\d{2})(\d{2})(\d{2})$/; # $1 is XX, $2 is ZZ, and so on
Re: extracting numbers from a string
by kschwab (Vicar) on Feb 07, 2001 at 18:27 UTC
    Have a look at perldoc perlre.

    #!/usr/bin/perl my($filename)="11-22-2001012004"; my($x,$z,$y,$m,$d,$h)= ($filename=~/(\d\d)-(\d\d)-(\d\d\d\d)(\d\d)(\d\d)(\d\d)/); printf "[%s] [%s] [%s] [%s] [%s] [%s]\n", $x,$z,$y,$m,$d,$h;

    Produces:

    [11] [22] [2001] [01] [20] [04]
Re: extracting numbers from a string
by davorg (Chancellor) on Feb 07, 2001 at 18:39 UTC

    The easiest way might be something like this:

    #!/usr/bin/perl -w use strict; my $data = '12-34-2001020713'; my @nums = $data =~ /\d\d/g; print "@nums\n";

    But you end up with the year split between two elements of the array. I suppose it's not too difficult to join them together again.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: extracting numbers from a string
by Anonymous Monk on Feb 07, 2001 at 19:32 UTC
    thanks you all for the explanations and suggestions for readings. And sorry if the question was a silly one: I've just started this week with Perl...
    roberto