File open problem

Discussion in 'Perl Misc' started by IJALAB, Nov 23, 2006.

  1. IJALAB

    IJALAB Guest

    I am trying to take the files as command line arguments and do some
    formatting to the files based on their extensions. I am getting error
    in the file open when i use $ARGV[$i]
    Also, I am assigning a fixed name to my files. can i dynamically do
    them based on my input file names?

    print $#ARGV;
    $copypath = "<path>";
    for ($i=0;$i<=$#ARGV;$i= $i + 1)
    {

    $b0 = system("copy $ARGV[$i] $copypath\\scripts");
    $b1 = chdir ("$copypath\\scripts");
    $extn = substr($ARGV[$i],-4,4);
    print $extn, "\n";
    print $ARGV[$i];
    $file = $ARGV[$i];
    if ($extn == ".txt")
    {

    #Error here!!!
    open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";
    open ($ofh, '>OutFile.txt') or die "Cannot open Output File:";


    while (my $line = <$fh>) {
    <Formatting here>


    }


    close $fh;
    close $ofh;
    $b3 = chdir ("$copypath");

    }

    elsif ($extn == ".csv")
    {
    #Error here!!!
    open my $f2h, '<', '$ARGV[$i]' or die "Cannot open InputFile.txt: ";
    open my $of2h, '>', 'OutFile.csv' or die "Cannot open Output File:";


    while (my $line = <$f2h>) {
    <Formatting exp here>



    }


    close $f2h;
    close $of2h;
    $b3 = chdir ("$copypath");
    }
    }


    thanks
    bala
     
    IJALAB, Nov 23, 2006
    #1
    1. Advertising

  2. IJALAB

    Keith Keller Guest

    On 2006-11-23, IJALAB <> wrote:
    >
    >
    > I am trying to take the files as command line arguments and do some
    > formatting to the files based on their extensions. I am getting error
    > in the file open when i use $ARGV[$i]


    See below.

    > Also, I am assigning a fixed name to my files. can i dynamically do
    > them based on my input file names?


    Of course. Parse the input file name for what you want, then
    use that parsed name as the name of the output file. Read perlreref
    and/or perlretut for help in this area.

    > print $#ARGV;


    use strict;
    use warnings;

    > $copypath = "<path>";
    > for ($i=0;$i<=$#ARGV;$i= $i + 1)


    This is a yucky-looking idiom. Better to use

    for my $filename (@ARGV)

    Then you can address each file as $filename in your for loop
    instead of as $ARGV[$i].

    > $b0 = system("copy $ARGV[$i] $copypath\\scripts");


    File::Copy is preferable to spawning a subshell.

    > $extn = substr($ARGV[$i],-4,4);


    Are all your extensions four characters? Mine aren't.

    > #Error here!!!
    > open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";


    If you read perldoc -f open, you'll notice a lot of references to
    the special variable $!, which is the error message that a system
    call returns. If you include it, then you will see exactly why
    your open is failing.

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    see X- headers for PGP signature information
     
    Keith Keller, Nov 23, 2006
    #2
    1. Advertising

  3. IJALAB

    Dan Mercer Guest

    "IJALAB" <> wrote in message news:...
    :
    :
    : I am trying to take the files as command line arguments and do some
    : formatting to the files based on their extensions. I am getting error
    : in the file open when i use $ARGV[$i]
    : Also, I am assigning a fixed name to my files. can i dynamically do
    : them based on my input file names?
    :
    : print $#ARGV;
    : $copypath = "<path>";
    : for ($i=0;$i<=$#ARGV;$i= $i + 1)
    : {
    :
    : $b0 = system("copy $ARGV[$i] $copypath\\scripts");
    : $b1 = chdir ("$copypath\\scripts");
    : $extn = substr($ARGV[$i],-4,4);
    : print $extn, "\n";
    : print $ARGV[$i];
    : $file = $ARGV[$i];
    : if ($extn == ".txt")
    : {
    :
    : #Error here!!!
    : open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";

    no variable substitution inside single quotes

    use strict;
    use warnings;

    Dan Mercer
    : open ($ofh, '>OutFile.txt') or die "Cannot open Output File:";
    :
    :
    : while (my $line = <$fh>) {
    : <Formatting here>
    :
    :
    : }
    :
    :
    : close $fh;
    : close $ofh;
    : $b3 = chdir ("$copypath");
    :
    : }
    :
    : elsif ($extn == ".csv")
    : {
    : #Error here!!!
    : open my $f2h, '<', '$ARGV[$i]' or die "Cannot open InputFile.txt: ";
    : open my $of2h, '>', 'OutFile.csv' or die "Cannot open Output File:";
    :
    :
    : while (my $line = <$f2h>) {
    : <Formatting exp here>
    :
    :
    :
    : }
    :
    :
    : close $f2h;
    : close $of2h;
    : $b3 = chdir ("$copypath");
    : }
    : }
    :
    :
    : thanks
    : bala
    :
     
    Dan Mercer, Nov 23, 2006
    #3
  4. IJALAB wrote:
    >
    > I am trying to take the files as command line arguments and do some
    > formatting to the files based on their extensions. I am getting error
    > in the file open when i use $ARGV[$i]
    > Also, I am assigning a fixed name to my files. can i dynamically do
    > them based on my input file names?


    To add to Dan's and Keith's comments.


    > print $#ARGV;
    > $copypath = "<path>";
    > for ($i=0;$i<=$#ARGV;$i= $i + 1)
    > {
    >
    > $b0 = system("copy $ARGV[$i] $copypath\\scripts");
    > $b1 = chdir ("$copypath\\scripts");
    > $extn = substr($ARGV[$i],-4,4);
    > print $extn, "\n";
    > print $ARGV[$i];
    > $file = $ARGV[$i];
    > if ($extn == ".txt")


    You are using a numerical comparison operator so the strings will be converted
    to numbers, and unless either string begins with numerical digits they will
    both be converted to 0, so that test will almost always be true no matter what
    $extn contains. Also Windows file names are case insensitive so you need to
    do something like this:

    if ( lc( $extn ) eq '.txt' )

    Or:

    if ( $extn =~ /\.txt\z/i )


    > {
    >


    [ snip ]

    > }
    >
    > elsif ($extn == ".csv")


    Same as above.




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Nov 23, 2006
    #4
  5. IJALAB

    Joe Smith Guest

    IJALAB wrote:
    >
    > I am trying to take the files as command line arguments and do some
    > formatting to the files based on their extensions. I am getting error
    > in the file open when i use $ARGV[$i]


    Don't use $ARGV[$i]. Use shift() or foreach() instead

    > Also, I am assigning a fixed name to my files. can i dynamically do
    > them based on my input file names?
    >
    > print $#ARGV;
    > $copypath = "<path>";
    > for ($i=0;$i<=$#ARGV;$i= $i + 1)


    foreach my $file (@ARGV) {
    # use proper indenting
    }

    > $b0 = system("copy $ARGV[$i] $copypath\\scripts");


    my $cmd = "copy $file $copypath\\scripts";
    print $cmd,"\n";
    (system $cmd) == 0 or warn "Command failed: $?";

    > $b1 = chdir ("$copypath\\scripts");


    Since you don't undo the chdir() later on, that will fail the second
    time if $copypath does not start with "\" or "X:\".

    > #Error here!!!
    > open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";


    Of course there is an error. '$file' is not the same as "$file" and
    you're using single quotes there. Better to use 3-argument open().

    open my $fh,'<',$file or die "Cannot open $file - $!";
     
    Joe Smith, Nov 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    4
    Views:
    534
    Gönen EREN
    Aug 22, 2003
  2. ml
    Replies:
    0
    Views:
    1,493
  3. ml
    Replies:
    2
    Views:
    4,900
    John C. Bollinger
    Nov 30, 2004
  4. Corne' Cornelius

    multiple open fd's on open(file)

    Corne' Cornelius, Aug 22, 2003, in forum: C Programming
    Replies:
    2
    Views:
    452
    -berlin.de
    Aug 22, 2003
  5. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    213
    Rick DeNatale
    Oct 24, 2006
Loading...

Share This Page