in reply to Re: Reading multiple lines from text file
in thread Reading multiple lines from text file

thank you. Could you help me how it looks to write inside a file,and not execute on command line
  • Comment on Re^2: Reading multiple lines from text file

Replies are listed 'Best First'.
Re^3: Reading multiple lines from text file
by choroba (Cardinal) on Aug 07, 2018 at 21:35 UTC
    perlrun shows exactly what -n adds to the script, the rest you can copy verbatim.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^3: Reading multiple lines from text file
by Athanasius (Cardinal) on Aug 08, 2018 at 05:55 UTC

    Hello newperlbie, and welcome to the Monastery!

    Here’s a variation on choroba’s solution which uses the flip-flop operator and a single flag:

    use strict; use warnings; my $in = 0; while (<DATA>) { if (/ dev$/ .. / manage$/) { $in = 1; print; } elsif ($in && / manage$/) { print; } else { $in = 0; } } __DATA__ aaa23 fgfgdf pppp Released test bbb34 fgfgdf pppp Released test dfsad324 fgfgdf pppp Released test efdewr23 fgfgdf pppp Released dev dsarfew234 fgfgdf pppp skip dev dqewr2321 fgfgdf pppp skip prod sdsw32 fgfgdf pppp Released prod asdw234 fgfgdf pppp Released prod sadw2342 fgfgdf pppp Released qa deww234 fgfgdf pppp Released qa qdrqew234 fgfgdf pppp block qa swd234 fgfgdf pppp Released manage swdq234 fgfgdf pppp Released manage dfwfr43 fgfgdf pppp Released manage drqewr234 fgfgdf pppp Released manage drqewr234 fgfgdf pppp Released qb

    Output:

    15:54 >cd ..\Tests\1901-2000 15:54 >perl 1916_SoPW.pl efdewr23 fgfgdf pppp Released dev dsarfew234 fgfgdf pppp skip dev dqewr2321 fgfgdf pppp skip prod sdsw32 fgfgdf pppp Released prod asdw234 fgfgdf pppp Released prod sadw2342 fgfgdf pppp Released qa deww234 fgfgdf pppp Released qa qdrqew234 fgfgdf pppp block qa swd234 fgfgdf pppp Released manage swdq234 fgfgdf pppp Released manage dfwfr43 fgfgdf pppp Released manage drqewr234 fgfgdf pppp Released manage 15:54 >

    Hope that helps,

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

      Thank You....yes,it helped. Looking for a little variation now, if I need to output only the keys dev,prod,qa,manage and not the entire line? can a little variation here help? or even more,if I am using variables instead of hardcoding, say if (/ $first/../ $last/), how to get only these strings, print and store them into an array(?) which can be traversed later.

        Since the key is always the last word on the line, you can get it by using split (which defaults to splitting on whitespace) and then index the resulting list with [-1] to get the last word (if any). You can then push the key onto an array for later use:

        use strict; use warnings; use Data::Dump; my $first = 'dev'; my $last = 'manage'; my $in = 0; my @keys; while (<DATA>) { if (defined(my $key = (split)[-1])) { if ($key eq $first .. $key eq $last) { $in = 1; push @keys, $key; } elsif ($in && $key eq 'manage') { push @keys, $key; } else { $in = 0; } } } dd @keys; __DATA__ < as before >

        Output:

        17:42 >perl 1916_SoPW.pl ( "dev", "dev", "prod", "prod", "prod", "qa", "qa", "qa", "manage", "manage", "manage", "manage", ) 17:42 >

        Note: the defined test is needed only if the input file might contain blank lines.

        Hope that helps,

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