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

Hi All,

Here i am wriiten code to to get the every groups based on this pattern > to > as each. So i want to store the each group as "> to >" push in to array as each array elements. But i could able to store the each group. it stored has single group start from "> to end >". please help me below i have givn sample text file

b

Thanks & Reg,

Senthil. V

Input: >RTRV-PM-ALL:ADEL-OM3500-1:ALL:1898::,0-UP,NEND,,1-DAY,02-11,,;| ADEL-OM3500-1 09-02-12 03:46:54 M 1898 COMPLD "OC192-11,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:OPRN,40,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"

>RTRV-PM-ALL:ADEL-OM3500-1:ALL:1898::,0-UP,NEND,,1-DAY,02-11,,;| ADEL-OM3500-1 09-02-12 03:46:56 M 1898 COMPLD "OC192-12,OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC48-3,OC48:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-145,STS3C:CVP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-145,STS3C:ESP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-145,STS3C:SESP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" >;

my $InputFile = "sample_log_file.txt"; open (my $IN, '<', $InputFile) or die "cannot open $ea +ch_file for writing: $!"; undef $/; # undef by default if localized while ($line = <$IN>) { #$line =~ s{^(RTRV)}{\>$1}gsi; #$line =~ s{(\"\s+)(;)}{$1\>$2}gsi; if($line =~ m/^\>(.*)\>/gis) { push @all_metrics, "$1"; } } print Dumper(@all_metrics); close($IN)

Replies are listed 'Best First'.
Re: Push the each group item into array
by davido (Cardinal) on Jun 07, 2011 at 06:43 UTC

    I've re-read your post a few times trying to make sure I was understanding the description of the problem. I may be way off in my interpretation, and if so, I'll apologize in advance.

    It seems like you're saying that your records are separated by a > character. We get so used to thinking of files in terms of lines that it's easy to forget that the input record separator can be other values aside from '\n' or undef. Try setting it like this: local $/ = ">";

    use strict; use warnings; use Data::Dumper; my $InputFile = "sample_log_file.txt"; my @all_metrics; open my $IN, '<', $InputFile or die $!; { local $/ = ">"; while( my $record = <$IN> ) { chomp; # Removes trailing '>' push @all_metrics, $record; } close $IN; } print Dumper @all_metrics;

    I also noticed that your 'die' error message says something about not being able to open the file for writing. Everything else we've been discussing deals with opening for reading, not writing. So it might save you future headaches to fix the error message.


    Dave

      Great Thanks Davido. it works fine. and also i know about local $/;

        i learned local $/.
Re: Push the each group item into array
by jwkrahn (Abbot) on Jun 07, 2011 at 06:14 UTC

    It sounds like you want something like:

    my $InputFile = "sample_log_file.txt"; open my $IN, '<', $InputFile or die "cannot open $each_file for writin +g: $!"; undef $/; # undef by default if localized my @all_metrics = <$IN> =~ /[^>]+/g; print Dumper( \@all_metrics ); close $IN;

      Thanks for code.

      it really works here. i got output here. but i need made some modification before put pattern as ur pattern /^>+/. because i need to read the first and last line of text file. because i add manually added '>' symbol in first and last line. In ur code i could not able put pattern

      $line =~ s{^(RTRV)}{\>$1}gsi;

      $line =~ s{(\"\s+)(;)}{$1\>$2}gsi;
Re: Push the each group item into array
by BrowserUk (Patriarch) on Jun 07, 2011 at 06:34 UTC

    You say:

    i want to store the each group as "> to >"

    And you supply what we can only assume are meant to be two records of input. But there are only 3 '>' characters in total.

    So either you want

    • the first record to not end with '>';
    • Or the second '>' to be (re)used both to end the first record and start the second;
    • Or you omitted a '>';

    Which is it?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.