[18239] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 407 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 3 14:10:31 2001

Date: Sat, 3 Mar 2001 11:10:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <983646613-v10-i407@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Sat, 3 Mar 2001     Volume: 10 Number: 407

Today's topics:
        sorting a hash <rbbdsb@earthlink.net>
    Re: sorting a hash (Tad McClellan)
    Re: sorting a hash <nouser@emailunwelcome.com>
        tagged-0.30 (MP3::Tag) released <thomasg@geffert.com>
    Re: unlink problems (Tad McClellan)
    Re: Why does yahoo handle Header request's diffrent? <iltzu@sci.invalid>
    Re: Why doesnt it work. The whole list deletes. <thelma@alpha2.csd.uwm.edu>
    Re: Why doesnt it work. The whole list deletes. (John Joseph Trammell)
    Re: Why doesnt it work. The whole list deletes. <kalle@tvettsvamp.a.se>
    Re: Why doesnt it work. The whole list deletes. <flavell@mail.cern.ch>
    Re: Why doesnt it work. The whole list deletes. <godzilla@stomp.stomp.tokyo>
    Re: Why doesnt it work. The whole list deletes. <billk@cts.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 03 Mar 2001 15:15:26 GMT
From: "Russell Brooks" <rbbdsb@earthlink.net>
Subject: sorting a hash
Message-Id: <iU7o6.11036$%4.1194662@newsread1.prod.itd.earthlink.net>

Hi,
I am reading values into a hash.  I'm trying to pull the top ten values
based on the sort of one of the fields.  In the sample below, I've stripped
out most of the fields leaving something like:

open(file, $file) || die "open $file failed";
while {<file>){
  my ($program, $table, $count, $time) = split(/\|/);
  $program_array{$program}[0] = $table;
  $program_array{$program}[1] = $count;
  $program_array{$program}[2] = $time;
}
# what I need to do is sort it based on $time which is the number of
seconds, not a date time field
foreach $program_array(sort { $program[2]{$a} <=> $program[2]{$b} } )
  keys %program) {
    print "$program $table $count $time\n";
 }

which obviously doesn't work.
Can anyone point me the right direction?

Thanks





------------------------------

Date: Sat, 03 Mar 2001 17:39:01 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: sorting a hash
Message-Id: <slrn9a25gi.q7i.tadmc@tadmc26.august.net>

Russell Brooks <rbbdsb@earthlink.net> wrote:
>Hi,
>I am reading values into a hash.  I'm trying to pull the top ten values
>based on the sort of one of the fields.  In the sample below, I've stripped
>out most of the fields leaving something like:
                                ^^^^^^^^^^^^^^

"something like" is worthless when you are discussing computer code.

A stray character can make all the difference.

We need to see *real* Perl code. We aren't seeing that in your post.


>open(file, $file) || die "open $file failed";


You should use UPPERCASE filehandles.

You should include the $! special variable in your die() message.


>while {<file>){
       ^
       ^ syntax error


It is now apparent that this is not the real code.

Stop reading.

You are on your own, since you are the only one with access to the
code that needs debugging.


># what I need to do is sort it based on $time which is the number of
>seconds, not a date time field


We do not know what the format for your dates are, so we cannot
help you convert them to another format.

I would look into using one of the Date::* modules from CPAN
instead of trying to work it out all over again.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Sat, 03 Mar 2001 12:42:32 -0500
From: Jay Tilton <nouser@emailunwelcome.com>
Subject: Re: sorting a hash
Message-Id: <tt92atgebd5scuoufm481el4o1k2tbkoun@4ax.com>

"Russell Brooks" <rbbdsb@earthlink.net> wrote:

>In the sample below, I've stripped
>out most of the fields leaving something like:

Code to be posted should at least execute.  This has syntax errors.
Trivial ones in this case, but still annoying.

>foreach $program_array (sort { $program[2]{$a} <=> $program[2]{$b} } )
>  keys %program) {
>    print "$program $table $count $time\n";
> }

Everything in those lines is backwards, but the intended meaning is
not totally lost.  Try dissecting it.

>foreach $program_array
Iterates over the list to be generated, storing the iteration value in
the scalar $program_array.  This is surely not what you want.

>(sort { $program[2]{$a} <=> $program[2]{$b} } )
Sorts the hash referenced by $program[2].  There is no array named
@program, let alone a hash reference in its third element.
There exists, however, a hash named %program_array with array
references in its values.
You have confused...
  $array_name[array index]{key to referenced hash}
