CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
command line slice argument

 



ryantkoehler
Novice

Feb 28, 2012, 1:19 PM

Post #1 of 5 (2135 views)
command line slice argument Can't Post

Hi

I would like to accept a slice argument (string) from the command line that then may be used to select a subset of columns from an input file. Doing this directly apparently does not work (error is that argument (to slice) is not numeric).

Here are some more details.

I use GetOptions to accept a command line string such as 1..5,10..15 (i.e. $col_slice = "1..5,10..15). Next, once I've got a list of items (e.g. words on a line), I would like to simply get the subset (if specified):

if ( $col_slice ) {
@words = $words[$col_slice];
}

I'm sure this is "easy" if you know the trick. I don't know the trick.

Thanks in advance for any help.
-Ryan


BillKSmith
Veteran

Feb 28, 2012, 2:44 PM

Post #2 of 5 (2132 views)
Re: [ryantkoehler] command line slice argument [In reply to] Can't Post


Code
use strict; 
use warnings;
my @words = qw( a b c d e f g h i j k l m n o p q );
my $col_slice = '1..5,10..15';
@words = @words[(eval $col_slice)];
print join( "\n", @words);

Good Luck,
Bill


ryantkoehler
Novice

Feb 28, 2012, 3:13 PM

Post #3 of 5 (2129 views)
Re: [BillKSmith] command line slice argument [In reply to] Can't Post

Thanks for getting back to me

Unfortunately, that didn't work.

What I want is to be able to accept a string and (somehow) convert this into a slice argument (without parsing the thing myself). Your eval didn't do this.

My "first language" is C, so I was hoping there was something equivalent to printing a format string (e.g. for printf() ) that might do the conversion for me (I wasn't thinking error checking to make sure the slice syntax was reasonable.. but maybe?). I still think that Perl likely has a means to do what I'm hoping for.

Thanks again,
-Ryan

p.s. I'm uploading my script (that doesn't work with the "-csl 1..5,10..15" argument)


BillKSmith
Veteran

Feb 28, 2012, 7:23 PM

Post #4 of 5 (2123 views)
Re: [ryantkoehler] command line slice argument [In reply to] Can't Post

It seems that your upload did not work.

I posted a complete working program. If yours does not work the same, there are only two possibilities.

  • You are not using GetOptions correctly. Try printing the value of $col_slice to verify that it contains the string which I used. (This is what you expect is it not?)


  • You are not using eval correctly. (The apparently extraneous parentheses are needed to force list context)

  • Good Luck,
    Bill


    ryantkoehler
    Novice

    Feb 28, 2012, 7:50 PM

    Post #5 of 5 (2116 views)
    Re: [BillKSmith] command line slice argument [In reply to] Can't Post

    I may be incorrect in either/both cases you mentioned. Hopefully this time the atechment works.
    Thanks again for the help.

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives