array question

Discussion in 'Perl Misc' started by Math55, Jun 26, 2003.

  1. Math55

    Math55 Guest

    hi, this is a piece of code from a program.

    ----
    my $size;
    my $realPath;
    my @filterArray;
    open READFILTEREDLIST,'</tmp/FILTEREDLIST' or die "Datei kann nicht
    geoeffnet werden: $!\n";

    while(<READFILTEREDLIST>) {

    @filterArray=<READFILTEREDLIST>;

    }
    close READFILTEREDLIST;

    open (WRITEPATHS, ">/tmp/FILTEREDPATHS") or die "Datei konnte nicht
    gefunden werden";
    print "open \n";
    foreach(@filterArray){

    print "array: $_\n"; #NOTHING!!!
    (my $datei, my $pfad)=fileparse($_);
    ($size, $realPath)=split('\s', $pfad);
    print "path: $realPath\n";
    print WRITEPATHS "$realPath\n";

    }
    close WRITEPATHS;

    ----

    the file FILTEREDLIST contains something like:

    132k /var/backups/dpkg.status.1.gz
    132k /var/backups/dpkg.status.2.gz
    124k /var/backups/dpkg.status.3.gz
    8.0k /var/log/auth.log.1.gz
    12k /var/log/auth.log.2.gz
    4.0k /var/log/auth.log.3.gz
    12k /var/log/daemon.log.1.gz
    12k /var/log/daemon.log.2.gz
    8.0k /var/log/daemon.log.3.gz
    8.0k /var/log/debug.1.gz
    12k /var/log/debug.2.gz
    4.0k /var/log/debug.3.gz
    8.0k /var/log/kern.log.1.gz
    4.0k /var/log/kern.log.2.gz
    8.0k /var/log/kern.log.3.gz
    8.0k /var/log/messages.1.gz
    8.0k /var/log/messages.2.gz
    8.0k /var/log/messages.3.gz
    4.0k /var/log/setuid.changes.1.gz

    but when i want to read that into the array, something does wrong. the
    array is always empty, do you see the mistake? the file is not
    empty...

    THANKS!!
     
    Math55, Jun 26, 2003
    #1
    1. Advertising

  2. (Math55) writes:

    > Subject: array question


    Please put the subject of your post in the Subject of your post. If
    in doubt try this simple test. Imagine you could have been bothered
    to have done a search before you posted. Next imagine you found a
    thread with your subject line. Would you have been able to recognise
    it as the same subject?

    If your subject truely refects your level of understanding of your
    problem (which appears to be a question about reading files) then you
    have not thought about your problem anywhere near enough thought to
    dream of asking anyone else for help.

    Math55, your poor subject lines have been pointed out to you before by
    a number of people. Do you realise how rude you are being by still
    not making any effort?

    > my @filterArray;
    > while(<READFILTEREDLIST>) {
    >
    > @filterArray=<READFILTEREDLIST>;
    >
    > }


    Where did you learn that construct?

    The above is an obfucated way of writing...

    scalar <READFILTEREDLIST>; # Discard first line
    my @filterArray=<READFILTEREDLIST>; # Read remainder of file into array

    > the file FILTEREDLIST contains something like:
    >
    > 132k /var/backups/dpkg.status.1.gz
    > 132k /var/backups/dpkg.status.2.gz
    > 124k /var/backups/dpkg.status.3.gz
    > 8.0k /var/log/auth.log.1.gz
    > 12k /var/log/auth.log.2.gz
    > 4.0k /var/log/auth.log.3.gz
    > 12k /var/log/daemon.log.1.gz
    > 12k /var/log/daemon.log.2.gz
    > 8.0k /var/log/daemon.log.3.gz
    > 8.0k /var/log/debug.1.gz
    > 12k /var/log/debug.2.gz
    > 4.0k /var/log/debug.3.gz
    > 8.0k /var/log/kern.log.1.gz
    > 4.0k /var/log/kern.log.2.gz
    > 8.0k /var/log/kern.log.3.gz
    > 8.0k /var/log/messages.1.gz
    > 8.0k /var/log/messages.2.gz
    > 8.0k /var/log/messages.3.gz
    > 4.0k /var/log/setuid.changes.1.gz
    >
    > but when i want to read that into the array, something does wrong. the
    > array is always empty, do you see the mistake?


    Are you sure the file actually contains more than one line (as
    determined by the EOL sequence for your OS)?

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Jun 26, 2003
    #2
    1. Advertising

  3. Math55

    Anno Siegel Guest

    Math55 <> wrote in comp.lang.perl.misc:
    > hi, this is a piece of code from a program.
    >
    > ----
    > my $size;
    > my $realPath;
    > my @filterArray;
    > open READFILTEREDLIST,'</tmp/FILTEREDLIST' or die "Datei kann nicht
    > geoeffnet werden: $!\n";
    >
    > while(<READFILTEREDLIST>) {


    This reads one line from the file into $_

    >
    > @filterArray=<READFILTEREDLIST>;


    This reads the rest of the lines into @filterArray.

    >
    > }
    > close READFILTEREDLIST;
    >
    > open (WRITEPATHS, ">/tmp/FILTEREDPATHS") or die "Datei konnte nicht
    > gefunden werden";
    > print "open \n";
    > foreach(@filterArray){
    >
    > print "array: $_\n"; #NOTHING!!!
    > (my $datei, my $pfad)=fileparse($_);

    ^^^^^^^^^
    The function "fileparse()" is nowhere defined. So the program exits
    the first time through the loop.

    > ($size, $realPath)=split('\s', $pfad);
    > print "path: $realPath\n";
    > print WRITEPATHS "$realPath\n";


    The variable "$realPath" is never assigned to. "$pfad" isn't even
    declared. If the program compiles for you, you are not using "strict",
    which is bad.

    >
    > }
    > close WRITEPATHS;
    >
    > ----
    >
    > the file FILTEREDLIST contains something like:
    >
    > 132k /var/backups/dpkg.status.1.gz
    > 132k /var/backups/dpkg.status.2.gz
    > 124k /var/backups/dpkg.status.3.gz


    [more data snipped]

    Your diagnosis is wrong. The file *is* read into the array (save for
    the first line, and after getting compile-time errors out of the way).
    I don't know why you think it isn't.

    To get the whole file, do away with the while-loop and just say

    @filterArray=<READFILTEREDLIST>;

    Anno
     
    Anno Siegel, Jun 26, 2003
    #3
  4. Math55

    Graham Wood Guest

    This is balderdash. Ignore me.

    Graham

    Graham Wood wrote:

    > > while(<READFILTEREDLIST>) {
    > >
    > > @filterArray=<READFILTEREDLIST>;
    > >
    > > }
    > > close READFILTEREDLIST;

    >
    > Here's where I think your problem lies.
    >
    > while(<READFILTEREDLIST>){ }
    > steps through each line of the file returning the contents in $_ each time
    >
    > @filterArray=<READFILTEREDLIST>;
    > reads the rest of the file contents into @filterArray.


    >
    > If you do both of these at the same time you will end up with nothing in
    > @filterArray because this is happening
    >
    > 1. While loop reads a line
    > 2. @filterArray gets all the rest of the lines
    > 3. While loop reads a line
    > 4. @filterArray gets the rest of the lines
    >
    > This will repeat until the while loop reads the last line and then
    > <READFILTEREDLIST> will return nothing to @filterArray then your script will
    > proceed with the lines after the while loop.
    >
    > You either need to use a while loop or @array=<FILEHANDLE> not both at the same
    > time.
    >
    > while(<READFILTEREDLIST>){
    > push(@filterArray,$_);
    > }
    >
    > OR
    >
    > @filterArray=<READFILTEREDLIST>;
    >
    > Hope this helps
    >
    > Graham
     
    Graham Wood, Jun 26, 2003
    #4
  5. On Thu, Jun 26, by accident I became aware that Graham Wood
    had caused the following to appear:

    Parts/Attachments:
    1 Shown 48 lines Text (charset: UTF-8)
    2 OK 8 lines Text (charset: UTF-8), "Card for Graham Wood"
    ----------------------------------------

    [ The following text is in the "UTF-8" character set. ]


    and had then inscribed on the eternal scroll:

    > This is balderdash. Ignore me.


    That's OK. Usenet postings with attachments normally get ignored by
    my newsreader. I had temporarily lifted the normal filters for a
    different reason, otherwise I'd never have seen those non-conformant
    postings.

    all the best
     
    Alan J. Flavell, Jun 26, 2003
    #5
  6. Math55 <> wrote:

    > while(<READFILTEREDLIST>) {
    >
    > @filterArray=<READFILTEREDLIST>;



    The while() reads the first line into the $_ variable, then
    you never do anything with that first line...


    > ($size, $realPath)=split('\s', $pfad);



    A pattern match should *look like* a pattern match.

    Space characters are not a scarce resource. Feel free to use as
    many as you like to make your code easier to read and understand:


    ($size, $realPath) = split( /\s/, $pfad );


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 26, 2003
    #6
  7. Alan J. Flavell <> wrote:
    > On Thu, Jun 26, by accident I became aware that Graham Wood
    > had caused the following to appear:


    >> This is balderdash. Ignore me.

    >
    > Usenet postings with attachments normally get ignored by
    > my newsreader.



    By mine too.

    Sometimes I think my scorefile is prescient. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 26, 2003
    #7
    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. Joseph Turian
    Replies:
    11
    Views:
    535
    Joseph Turian
    Jan 19, 2005
  2. Fabio
    Replies:
    0
    Views:
    327
    Fabio
    Nov 5, 2008
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    525
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    237
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    532
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page