Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: Reading multiple lines from text file

by Athanasius (Archbishop)
on Aug 08, 2018 at 07:45 UTC ( [id://1220056]=note: print w/replies, xml ) Need Help??


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

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,

Replies are listed 'Best First'.
Re^6: Reading multiple lines from text file
by newperlbie (Acolyte) on Aug 08, 2018 at 08:01 UTC
    Ah thanks!! but the key is not the last word on the line!! in fact that was my previous comment, but I removed it since I figured a way to grep it from the middle of the line.The key column can be any where ,not necessarily last....what should be the adaptation now.
    aaa23 fgfgdf test pppp Released bbb34 fgfgdf test pppp Released dfsad324 fgfgdf test pppp Released efdewr23 fgfgdf dev pppp Released dsarfew234 fgfgdf dev pppp skip dqewr2321 fgfgdf prod pppp skip sdsw32 fgfgdf prod pppp Released asdw234 fgfgdf prod pppp Released sadw2342 fgfgdf qa pppp Released deww234 fgfgdf qa pppp Released qdrqew234 fgfgdf qa pppp block swd234 fgfgdf manage pppp Released swdq234 fgfgdf manage pppp Released dfwfr43 fgfgdf manage pppp Released drqewr234 fgfgdf manage pppp Released

    2018-08-19 Athanasius added code tags

      Hello again newperlbie,

      (1) Please read Markup in the Monastery “Chapter 1” and update your post accordingly.

      (2) “in fact that was my previous comment, but I removed it

      In future, please use <strike>...</strike> tags around discarded text rather than deleting it. Also clearly mark any updates as such. This will reduce the likelihood of confusion for responders to your posts.

      (3) “I figured a way to grep it from the middle of the line.

      Good! (But then, why the next question?)

      (4) “The key column can be any where ,not necessarily last....what should be the adaptation now.

      Can the key column change from one line to another? Is it guaranteed that the key words will never appear accidentally in non-key positions within a line? If so, you can just test for the presence of a keyword anywhere in the line with a simple regular expression:

      while (<DATA>) { if (/\b$first\b/ .. /\b$last\b/) { $in = 1; print; } elsif ($in && /\b$last\b/) { print; } else { $in = 0; } }

      (In a regex, \b matches a word boundary.)

      Hope that helps,

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1220056]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found