Loading ...
Sorry, an error occurred while loading the content.

Test for existance

Expand Messages
  • Dukelow, Don
    I m trying to test for existence of one or more log files. If they don t exist I want to enter the while loop and do my thing. I know the log file exists in
    Message 1 of 9 , Jan 23, 2008
    • 0 Attachment
      I'm trying to test for existence of one or more log files. If they don't exist I want to enter the while loop and do my thing. I know the log file exists in this case there are three of them so I want to skip the loop but it can't see the files so it entries the loop. Which is what I don't want to do! What simple and dumb thing am I doing wrong.

      Use warnings;
      use strict;

      my $log_dir = '/var/adm/lms';

      print "\tWhat is log file name: ";
      chomp (my $log_file = (<>));
      while (! -e "$log_dir/$log_file*.log") {
      # Do my thing
      {

      -----
      Don Dukelow
    • Jeff Pinyan
      ... exist I want to enter the while loop and do my thing. I know the log file exists in this case there are three of them so I want to skip the loop but it
      Message 2 of 9 , Jan 23, 2008
      • 0 Attachment
        On Jan 23, 2008 12:57 PM, Dukelow, Don <dukelow@...> wrote:
        >
        > I'm trying to test for existence of one or more log files. If they don't
        exist I want to enter the while loop and do my thing. I know the log file
        exists in this case there are three of them so I want to skip the loop but
        it can't see the files so it entries the loop. Which is what I don't want to
        do! What simple and dumb thing am I doing wrong.
        >
        > Use warnings;
        > use strict;
        >
        > my $log_dir = '/var/adm/lms';
        >
        > print "\tWhat is log file name: ";
        > chomp (my $log_file = (<>));
        > while (! -e "$log_dir/$log_file*.log") {
        > # Do my thing
        > {

        You can't use wildcards with filetests like -e. You'll need to use glob()
        here instead:

        use warnings;
        use strict;

        my $log_dir = '/var/adm/lms';
        my $log_file;

        do {
        print "Enter logfile name: ";
        chomp($log_file = <STDIN>);
        } while () = glob "$log_dir/$log_file*.log");

        # if you get out here, that means
        # there are no files that match
        # $log_dir/$log_file*.log

        Ta da. That 'while () = ...' trick deserves some explaining.

        glob() behaves different in scalar vs. list context; we want the list
        context, where glob() will return all the files it finds that match the
        pattern we give it. However -- as far as I know -- you don't care what
        those files are called, you just want to know if any exist. So instead of
        storing the return value in an array or list of scalars, we dump the return
        value into an empty list. The BENEFIT of this is that a list assignment, in
        scalar context (which while requires) returns the number of elements from
        the right-hand side of the = sign.

        So if the glob() in list context returns 4 files, we discard the results
        (because we don't care about them) and simply return the NUMBER of results
        (4) to the while() condition.

        --
        [Mary said,] "Do whatever he tells you." ~ John 2:5


        [Non-text portions of this message have been removed]
      • Dukelow, Don
        Thank you this did work somewhat it is smaller and more compact then the way I was doing it. But in this case if the files exist I don t want to enter the do
        Message 3 of 9 , Jan 23, 2008
        • 0 Attachment
          Thank you this did work somewhat it is smaller and more compact then the way I was doing it. But in this case if the files exist I don't want to enter the "do while" loop. So where do I put the "!" sign to stay out of the loop if the files exist.


          -----
          Don Dukelow
          HP License Team
          Hewlett-Packard Company
          Tel: 810-728-3388
          e-mail: dukelow@...



          ________________________________
          From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com] On Behalf Of Jeff Pinyan
          Sent: Wednesday, January 23, 2008 2:30 PM
          To: perl-beginner@yahoogroups.com
          Subject: Re: [PBML] Test for existance


          On Jan 23, 2008 12:57 PM, Dukelow, Don <dukelow@...<mailto:dukelow%40hp.com>> wrote:
          >
          > I'm trying to test for existence of one or more log files. If they don't
          exist I want to enter the while loop and do my thing. I know the log file
          exists in this case there are three of them so I want to skip the loop but
          it can't see the files so it entries the loop. Which is what I don't want to
          do! What simple and dumb thing am I doing wrong.
          >
          > Use warnings;
          > use strict;
          >
          > my $log_dir = '/var/adm/lms';
          >
          > print "\tWhat is log file name: ";
          > chomp (my $log_file = (<>));
          > while (! -e "$log_dir/$log_file*.log") {
          > # Do my thing
          > {

          You can't use wildcards with filetests like -e. You'll need to use glob()
          here instead:

          use warnings;
          use strict;

          my $log_dir = '/var/adm/lms';
          my $log_file;

          do {
          print "Enter logfile name: ";
          chomp($log_file = <STDIN>);
          } while () = glob "$log_dir/$log_file*.log");

          # if you get out here, that means
          # there are no files that match
          # $log_dir/$log_file*.log

          Ta da. That 'while () = ...' trick deserves some explaining.

          glob() behaves different in scalar vs. list context; we want the list
          context, where glob() will return all the files it finds that match the
          pattern we give it. However -- as far as I know -- you don't care what
          those files are called, you just want to know if any exist. So instead of
          storing the return value in an array or list of scalars, we dump the return
          value into an empty list. The BENEFIT of this is that a list assignment, in
          scalar context (which while requires) returns the number of elements from
          the right-hand side of the = sign.

          So if the glob() in list context returns 4 files, we discard the results
          (because we don't care about them) and simply return the NUMBER of results
          (4) to the while() condition.

          --
          [Mary said,] "Do whatever he tells you." ~ John 2:5

          [Non-text portions of this message have been removed]





          [Non-text portions of this message have been removed]
        • Jeff Pinyan
          ... way I was doing it. But in this case if the files exist I don t want to enter the do while loop. So where do I put the ! sign to stay out of the loop
          Message 4 of 9 , Jan 23, 2008
          • 0 Attachment
            On Jan 23, 2008 4:12 PM, Dukelow, Don <dukelow@...> wrote:
            >
            >> do {
            >> print "Enter logfile name: ";
            >> chomp($log_file = <STDIN>);
            >> } while () = glob "$log_dir/$log_file*.log");
            >>
            >> # if you get out here, that means
            >> # there are no files that match
            >> # $log_dir/$log_file*.log
            >
            > Thank you this did work somewhat it is smaller and more compact then the
            way I was doing it. But in this case if the files exist I don't want to
            enter the "do while" loop. So where do I put the "!" sign to stay out of the
            loop if the files exist.

            Oh, I'd write it this way, then:

            my $log_dir = "...";
            print "Enter logfile name: ";
            chomp(my $log_file = <STDIN>);

            if (() = glob "$log_dir/$log_file*.log") {
            # files already exist
            }
            else {
            # no such files exist
            }

            --
            [Mary said,] "Do whatever he tells you." ~ John 2:5


            [Non-text portions of this message have been removed]
          • jiqiang yao
            Hi, I am extracting data from a file. I use $x to store the location of a sample. The location could be 0 (a valid value). However, sometimes there is no
            Message 5 of 9 , Jan 29, 2008
            • 0 Attachment
              Hi,

              I am extracting data from a file.

              I use $x to store the location of a sample.

              The location could be 0 (a valid value). However,
              sometimes there is no location record for a sample
              (NULL).

              Perl could not distinguish between 0 and NULL. It
              regards all 0 and NULL cases as NULL.

              How do your smart guys write codes to tell 0 from
              NULL?

              Thanks!





              ____________________________________________________________________________________
              Looking for last minute shopping deals?
              Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
            • merlyn@stonehenge.com
              ... jiqiang Hi, jiqiang I am extracting data from a file. jiqiang I use $x to store the location of a sample. jiqiang The location could be 0 (a valid
              Message 6 of 9 , Jan 29, 2008
              • 0 Attachment
                >>>>> "jiqiang" == jiqiang yao <jiqiang123@...> writes:

                jiqiang> Hi,
                jiqiang> I am extracting data from a file.

                jiqiang> I use $x to store the location of a sample.

                jiqiang> The location could be 0 (a valid value). However,
                jiqiang> sometimes there is no location record for a sample
                jiqiang> (NULL).

                How is "no data" represented in the file? Would Perl's "undef"
                work for that? Or even the absence of a key? You haven't said
                nearly enough about the problem, since "NULL" doesn't mean anything
                in the context of Perl.

                Show us the code you're using to parse, and some of the data.

                --
                Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
                See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
              • jiqiang yao
                This is a piece of data in the table ( sample ) of oracle database: position name 1 16721 2 16722 0 16723 16724 5 16725 Please
                Message 7 of 9 , Jan 30, 2008
                • 0 Attachment
                  This is a piece of data in the table ("sample") of
                  oracle database:

                  position name
                  1 16721
                  2 16722
                  0 16723
                  16724
                  5 16725


                  Please note that sample 16724 has no position (maybe
                  the technician made a mistake).

                  The following is my code:

                  ...
                  $get_sql = "select POSITION,SAMPLE_ID from
                  SAMPLE_RUN";
                  $sth_get = $dbh->prepare($get_sql);
                  $sth_get -> execute();

                  while(($POSITION,$SAMPLE_ID)=$sth_get->fetchrow_array
                  )
                  {
                  if( !$POSITION){$POSITION = "-1"; }
                  print "$POSITION\t$SAMPLE_ID\n";
                  }
                  ...

                  The result is that:

                  sample 16723 and 16724 both have position value of -1.




                  --- merlyn@... wrote:

                  > >>>>> "jiqiang" == jiqiang yao
                  > <jiqiang123@...> writes:
                  >
                  > jiqiang> Hi,
                  > jiqiang> I am extracting data from a file.
                  >
                  > jiqiang> I use $x to store the location of a sample.
                  >
                  > jiqiang> The location could be 0 (a valid value).
                  > However,
                  > jiqiang> sometimes there is no location record for a
                  > sample
                  > jiqiang> (NULL).
                  >
                  > How is "no data" represented in the file? Would
                  > Perl's "undef"
                  > work for that? Or even the absence of a key? You
                  > haven't said
                  > nearly enough about the problem, since "NULL"
                  > doesn't mean anything
                  > in the context of Perl.
                  >
                  > Show us the code you're using to parse, and some of
                  > the data.
                  >
                  > --
                  > Randal L. Schwartz - Stonehenge Consulting Services,
                  > Inc. - +1 503 777 0095
                  > <merlyn@...>
                  > <URL:http://www.stonehenge.com/merlyn/>
                  > Perl/Unix/security consulting, Technical writing,
                  > Comedy, etc. etc.
                  > See PerlTraining.Stonehenge.com for onsite and
                  > open-enrollment Perl training!
                  >



                  ____________________________________________________________________________________
                  Be a better friend, newshound, and
                  know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
                • Jeff Pinyan
                  ... The problem is that you re using the wrong kind of test. The values 0, (the empty string), and undef are all false. I can t be sure whether your
                  Message 8 of 9 , Jan 30, 2008
                  • 0 Attachment
                    On Jan 30, 2008 2:00 PM, jiqiang yao <jiqiang123@...> wrote:
                    >
                    > This is a piece of data in the table ("sample") of
                    > oracle database:
                    >
                    > position name
                    > 1 16721
                    > 2 16722
                    > 0 16723
                    > 16724
                    > 5 16725
                    >
                    > $get_sql = "select POSITION,SAMPLE_ID from
                    > SAMPLE_RUN";
                    > $sth_get = $dbh->prepare($get_sql);
                    > $sth_get -> execute();
                    >
                    > while(($POSITION,$SAMPLE_ID)=$sth_get->fetchrow_array
                    > )
                    > {
                    > if( !$POSITION){$POSITION = "-1"; }
                    > print "$POSITION\t$SAMPLE_ID\n";
                    > }

                    The problem is that you're using the wrong kind of test. The values
                    0, "" (the empty string), and undef are all false. I can't be sure
                    whether your database is returning undef or the empty string. If you
                    have warnings turned on, you'd get a warning when you print
                    $POSITION's value (before you set it to -1, of course).

                    Here's how I'd write the program if the empty value is really undef
                    (NULL in SQL-speak):

                    while (...) {
                    $POSITION = 1 if not defined $POSITION;
                    ...
                    }

                    Here's how I'd write the program if the empty value is really the empty string:

                    while (...) {
                    $POSITION = -1 if $POSITION eq "";
                    ...
                    }

                    --
                    [Mary said,] "Do whatever he tells you." ~ John 2:5
                    The Cross Reference - http://thecrossreference.blogspot.com/
                    Nos autem praedicamus Christum crucifixum (1 Cor 1:23)
                  • jiqiang yao
                    Hi, I have an oracle database that stores sample_id as BINARY, 16 Bytes. Whoever knows how to translate this binary data to ascii character with perl? Thanks!
                    Message 9 of 9 , Feb 13, 2008
                    • 0 Attachment
                      Hi,

                      I have an oracle database that stores sample_id as
                      BINARY, 16 Bytes.

                      Whoever knows how to translate this binary data to
                      ascii character with perl?

                      Thanks!


                      ____________________________________________________________________________________
                      Looking for last minute shopping deals?
                      Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
                    Your message has been successfully submitted and would be delivered to recipients shortly.