Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Access and modify specific element of tab delimited .txt file

by K_Edw (Beadle)
on Mar 30, 2016 at 10:30 UTC ( [id://1159097]=perlquestion: print w/replies, xml ) Need Help??

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

Given a matrix stored as a tab-delimited file with row + col headers, the upper left entry is often blank (a single tab):
A B C D E A 1 1 1 1 1 B 1 1 1 1 1 C 1 1 1 1 1 D 1 1 1 1 1 E 1 1 1 1 1

I wish to be able to access and modify this entry. Ordinarily to access a specific element I would use something like:

perl -lanse 'print $F[$j-1] if $. == $i' -- -i=1 -j=1

However this prints 'A' and does not recognise the initial tab as counting as an element. Is there a way to print something into this first element? For example producing:

ID A B C D E A 1 1 1 1 1 B 1 1 1 1 1 C 1 1 1 1 1 D 1 1 1 1 1 E 1 1 1 1 1

Replies are listed 'Best First'.
Re: Access and modify specific element of tab delimited .txt file
by Eily (Monsignor) on Mar 30, 2016 at 11:21 UTC

    You can use the -F option to specify the split pattern for each line. If the pattern is found at the beginning of the string, $F[0] will be the empty string.

    Edit: so replacing the empty element with 'ID' can be done like this: perl -lF"\t" -E 'print join qq<\t>, map { $_ || q<ID> } @F', which works because there are no 0 in your values.

Re: Access and modify specific element of tab delimited .txt file
by BrowserUk (Patriarch) on Mar 30, 2016 at 11:16 UTC
    Is there a way to print something into this first element?

    I suspect you're after something more complex, but on the face of it, this seems to answer that question:

    C:\test>type junk.dat A B C D E A 1 1 1 1 1 B 1 1 1 1 1 C 1 1 1 1 1 D 1 1 1 1 1 E 1 1 1 1 1 C:\test>perl -plae" $.== 1 and $F[0] eq 'A' and $_ = 'ID' . $_" junk.d +at ID A B C D E A 1 1 1 1 1 B 1 1 1 1 1 C 1 1 1 1 1 D 1 1 1 1 1 E 1 1 1 1 1

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks! This works well (after replacing " with '). It is slightly more complex in that $F[0] will not always eq 'A', so this could also be used:

      perl -plae '$.== 1 and $_ = 'ID' . $_'
        Is there a way for it to simply check for any string (non-delimiter character) at all?

        Eily called it right:

        C:\test>type junk.dat A B C D E A 1 1 1 1 1 B 1 1 1 1 1 C 1 1 1 1 1 D 1 1 1 1 1 E 1 1 1 1 1 C:\test>perl -F\t -plae"$.== 1 and $F[0] eq '' and $_ = 'ID' . $_" jun +k.dat ID A B C D E A 1 1 1 1 1 B 1 1 1 1 1 C 1 1 1 1 1 D 1 1 1 1 1 E 1 1 1 1 1

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Access and modify specific element of tab delimited .txt file
by duelafn (Parson) on Mar 30, 2016 at 14:05 UTC

    If you literally only need to create first entry, don't split?

    perl -pe 'BEGIN { print "ID"; }' /tmp/test.dat

    Good Day,
        Dean

      This is an excellent example of finding the X in an X/Y problem. You've done a good job of separating the problem from the proposed solution on even a fairly simple example program. I'm going to bookmark this to reference for future discussions of this sort.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-25 01:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found