with...
  $hash_name{hash key}[index within referenced array]

>keys %program
Generates a list of the keys to the hash named %program.
Your hash is named %program_array.

>print "$program $table $count $time\n";
No values have been assigned to these scalars recently.  They will
contain whatever values were left when the file was being read.

See if this form of the code works as you intended.

use strict;
#always a good idea when creating and debugging
my $file = 'testdata.txt';
#Or whatever value you want, and however you want to assign it.
my %program_array;
open(file, $file) || die "open $file failed: $!";
#You'll probably want to know why            ^^
while (<file>){
    chomp $_; #Unless you really want to keep the record separators.
    my ($program, $table, $count, $time) = split(/\|/);
    $program_array{$program} = [$table, $count, $time];
    #One line does the work of three.
}

foreach my $program (
    sort { $program_array{$a}[2] <=> $program_array{$b}[2] }
    keys %program_array
) {
    my ($table, $count, $time) = @{$program_array{$program}};
    #Dereference the array.      ^^                        ^
    print "$program $table $count $time\n";
}


------------------------------

Date: 01 Mar 2001 18:54:35 +0100
From: Thomas Geffert <thomasg@geffert.com>
Subject: tagged-0.30 (MP3::Tag) released
Message-Id: <ta23vjmopc4276@corp.supernews.com>

I want to announce the newest release of tagged/ MP3::Tag.

This is a module for reading/writing MP3 tags and contains
also a graphical Tk interface for manipulating ID3 tags.

The module can be found at:

http://search.cpan.org/search?dist=tagged
or
http://tagged.sourceforge.net

If you have any comments or question, feel free to send me an email.

  Thomas
  <thg@users.sourceforge.net>
 

Release Name: tagged-0.30
=========================

This version contains a bugfix for MP3::Tag, and some
new functions for MP3::Tag, MP3::Tag::ID3v1 and tk-tag.pl .

Changes:
========
* Tag.pm / Tag::File.pm
  - All file access routines are collected in an extra modul now.
    This prevents circular references between Tag.pm and the ID3v..pm
    modules. These circular references prevented Perl from calling
    the destructor for mp3-objects.
* Tag.pm
  - autoinfo() function added. This returns artist/songtitle/track/album.
    It tries to find this information in an ID3v1 or ID3v2 tag or tries 
    to extract it from the filename. The order in which this happens
    can be configured with the new config() function.
* ID3v2.pm
  - four new functions: artist(), song(), track() and album(). These
    are included for compability with the ID3v1 and filename module.
* tk-tag
  - Loading/Saving of binary data in frame-fields is supported now
  - tk-tag uses now the new what_data functionally of ID3v2 to offer 
    BrowseEntrys for TCON and TFLT and also for parts of APIC and COMR
  - Set Filename uses now the actual contents of the Tags not the old 
    saved one
  - Set Filename from ID3v2-Tag works now at least with Artist (%a),
    Album(%l) and Song (%s)
* ID3v2.pm
    what_data() returns now also information about possible restricted
    input for some frame fields (APIC; TCON; COMR; TFLT are supported yet).




------------------------------

Date: Sat, 3 Mar 2001 10:50:49 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: unlink problems
Message-Id: <slrn9a24mp.q4o.tadmc@tadmc26.august.net>

