Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: remove last column of tab-delimited file

by kroach (Pilgrim)
on Jul 22, 2016 at 17:57 UTC ( [id://1168380]=note: print w/replies, xml ) Need Help??


in reply to remove last column of tab-delimited file

You can remove the last element of @header_for_table1 before reading the rest of $table1. This way, when you assign split to the hash slice of %row, the last element will be ignored.
  • Comment on Re: remove last column of tab-delimited file

Replies are listed 'Best First'.
Re^2: remove last column of tab-delimited file
by mika6891 (Initiate) on Jul 22, 2016 at 18:08 UTC
    Would something like this work then?
    chomp( my @header_for_table1 = split /\t/, <$table1> ); @new_header_for_table1 = pop(@header_for_table1); print $table3 join "\t", @new_header_for_table1, "name1", "name2", "\n +";

      No need to assign the pop operation to anything since you want to throw it away. Just:

      pop(@header_for_table1);

      EDIT:

      To clarify, pop will alter the array you are using it on by removing the last element. It returns the last element that is removed, not all the remaining elements excluding the last element removed. So the code you showed would create a new array with just one element, the last element of the original array you wanted to discard. That's not what you want.

      I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
      I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

      Why don't you try it instead of asking? ;)

      I would use splice @header_for_table1, -1 without assignment, as splice returns what it has removed from an array. Likewise pop cuts the last element off an array and returns it, but in contrary what you want is all items but the last, don't you?

      UPDATE: pop() without assignment is the same like splice(@array, -1), yeah.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found