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

Hi Monks!!

Somebody pls help me out!! From the folowing code, I'm supposed to get 3 outputs. Whay am I getting just one??

my $dt = 'Wed Mar 10 10:10:48 IST 2010'; my (@dtar) = split /[ :]/, $dt; $valar = 'Fri_Jan_29_14:30:22_v09.11-e024_1 Mon_Nov_30_09:20:08_v09.1 +1-e006_1 Tue_Dec_22_14:30:07_v09.11-e015_1 Tue_Nov_24_18:45:00_v08. +13-s001_1 Mon_Dec_07_14:30:09_v09.11-e013_1 Thu_Dec_03_14:30:10_v09.11-e010_1 Tu +e_Feb_02_14:30:06_v09.11-e026_1 Wed_Dec_02_09:20:09_v09.11-e008_1 Mon_Jan_04_14:30:06_v09.11-e017_1 T +hu_Dec_10_14:30:11_v09.11-e014_1 Tue_Feb_09_13:47:51_v09.11-e029_1 +Wed_Jan_06_14:30:08_v09.11-e020_1 Mon_Jan_11_14:30:06_v09.11-e021_1 Thu_Feb_04_09:20:06_v09.11-e027_1 T +ue_Nov_24_09:13:22_v09.10-b072_1 Wed_Jan_27_14:30:11_v09.11-e022_1 Mon_Mar_08_14:30:04_v09.11-s008_1 Thu_Jan_28_14:30:13_v09.11-e023_1 +Tue_Nov_24_15:54:18_v09.11-e005_1 Wed_Mar_03_16:35:18_v09.11-s005_1 Mon_Nov_23_12:36:58_v09.11-e004_1 Tue_Dec_01_14:30:08_v09.10-p002_1 +Tue_Nov_24_17:31:07_v09.10-p001_1 Wed_Nov_25_10:27:43_v09.11-e002_1 '; @valar1 = split(/[\s+\n]/, $valar); foreach (@valar1) { if ($_ =~ /@dtar[1]/) { my ($ver1) = $_ =~ m/(v09.11-[a-z]\d{3})/; print "$ver1\n"; last; } } foreach (@valar1) { my ($ver2) = $_ =~ m/(v09.10-[a-z]\d{3})/; print "$ver2\n"; last; } foreach (@valar1) { my ($ver3) = $_ =~ m/(v08.\d{2}-[a-z]\d{3})/; print "$ver3\n"; last; }

Pls also suggest a more efficient way of doing this so i dont have to repeat so much code!

Thanks!!

BG

Replies are listed 'Best First'.
Re: missing outputs
by Ratazong (Monsignor) on Mar 10, 2010 at 10:31 UTC
    foreach (@valar1) { my ($ver2) = $_ =~ m/(v09.10-[a-z]\d{3})/; print "$ver2\n"; last; }

    Due to the last, you are only checking the first line in your array. You probably want to execute the last only if your match was successful.

    HTH, Rata
      yes... yes... I got it..Thank you!!
      My script is working now and giving me 3 outputs. I want to use these outputs in another perl script that has an html page in which I want these outputs to be displayed. I cant figure out how should I do this...
      yes u r right...but how do i achieve that?
Re: missing outputs
by Svante (Sexton) on Mar 10, 2010 at 11:54 UTC
    $valar = 'Fri_Jan_29_14:30:22_v09.11-e024_1 Mon_Nov_30_09:20:08_v09.1 +1-e006_1 +Tue_Dec_22_14:30:07_v09.11-e015_1 Tue_Nov_24_18:45:00_v08.13-s001_1 Mon_Dec_07_14:30:09_v09.11-e013_1 Thu_Dec_03_14:30:10_v09.11-e010_1 Tu +e_Feb_02_1 +4:30:06_v09.11-e026_1 Wed_Dec_02_09:20:09_v09.11-e008_1 Mon_Jan_04_14:30:06_v09.11-e017_1 T +hu_Dec_10_ +14:30:11_v09.11-e014_1 Tue_Feb_09_13:47:51_v09.11-e029_1 Wed_Jan_06 +_14:30:08_ +v09.11-e020_1 Mon_Jan_11_14:30:06_v09.11-e021_1 Thu_Feb_04_09:20:06_v09.11-e027_1 T +ue_Nov_24_ +09:13:22_v09.10-b072_1 Wed_Jan_27_14:30:11_v09.11-e022_1 Mon_Mar_08_14:30:04_v09.11-s008_1 Thu_Jan_28_14:30:13_v09.11-e023_1 +Tue_Nov_24 +_15:54:18_v09.11-e005_1 Wed_Mar_03_16:35:18_v09.11-s005_1 Mon_Nov_23_12:36:58_v09.11-e004_1 Tue_Dec_01_14:30:08_v09.10-p002_1 +Tue_Nov_24 +_17:31:07_v09.10-p001_1 Wed_Nov_25_10:27:43_v09.11-e002_1 '; @valar1 = split(/[\s+\n]/, $valar);
    This is a bit suboptimal, and a bit wrong. Your split regex splits on "a whitespace, a plus sign, or a newline". Anyway, Perl provides a quote-like operator for doing what you want:
    @valar1 = qw(Fri_Jan_29_14:30:22_v09.11-e024_1 Mon_Nov_30_09:20:08_v09 +.11-e006_1 Tue_Dec_22_14:30:07_v09.11-e015_1 Tue_Nov_24_18:45:00_v08 +.13-s001_1 Mon_Dec_07_14:30:09_v09.11-e013_1 Thu_Dec_03_14:30:10_v09 +.11-e010_1 Tue_Feb_02_14:30:06_v09.11-e026_1 Wed_Dec_02_09:20:09_v09 +.11-e008_1 Mon_Jan_04_14:30:06_v09.11-e017_1 Thu_Dec_10_14:30:11_v09 +.11-e014_1 Tue_Feb_09_13:47:51_v09.11-e029_1 Wed_Jan_06_14:30:08_v09 +.11-e020_1 Mon_Jan_11_14:30:06_v09.11-e021_1 Thu_Feb_04_09:20:06_v09 +.11-e027_1 Tue_Nov_24_09:13:22_v09.10-b072_1 Wed_Jan_27_14:30:11_v09 +.11-e022_1 Mon_Mar_08_14:30:04_v09.11-s008_1 Thu_Jan_28_14:30:13_v09 +.11-e023_1 Tue_Nov_24_15:54:18_v09.11-e005_1 Wed_Mar_03_16:35:18_v09 +.11-s005_1 Mon_Nov_23_12:36:58_v09.11-e004_1 Tue_Dec_01_14:30:08_v09 +.10-p002_1 Tue_Nov_24_17:31:07_v09.10-p001_1 Wed_Nov_25_10:27:43_v09 +.11-e002_1);
      The plus signs were added by this site due to space constraints I believe

        Sorry, that was a cut-and-paste carelessness by me. I realize that it has a strange connection to my point of the regex being wrong. I am (and was) aware that they are not part of the real file.

