http://www.perlmonks.org?node_id=1060718

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

An array contains few user IDs. Each user ID is used as a match element in another file. How to match a specific line and array element? And print words at previous line?

The specific line is:

  cell ("b05hrn23ud0d3") {

array file:

b05bdc00lnz64 b05hrn23ud0d3 b05bgn00lnz00 b05bgn01lnz00

coding:

foreach $a (@arr) { #read each line in array file for (my $count=0; $count <5; $count++) { #iteration for each ID in + array file if(@arr[$count] =~ m/cell\(@arr[$count])){ #match specific line print "aa\n"; #print words at previous line } }

Replies are listed 'Best First'.
Re: Perl: How to match a line and print words at line immediately above it?
by hdb (Monsignor) on Nov 01, 2013 at 09:07 UTC

    In order to access a single element of an array you would usually write $arr[$count] and not @arr[$count]. If you add use strict;use warnings; at the beginning of your script, Perl would highlight those occations to you. In order to access the element before $arr[$count], you would write $arr[$count-1], potentially checking that $count is greater than zero beforehand.

Re: Perl: How to match a line and print words at line immediately above it?
by Doozer (Scribe) on Nov 01, 2013 at 09:57 UTC
    The way I iterate through an array and keep track of the previous element is like so:

    my $previous = ''; foreach my $line (@arrayfile) { if ($line =~ m/WhateverIWantToMatch/) { print "$previous\n" if ($previous); } $previous = $line; #### This line now becomes the previous line for + the next array element seeing as ALL lines in the file will be proce +ssed }

    This is a quick example and presumes you are not going to find a match on the first line of the file.

      ... or perhaps my $previous = undef; to clearly reflect that there is no previous-line, much as NULL (in a database column) denotes the complete absence of any value at all.   Whatever makes the most sense in your particular situation.

Re: Perl: How to match a line and print words at line immediately above it?
by davido (Cardinal) on Nov 01, 2013 at 16:40 UTC

    I apologize for disrupting the thread with this, but I think you are not seeing my /msg's...

    Your Perl related questions belong in Seekers of Perl Wisdom, not in Perlmonks Discussion, which is reserved for the discussion of topics related to this site. This has been a problem with at least three of your posts (I moved one manually, and two others were duplicates that got reaped). Also, you have duplicate-posted five times (the duplicates have been reaped). This duplication is unnecessary, and an annoyance that can lead to confusion; eventually someone will mistakenly reply to a duplicate and then we have two problems to fix.

    After you post a question someone here at the Monastery must manually approve it. This can take a few minutes, during which time you may have difficulty seeing your original post. This also may occur by hitting your browser's back button and re-sending POST data. Please dont duplicate your posts.


    Dave

Re: Perl: How to match a line and print words at line immediately above it?
by Anonymous Monk on Nov 01, 2013 at 09:02 UTC
    use math like $count+1 or $count-1 or some such