suekawar has asked for the wisdom of the Perl Monks concerning the following question:

Hi all , this seem to be simple but I have had it with it: my code looks like this
$envLwr =~ tr/a-z/A-Z/; my $envUpr= $envLwr; $envUpr = qx[cut -c3-6]; my $envCode= $envUpr;
I keep getting error because of the "cut" . is it the right way to do it or am i doing it wrong; thanks for help.

Replies are listed 'Best First'.
Re: cut in perl
by Kanji (Parson) on Feb 21, 2003 at 00:07 UTC

    It isn't clear if you're trying to cut the string in $envUpr or a file name that $envUpr names, but you would want to use something like the following instead...

    $envUpr = qx[echo $envUpr | cut -c3-6]; # string $envUpr = qx[cut -c3-6 $envUpr]; # file

    However, a more Perlish way of cuting a string is to use substr, like so...

    $envUpr = substr( $envUpr, 2, 5 ); # offsets from 0 not 1

        --k.


Re: cut in perl
by allolex (Curate) on Feb 21, 2003 at 00:22 UTC

    Hi,

    Just a couple of things... First of all, why not use uc() to change the string to upper case (or did I misunderstand your code--well possible)? Secondly, what is the text of the error message you are getting? Whenever you ask for help, the error message should be included. Oh, and a serious by-the-way: what are you trying to accomplish with the script?

    cut might not be the appropriate tools for what you want to do.

    --
    Allolex

Re: cut in perl
by bsb (Priest) on Feb 21, 2003 at 03:19 UTC
    I had this lying around. You may be able to adapt it to your needs. See also perldoc:perlrun and the -F + -a options.
    cat ./bin/ncut #!/usr/bin/perl -l -a -w -s -F/\t/ -n =pod ncut '-f=field1,field4,total*' file.tsv ncut - named and ordered field cut can sneak in perl regexp stuff =cut BEGIN { die "use -f=name1,name2" unless $f; my @f = split /,/, $f; { no warnings 'once'; $_ = <>; } chomp; @F = split /\t/; #$re = join '|', @f; @keep = grep { $F[$_] =~ /($re)$/ } 0..$# +F; for $f (@f) { push @keep, grep { $F[$_] =~ /$f/ } 0..$#F; } print join "\t", @F[@keep]; } print join "\t", @F[@keep];
Re: cut in perl
by data64 (Chaplain) on Feb 21, 2003 at 00:04 UTC

    What is the error you get ?

    You do not seem to be giving cut any file so it is probably waiting for input from STDIN.


    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd