Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This is where showing sample data is really important if you want people to be able to help you. I am going to make some guesses about your data files and attempt to answer your question.

First, I think your columns file looks like this:

1 3 4 6

If that's the case, then you are forgetting to split the columns data.

In any case, you are doing something strange when you access the data array in your while loop.

!/usr/bin/perl use strict; use warnings; my $col_file = shift; my $data_file = shift; open my $columns, '<', $col_file or die "Could not open $col_file - $!\n"; open my $file, '<', $col_file or die "Could not open $data_file - $!\n"); my @columns = split /\s+/, <$columns>; chomp(@columns); # use a hash for quick lookup of columns to skip # here I use a hash slice to assign undef values to the keys stored in + @columns my %skip; @skip{@columns} = (); while( my $line = <$file>) { chomp $line; my @raw_data = split /\s+/, $line; # Map and grep are a bit tough for newbies but they are important, + powerful tools that should be mastered. # Read from right to left: # Take the list of indexes in @raw_data # (grep) keep only those that do not exist as keys to %skip # (map) and look up the the item at that index in @raw_data # and finally print the resulting list. print join ' ', map $raw_data[$_], grep !exists $skip{$_}, 0..$#raw_data; print "\n"; }

Accessing a negative array index indexes from the end of the array.

Negating an array, causes the array to be evaluated in scalar context, yielding the number of members in the array. This value is then negated. So if @columns has 6 members, then -@columns is -6.

Finally, a few points.

  • You can use a slice instead of map: print "@raw_data[ grep !exists $skip{$_}, 0..$#raw_data ]"; if you are strongly wedded to using an array slice.
  • If you know the width of your data file you can build your keep list outside the loop. my @keep = grep !exists $skip{$_}, 0..$width; ... print "@raw_data[@keep]\n";

    Update I made some updates with a few little bug fixes, and some extra points on slices and moving the keep list calculation out of the while loop. If things are suddenly different on you, that's why.


    TGI says moo


    In reply to Re: cut columns with an array slice by TGI
    in thread cut columns with an array slice by coldy

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found