Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Editing just one column in a file

by vinian (Beadle)
on Nov 29, 2011 at 02:04 UTC ( [id://940506]=note: print w/replies, xml ) Need Help??


in reply to Re: Editing just one column in a file
in thread Editing just one column in a file

hi, i am confused about this, need your help.
echo "Just another perl monkers " | perl -lane 'print $F[0..3]'
Output:
Just
why it treat $F[0..3] as $F[0] but not $F[1]or $F[2]or $F[3].

Replies are listed 'Best First'.
Re^3: Editing just one column in a file
by BrowserUk (Patriarch) on Nov 29, 2011 at 02:35 UTC

    Because the Perl syntax for an array slice is @a[ ... ].

    If you enable warnings, you'll see that by using $a[1..3], the 1..3 is treated as a flip-flop operator comparing against $., and under most circumstances will produce false, which in the numeric context gets taken as 0:

    c:\test>perl -wE"@a = 0..10; say $a[1..3]; say @a[1..3]" Use of uninitialized value in range (or flip) at -e line 1. Argument "" isn't numeric in array element at -e line 1. 0 123

    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Why is it that this

       perl -lane '$,="\t"; print @F[0..4] ."\t\t\t" . @F[25..30]' file.txt

      Does not do what I intended ( for the ranges to be printed by tabs, followed by three blank columns followed by another range). Thankyou for your help!

        Because $, is the character used when you give a list of values to the print statement, but you've used '.'s.

        Ie. You're giving the print statement a single (concatenated) value, therefore the item separator is never used.

        This might achieve your goal:

        perl -lane '$,="\t"; print @F[0..4], "\t\t\t", @F[25..30]' file.txt

        but "three blank columns" is a very nebulous concept.

        Update: FTR, I might code that as:

        print join "\t", @F[0..4], ('')x3, @F[25..30];

        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".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

      thanks, but i got something more strange
      vinian@cc:~$ echo "Just another perl monker" | perl -w -lane 'print $F +[0..3]' Argument "" isn't numeric in array element at -e line 1, <> line 1. Just vinian@cc:~$ echo "Just another perl monker" | perl -w -lane 'print $F +[1..3]' another vinian@cc:~$ echo "Just another perl monker" | perl -w -lane 'print $F +[2..3]' Argument "" isn't numeric in array element at -e line 1, <> line 1. Just vinian@cc:~$ echo "Just another perl monker" | perl -w -lane 'print $F +[3..3]' Argument "" isn't numeric in array element at -e line 1, <> line 1. Just

      when $F[1..3] there is no warnings and the output is $F[1]

        In scalar context, the .. operator with numeric constants is interpreted as line range. As you are reading lines with -n and you are on the first line, 1..3 evaluates to 1. All other ranges evaluate to undef (stringified to ''), because the first line is out of the range, and that's why you get the warning.

        You should've used @ again:

        $ echo "Just another perl monker" | perl -w -lane 'print @F[1..3]' anotherperlmonker $ echo "Just another perl monker" | perl -wanE'say@F[1..3]' anotherperlmonker $ echo "Just another perl monker" | perl -w -lane 'print @F[3..3]' monker $ echo "Just another perl monker" | perl -wanE'say@F[3..3]' monker $

        Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

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

    No recent polls found