Ken Brown <Ken.Brown@DIALWRAP.COM> wrote:
>Requirement delete *.txt in a given directory
>
>Code
>
>push @DelFiles, $LogDir."\\*.txt";
>DiskCleanup();
>
>sub DiskCleanup
>{
>	foreach $i (0 .. $#DelFiles)
>	{
>		print "@DelFiles[$i]\n";
>		$FailedUnlink = grep {not unlink} @DelFiles[$i];
                                                  ^
                                                  ^

You should enable warnings on ALL of your Perl programs. Then fix
your program so that it does not generate any warnings.


># pinch from programming PERL
              ^^^^^^^^^^^^^^^^


Where in "Programming Perl" (note the proper spelling)?

What you have there is NOT from "Programming Perl".
Your code is using grep() in a scalar context.
The Camel uses grep() in a list context.
Not the same thing at all...

If you do not need to report _which_ files failed, you can just
do this for the same effect:

   unlink($DelFiles[$i]) or 
      die "could not unlink one of: $DelFiles[$i]  $!";

Avoiding the temporary variable and the grep()ing.

But that still has the unexpanded glob pattern problem. Fix that:

   unlink(glob $DelFiles[$i]) or 
          ^^^^
      die "could not unlink one of: $DelFiles[$i]  $!";


>		die "$0: could not unlink @DelFiles[$i]\n" if
                                          ^
                                          ^ yet another warning!
>$FailedUnlink;


You should include the $! special variable in your die() message.

You should probably not have a newline at the end of your die() message,
then you can leave out the $0.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: 3 Mar 2001 17:21:21 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: Why does yahoo handle Header request's diffrent?
Message-Id: <983639429.12201@itz.pp.sci.fi>

In article <b75o6.4806$mt.569806@news2-win.server.ntlworld.com>, Chile wrote:
>
>I am writing a prog to check link status and it works on every link bar
>yahoo.com's
>
>basically i just return the header of the url, file etc.. and with urls like
>www.google.com, www.hotbot.com etc.. it return's
>
>but when i try it on www.yahoo.com it always return the whole source of
>yahoo.com along with the header at the top??

Well, you've been lucky so far -- in my experience a significant
fraction of dynamic websites don't understand what a HEAD request is,
and will treat it as if it were GET.  A few even refuse HEAD requests
entirely.  Both are violations of RFC 2616.

The solution in the first case is just to ignore the body.  You could
always complain, but I doubt it'll do much good.  In the second case,
you may wish to issue a GET request if the HEAD request comes back with
a 405 or 501 error.  In that case I definitely *would* complain.

-- 
Ilmari Karonen - http://www.sci.fi/~iltzu/
"Ahh, 'interacts'... don't you just love carefully neutral terms like that
 to describe catastrophic orbital evolution?"   -- Brian Davis in r.a.sf.s

Please ignore Godzilla / Kira -- do not feed the troll.


------------------------------

Date: 3 Mar 2001 14:53:40 GMT
From: Thelma Lubkin <thelma@alpha2.csd.uwm.edu>
Subject: Re: Why doesnt it work. The whole list deletes.
Message-Id: <97r0hk$o27$1@uwm.edu>

Kalle Anka <kalle@tvettsvamp.a.se> wrote:
: I'm trying to delete one e-mail address using a form to tell the script what
: to do.

: <form action="../cgi-tvett/unsub.pl" method="POST">
:     <p>Email Address: <input type="text" size="20" name="email">
:     </p>
: <input type="submit" value="Skicka">
: </form>

: Instead of remove one e-mail address, the whole list deletes.

: I'm not so good at perl, so if someone will explain, please do well.

: Kalle


: #!/usr/bin/perl

: $filename = "/home/tvett/www/mail.htm";
: $backupfile = "/home/tvettwww/backup.htm";



: use CGI qw(:standard);

: $q = CGI->new();

: print $q->header();
: print $q->start_html('Resultat av kommando');
: print $q->h1('Resultat av kommando');
: print(&cleanup); .
: print $q->end_html();

: sub cleanup {
:     open INFILE, $filename
: or return "Couldn't open file for reading";

:     @infile = <INFILE>;

:     close INFILE;

:     open BACKUP, ">$backupfile"
: or return "Couldn't make backup of original file";

:     if (@infile) { for $line (@infile) {
:     print BACKUP $line;
: }
:     }

:     open OUFILE, ">$filename"
: or return "Couldn't open file for writing";

:     for $line (@infile) {
: print OUTFILE $line unless ($line =~/$email/);
:     }

:     close OUTFILE;

:     return "The address has been removed";
: }

          Where do you set $email to the line to delete?

          You open 'OUFILE' but write to 'OUTFILE', so your write
          will fail.
                                  --thelma





------------------------------

Date: Sat, 03 Mar 2001 16:52:14 GMT
From: trammell@bayazid.hypersloth.net (John Joseph Trammell)
Subject: Re: Why doesnt it work. The whole list deletes.
Message-Id: <slrn9a2639.6ig.trammell@bayazid.hypersloth.net>

On Sat, 03 Mar 2001 13:04:11 GMT, Kalle Anka <kalle@tvettsvamp.a.se> wrote:
[snip]
>     for $line (@infile) {
> print OUTFILE $line unless ($line =~/$email/);
>     }
[snip]

1. Try 'perl -w' and 'use strict;'.
2. Where does $email get defined?
3. What happens if I say my email address is "a"?  All
   addresses with an 'a' in them get removed -- probably
   not what you wanted...



------------------------------

Date: Sat, 03 Mar 2001 18:18:39 GMT
From: "Kalle Anka" <kalle@tvettsvamp.a.se>
Subject: Re: Why doesnt it work. The whole list deletes.
Message-Id: <3Aao6.21450$AH6.2990515@newsc.telia.net>

Same result! The whole list is deleted.
After I wrote "use strict;"  ,   = Internal server error

Kalle


#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);

$filename = "/home/markisspecialisten/www/guestmail.htm";
$backupfile = "/home/markisspecialisten/www/backup.html";
$email = param('address');

$q = CGI->new();

print $q->header();
print $q->start_html('Resultat av kommando');
print $q->h1('Resultat av kommando');
print(&cleanup); # Kjør sub og skriv returnert verdi.
print $q->end_html();

sub cleanup {
    open INFILE, $filename
or return "Couldn't open file for reading";

    @infile = <INFILE>;

    close INFILE;

    open BACKUP, ">$backupfile"
or return "Couldn't make backup of original file";

    if (@infile) { # Sjekk om den er tom
for $line (@infile) {
    print BACKUP $line;
}
    }

    open OUTFILE, ">$filename"
or return "Couldn't open file for writing";

    for $line (@infile) {
print OUTFILE $line unless ($line =~/$email/);
    }

    close OUTFILE;

    return "The address has been removed";
}




------------------------------

Date: Sat, 3 Mar 2001 19:38:22 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Why doesnt it work. The whole list deletes.
Message-Id: <Pine.LNX.4.30.0103031933270.11025-100000@lxplus003.cern.ch>

On Sat, 3 Mar 2001, Kalle Anka blurted out:

> After I wrote "use strict;"  ,   = Internal server error

That's too easy.  When you debug it from the command line, I expect
it will be obvious what's wrong.  (The problem _should_ also be
evident in the server logs).

"use strict" isn't supposed to solve or hide problems: it's supposed
to expose them so that you can fix them.  Normally, get those bugs
ironed out on the lab bench, _before_ you try to run the thing from a
web server.

> use CGI qw(:standard);

Good idea.  This makes command-line debugging pretty easy.




------------------------------

Date: Sat, 03 Mar 2001 10:46:01 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Why doesnt it work. The whole list deletes.
Message-Id: <3AA13BE9.2E41A4C@stomp.stomp.tokyo>

Kalle Anka wrote:
 
> Same result! The whole list is deleted.
> After I wrote "use strict;"  ,   = Internal server error

(snipped)

Do yourself a favor and don't post a different
program with each of your articles. This is
not exceptionally intelligent. Posting a corrected
program is fine. Posting a radically different
program and implying it is the same, this causes
me to question your intent, especially when your
original first posted program does nothing at all.

Godzilla!


------------------------------

Date: Sat, 03 Mar 2001 19:03:29 GMT
From: "Bill Kelly" <billk@cts.com>
Subject: Re: Why doesnt it work. The whole list deletes.
Message-Id: <5ebo6.103141$GV2.23434858@typhoon.san.rr.com>


"Kalle Anka" <kalle@tvettsvamp.a.se> wrote:
> Same result! The whole list is deleted.
> After I wrote "use strict;"  ,   = Internal server error

Do you have Perl on your own computer, or are you at present
only able to run your program by uploading it to the web server?

I'd recommend getting the part of the program that deletes a line
from the file working locally first, if you can.  (That is, if
you have Perl installed locally.  If not, maybe you should consider
that?)

You can try it locally, and you don't get mysteries like "internal
server error" - instead you'll get Perl telling you what it didn't
like about the code.  For instance, having added "use strict;" to
your program, what Perl will be trying to tell you is:

> #!/usr/bin/perl -w
>
> use strict;
> use CGI qw(:standard);
>
> $filename = "/home/markisspecialisten/www/guestmail.htm";

Global symbol "$filename" requires explicit package name
at yourprogram.pl, line 6.

See?  . . . A little nicer than "internal server error".  :-)


Also, something I find useful in debugging CGI scripts running
on the server is to add a line like the following at the beginning
of your script:

BEGIN { open (STDERR, ">>myprog-log") }

In your case, that might be:

BEGIN { open (STDERR, ">>/home/markisspecialisten/www/guestmail.log") }

That way, when you get an "internal server error", point your
browser at the log file, and see what Perl is trying to tell you.

Hope this helps.

(By the way, is this a homework assignment of some kind?  See also the
thread "How can I search and delete a specific name in a textfile."
by "Fabian Thorbjörnsson", discussing the same problem.)

Bill





------------------------------

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 407
**************************************


home help back first fref pref prev next nref lref last post