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

Hello Monks,

I read a file and grep for keyword and string resulted line in array. so far so good. Now from this line i want to take out values between "" which does not work.

my $file = "/tmp/test.xml"; open (INFILE, "<$file"); my @FiTime = grep {/SentTime/} (<INFILE>); Print "\n***** @FiTime";

This returns <SentTime v="2013-10-21T07:54:21z">

now i add following line of code my @ActTime = split (/(^".*")/, @FiTime);

but this does not return 2013-10-21T07:54:21z, it return only 1.

could some tell me what wrong i am doing?

Replies are listed 'Best First'.
Re: Extract value between quotes
by hdb (Monsignor) on Oct 21, 2013 at 13:54 UTC

    If you look up split, you will see that it expexts a string as its second argument. @FiTime will be evaluated in scalar context, which is the number of fields in the array, e.g. 1. The regex provided to split will not match, so ActTime will only contain one element, i.e. 1.

    Secondly, your regex /(^".*")/ does not match your string, as the caret matches only from the beginning of your string. Try something like /"(.*?)"/ which will return the string between the first two quotes encountered.

    In total, you probably want to apply the regex to each element of your array like this:

    my @ActTime = map { /"(.*?)"/ } @FiTime;

      Thanks. You are always helpful. i was wondering if it is possible to use localtime() and convert this time to my systems timezone (CET)?

        The date/time returned by localtime should already be in your system’s timezine. However, for general conversions between timezones I like the OO-interface to the Date::Manip family of modules. Daylight saving time is handled correctly. Here is some example code:

        Output:

        16:42 >perl 753_SoPW.pl My time (EST) is Tue Oct 22 16:42:35 2013 Your time (CET) is Tue Oct 22 08:42:35 2013 16:42 >

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,