Re: missing outputs
by GrandFather (Saint) on Mar 11, 2010 at 07:37 UTC

    Without an example of the output you expect and lacking a clear description of what you are trying to achieve it's rather hard to know what you want. However the following may give you something to think about:

    use strict; use warnings; use 5.010; my $date = 'Wed Mar 10 10:10:48 IST 2010'; my @parts = split /[ :]/, $date; my $valueStr = <<VALS; Fri_Jan_29_14:30:22_v09.11-e024_1 Wed_Jan_27_14:30:11_v09.11-e022_1 Mon_Mar_08_14:30:04_v09.11-s008_1 Thu_Jan_28_14:30:13_v09.11-e023_1 Tue_Nov_24_15:54:18_v09.11-e005_1 Tue_Mar_01_14:30:08_v09.10-p002_1 Tue_Nov_24_17:31:07_v09.10-p001_1 Wed_Nov_25_10:27:43_v09.11-e002_1 Wed_Mar_25_10:27:43_v08.11-e002_1 VALS my @values = split /[\s\n]+/, $valueStr; for my $value (@values) { next if $value !~ /_$parts[1]_/; given ($value) { when (/(v09.11-[a-z]\d{3})/) {print "$1\n"; continue}; when (/(v09.10-[a-z]\d{3})/) {print "$1\n"; continue}; when (/(v08.\d{2}-[a-z]\d{3})/) {print "$1\n"; continue}; } }

    Prints:

    v09.11-s008 v09.10-p002 v08.11-e002

    I've trimmed and edited the data somewhat to better suit what I guess you might be trying to do and reduce the size and noise in the sample.


    True laziness is hard work
      how can these outputs be displayed in a html page?
        use strict; use warnings; use 5.010; use CGI qw(); my $date = 'Wed Mar 10 10:10:48 IST 2010'; my @parts = split /[ :]/, $date; my $valueStr = <<VALS; Fri_Jan_29_14:30:22_v09.11-e024_1 Wed_Jan_27_14:30:11_v09.11-e022_1 Mon_Mar_08_14:30:04_v09.11-s008_1 Thu_Jan_28_14:30:13_v09.11-e023_1 Tue_Nov_24_15:54:18_v09.11-e005_1 Tue_Mar_01_14:30:08_v09.10-p002_1 Tue_Nov_24_17:31:07_v09.10-p001_1 Wed_Nov_25_10:27:43_v09.11-e002_1 Wed_Mar_25_10:27:43_v08.11-e002_1 VALS my @values = split /[\s\n]+/, $valueStr; my $cgi = CGI->new (); print $cgi->header (); print $cgi->start_html (); for my $value (@values) { next if $value !~ /_$parts[1]_/; given ($value) { when (/(v09.11-[a-z]\d{3})/) {print $cgi->p ($1); continue} +; when (/(v09.10-[a-z]\d{3})/) {print $cgi->p ($1); continue} +; when (/(v08.\d{2}-[a-z]\d{3})/) {print $cgi->p ($1); continue} +; } } print $cgi->end_html ();

        Prints:

        Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <p>v09.11-s008</p><p>v09.10-p002</p><p>v08.11-e002</p> </body> </html>

        True laziness is hard work