Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

search and replace a word in a column

by lakssreedhar (Acolyte)
on Aug 07, 2013 at 08:29 UTC ( [id://1048289]=perlquestion: print w/replies, xml ) Need Help??

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

i have a file with 9 tab seperated columns.I want to replace the value "I" in 9th column if I is preceded by o in the 9th column

o I B I

here i want to replace the first I and print the whole file with 9 columns.

Replies are listed 'Best First'.
Re: search and replace a word in a column
by Anonymous Monk on Aug 07, 2013 at 08:41 UTC

      Hi Lakssreedhar
      Use a 2D array to read a file column wise. Then adopt the folowing steps :

      1. count the number of lines of your file.
      2. Use some regular expression to split the file with respect to the column delimiters i.e tab in your case . Thus build the 2D array.
      3. Use a sub routine to get all the values of the 9th column and push them in an array.
      4. Process the array to look where at which index I is preceded by O. Find those indexes.
      5. open your data file again and using the 2D matrix just replace those positions .
      The following code could be a bit help to you

      my @matrix; my $row = 0; # count no. of lines open(DATA, "< data.txt"); my @lines = <DATA>; close(DATA); my $c = @lines; open(DATA, "< data.txt"); push @{$matrix[$row++]}, split while <DATA>; dd @matrix; &colprint(\@matrix); close(DATA); my @arrayOfninth_col; sub colprint { my $matrix_ref = shift; for (1 .. $c) { print "$matrix_ref->[$c-1][9] \n"; my $value = $matrix_ref->[$c-1][9]; push @arrayOfninth_col , $value; } # process the array @arrayOfninth_col for indexes }

        i have just split the columns using tab and then again split the 9th column and checked if value of a particular index is I and index-1 is o.But the substitution is not working properly

Re: search and replace a word in a column
by ghosh123 (Monk) on Aug 07, 2013 at 09:19 UTC

    Hi Lakssreedhar
    Use a 2D array to read a file column wise. Then adopt the folowing steps :

    1. count the number of lines of your file.
    2. Use some regular expression to split the file with respect to the column delimiters i.e tab in your case . Thus build the 2D array.
    3. Use a sub routine to get all the values of the 9th column and push them in an array.
    4. Process the array to look where at which index I is preceded by O. Find those indexes.
    5. open your data file again and using the 2D matrix just replace those positions .
    The following code could be a bit help to you

    my @matrix; my $row = 0; # count no. of lines open(DATA, "< data.txt"); my @lines = <DATA>; close(DATA); my $c = @lines; open(DATA, "< data.txt"); push @{$matrix[$row++]}, split while <DATA>; dd @matrix; &colprint(\@matrix); close(DATA); my @arrayOfninth_col; sub colprint { my $matrix_ref = shift; for (1 .. $c) { print "$matrix_ref->[$c-1][9] \n"; my $value = $matrix_ref->[$c-1][9]; push @arrayOfninth_col , $value; } # process the array @arrayOfninth_col for indexes }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-03-19 04:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found