Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Editing just one column in a file

by BrowserUk (Pope)
on Nov 29, 2011 at 02:35 UTC ( #940507=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^3: Editing just one column in a file
Select or Download Code
Re^4: Editing just one column in a file
by vinian (Beadle) on Nov 29, 2011 at 09:00 UTC
    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

        3ks, Tux, I know it's wrong to use $[ $index_begin .. $index_end ], but i want to why range operator produce strange result in different content( scalar or list).
        and also 3ks choroba, i think I got it after seeing what you posted.

Re^4: Editing just one column in a file
by ZWcarp (Beadle) on Nov 30, 2011 at 20:23 UTC

    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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2014-10-21 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls