[17748] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5168 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 21 03:10:43 2000

Date: Thu, 21 Dec 2000 00:10:16 -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: <977386216-v9-i5168@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 21 Dec 2000     Volume: 9 Number: 5168

Today's topics:
    Re: Simple Pattern Problem (Tad McClellan)
    Re: Simple Pattern Problem (Garry Williams)
    Re: SSI in perl output <cleon42@my-deja.com>
    Re: SSI in perl output <ng@fnmail.com>
    Re: switch/case in Perl? <peter.sundstrom@eds.com>
    Re: switch/case in Perl? (Tad McClellan)
    Re: switch/case in Perl? (Alan Barclay)
    Re: switch/case in Perl? (Logan Shaw)
    Re: switch/case in Perl? <anmcguire@my-deja.com>
        trying to count words - not working fhinchey@my-deja.com
    Re: Understanding interpolation <nospam@nospam.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 20 Dec 2000 21:32:20 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Simple Pattern Problem
Message-Id: <slrn942qtk.3r2.tadmc@magna.metronet.com>

Stephen Hargrove <stephen@virtual-attorney.com> wrote:

> Subject: Simple Pattern Problem
                          ^^^^^^^

You have a whole boatload of problems below.

Your primary problem has nothing to do with pattern matching.
It has to do with the return value of one of the functions
that you are using.

You should read the documentation for the functions that you use.


>i think i'm finally going blind, because i have a very simple pattern
>matching problem 


Sounds like you were looking in the wrong place (at the pattern).

That is only half of what is needed to evaluate a pattern match.


>and i can't seem to locate the error. 


The other half is the string that is to be matched against. That
is where your problem is.


>following program should recursively traverse a directory structure.


There are Perl modules for doing that you know.


>can someone PLEASE show me where i'm missing the boat on this.
     ^^^^^^^

   perldoc -f chop

can.


But don't use chop() for removing line endings. Use chomp() for that.


>#!/usr/bin/perl

   #!/usr/bin/perl -w
   use strict;

Leaving those out is usually enough to make me stop reading immediately,
I find it demeaning to do a machine's work. But I'm bored tonight.


>open (INF, ">/home/Stephen/perl/output");


You should always, yes *always*, check the return value from open():

   open (INF, ">/home/Stephen/perl/output") || 
      die "could not open '/home/Stephen/perl/output'  $!";


>sub processSong {
>        $Test = $TreeDir . '/' . $Mp3;


Most people think that interpolating is easier to read:

   $Test = "$TreeDir/$Mp3";


>        $tag = get_mp3tag($Test);
>        $x = 0;
>        for (keys %$tag) {
>                $x++;


Uh oh. Why the counter?

Are you depending on the return value from keys() to be in some
particular order? 

Looks that way to me. You cannot depend on that, hashes are 
(apparently) unordered.


>                        printf SQL "Album: %s\n", $tag->{$_};
>                        printf SQL "Year: %s\n", $tag->{$_};
>                        printf SQL "Type: %s\n", $tag->{$_};
>                        printf SQL "Song: %s\n", $tag->{$_};
>                        printf SQL "Rip: %s\n", $tag->{$_};
>                        printf SQL "Song No.: %s\n", $tag->{$_};
>                        printf SQL "Group: %s\n", $tag->{$_};


Repeating patterns like that should start bells ringing that
there is probably a Better Way...


>        $Mp3 = chop($Song);


print "Mp3 is '$Mp3'\n";  # lets see what it looks like


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


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

Date: Thu, 21 Dec 2000 03:51:09 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: Simple Pattern Problem
Message-Id: <N6f06.575$Kk5.31202@eagle.america.net>

On Wed, 20 Dec 2000 18:29:36 -0600, Stephen Hargrove
<stephen@virtual-attorney.com> wrote:
>the following program should recursively traverse a directory
>structure.  when i locates a file ending in .mp3, it should branch to
>the subroutine and process the song with the MPEG::MP3Info module.
>Following the source code is a sample listing of the output.  As you
>can see, it's failing on the pattern match for .mp3 and is
>considering everything a directory.
>
>#!/usr/bin/perl

 #!/usr/bin/perl -w
 use strict;

>use MPEG::MP3Info;
>
>$rootDir= "/music/mp3/";
>
>open (INF, ">/home/Stephen/perl/output");
>open (SQL, ">/home/Stephen/perl/insert.sql");

 open(INF, ">/home/Stephen/perl/output")
   || die "can't open /home/Stephen/perl/output: $!";
 open(SQL, ">/home/Stephen/perl/insert.sql")
   || die "can't open /home/Stephen/perl/insert.sql: $!";

Always check the result of open() and always print the reason when it
fails.  

[snip]

>@songList = `ls -R $rootDir`;
>foreach $Song (@songList) {
>        next if $Song =~ /^\./;   # skip . files (. .. .bashrc etc)
>        next if $Song =~ /m3u/;   # skip m3u files
>        $Mp3 = chop($Song);

In the chop() section of the perlfunc manual page: 

    Chops off the last character of a string and returns the character
    chopped. 

So, based on the manual, $Mp3 now contains the string "\n".  It cannot
be matched by /\.mp3/i.  

You probably meant to say: 

	chop $Song;
	$Mp3 = $Song;

or 

	chop($Mp3 = $Song);

(You should also use chomp() for this instead of chop().  See perlfunc
for why.  In this case, it's not important, but in general it's
safer.)  

-- 
Garry Williams


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

Date: Thu, 21 Dec 2000 04:09:43 GMT
From: Adam Levenstein <cleon42@my-deja.com>
Subject: Re: SSI in perl output
Message-Id: <91rvq4$apr$1@nnrp1.deja.com>



Well, no, mainly because you're already generating the code dynamically.

The real question is, why would you want to? I'm willing to bet there's
an easier way to accomplish what you're looking to do.

In article <91rj10$le6$1@info1.fnal.gov>,
  "Enrico Ng" <ng@fnmail.com> wrote:
> Is there a way to put a SSI include directive into the output of a
perl
> script and have the server interpret it as a shtml?
>
> --
> Enrico Ng <ng@fnmail.com>
>
>

--
-------------------------------------------------
Adam Levenstein
cleon42@my-deja.com

"Extraordinary claims require extraordinary evidence."
				-- Carl Sagan


Sent via Deja.com
http://www.deja.com/


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

Date: Thu, 21 Dec 2000 00:02:54 -0600
From: "Enrico Ng" <ng@fnmail.com>
Subject: Re: SSI in perl output
Message-Id: <91s60f$24b$1@info1.fnal.gov>

yeah I guess there is an easier way.
I was thinking of adding a "footer" the the bottom of all my pages.
stuff like that

--
Enrico Ng <ng@fnmail.com>
"Adam Levenstein" <cleon42@my-deja.com> wrote in message
news:91rvq4$apr$1@nnrp1.deja.com...
>
>
> Well, no, mainly because you're already generating the code dynamically.
>
> The real question is, why would you want to? I'm willing to bet there's
> an easier way to accomplish what you're looking to do.
>
> In article <91rj10$le6$1@info1.fnal.gov>,
>   "Enrico Ng" <ng@fnmail.com> wrote:
> > Is there a way to put a SSI include directive into the output of a
> perl
> > script and have the server interpret it as a shtml?
> >
> > --
> > Enrico Ng <ng@fnmail.com>
> >
> >
>
> --
> -------------------------------------------------
> Adam Levenstein
> cleon42@my-deja.com
>
> "Extraordinary claims require extraordinary evidence."
> -- Carl Sagan
>
>
> Sent via Deja.com
> http://www.deja.com/




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

Date: Thu, 21 Dec 2000 14:09:11 +1300
From: "Peter Sundstrom" <peter.sundstrom@eds.com>
Subject: Re: switch/case in Perl?
Message-Id: <91rl88$4a8$1@hermes.nz.eds.com>


Adam Levenstein <cleon42@my-deja.com> wrote in message
news:91rfor$ucr$1@nnrp1.deja.com...
> Here's a suggestion: Why not re-work the FAQ to make things easier to
> find? A search function would be nice, for example. "parts 0 1 2 3 4 5
> 6 7 8 9" is fairly indescriptive.

You haven't found 'perldoc -q' ?

perldoc -q switch
perldoc -q case

both find the relevant part from the FAQ




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

Date: Wed, 20 Dec 2000 20:55:53 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: switch/case in Perl?
Message-Id: <slrn942op9.3r2.tadmc@magna.metronet.com>


[ Please do not send stealth Cc's. That is bad manners too! ]


Adam Levenstein <cleon42@my-deja.com> wrote:
>In article <slrn9424un.3cd.tadmc@magna.metronet.com>,
>  tadmc@metronet.com (Tad McClellan) wrote:
>
>> And now that you have displayed the attitude that it is OK to
>> re-ask FAQs, the chances of your getting help with future Perl
>> problems has been diminished. I hope your venting was worth
>> the trade-off you have made.
>
>As opposed to displaying the attitude that only people who find what
>they're looking for the in FAQ (I did look, believe it or not - and I
>bet "not") are worthy to post without rude response?
 ^^^^^^^^^


Right. So it was all a misunderstanding then.

You used the very search term itself, "switch".

If you had said "case statement", then I wouldn't have lit off
on you, because you might not have used the right term.

As far as I could tell, you *did not* check the FAQ before posting,
because it is damn hard to miss when you have the right term,
and you displayed that you had it.


>Obviously the chances of my getting help weren't diminished, or I
>wouldn't have received several polite answers. 


I'm wasn't talking about the answer to _this_ question. I was 
talking about all of your *future* Perl questions. FAQ askers 
get killfiled. Many (most?) of the frequent-answerers here
must have killfiles to deal with the volume. Getting your
own entry there will have a negative impact on getting
answers to Perl questions in the future.


People who are new to newsgroups often don't even know that
they are hurting themselves. I thought you might like to know
that that goes on since you are obviously new to newsgroups too.


>As for venting - well,
>yeah, I guess to you, only I was venting - not the guy who went on the
>rant in the first place about kilfiling my-deja.com.


No, he was venting too. But you two are not in the same league
at all. See below.


>Here's a suggestion: Why not re-work the FAQ 


Errr, you are talking to the wrong person for that.

"the guy" you refer to is the author of the bulk of the Perl FAQ,
you should address such issues to him.

Better yet, rather than have him do it, why don't you volunteer
to do it? It would be much more likely to get done that way.

Making work for yourself is up to you. Making work for others
is not up to you.


You see, he was on the newsgroups for several years. The "switch"
question comes up a couple of times a month here. So after a few
years (let's call it four) he had seen the question a hundred
times.

He thought it would be really nice to have the answers to these
recurring type questions bottled up in one place. So he (with
some help) wrote the Perl FAQ. 

It is big. 

It took him a long time, representing a whole lot of work. 

But he didn't mind, because having these answers readily available 
would help lots of his fellow Perl programmers. If they would read it.

Then, for a few *more* years, we watched as the same questions
continued to be asked. Say another hundred times.  Now he has 
seen the same question about two hundred times.

But *all* those questions were a waste of time, drawing the
question-answering type posters away from answering the
other Perl programmer's questions!

FAQ asking hurts your peers in this community. It is selfish.

Why are they ignoring all of the hard work he has done?

Over and over and over again!

Week after week. Year after year.

Then you posted. 

Pop!



He has earned the right to vent at FAQ asking.

You've been here, what? A few weeks. And you already know
enough about How It Is to suggest how it should be?

Get real.


>to make things easier to
>find? A search function would be nice, 


There *is* a search function!    (but Tom doesn't like it)


>for example. "parts 0 1 2 3 4 5
>6 7 8 9" is fairly indescriptive.


   perldoc -q switch


Brings up the text of the question and answer! 

Try it.

That's pretty darn easy, hard to see how you could possibly miss 
with that.

And everybody expects that Perl programmers know about the
programs that are installed along with the perl interpreter...

 ... so we (or I, at least) were quite sure that you did not
make the least little bit of effort to find the answer yourself.

We see now that you just missed it. But now is not then. We were
acting on the only information available to us. 


>Or, on the other hand, you could just "plonk" everyone who takes
>offense at Mr. Manners' responses.


I already do that, but thanks for the suggestion.

(about half of my kills are "sympathetic" where I was not involved
 in the thread that led to the killing.
)

It isn't much of an impact whether or not you make _my_ killfile,
there are lots of other people here that can answer your future
questions.

The big impact would be if I am typical, and you have landed in
multiple killfiles. I've been around here for quite a long time,
I'm pretty sure that you are not in just one killfile...


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


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

Date: 21 Dec 2000 04:55:46 GMT
From: gorilla@elaine.furryape.com (Alan Barclay)
Subject: Re: switch/case in Perl?
Message-Id: <977374536.95253@elaine.furryape.com>

In article <91r27e$i1t$1@nnrp1.deja.com>,
Adam Levenstein  <cleon42@my-deja.com> wrote:
>Thanks Bart...Just trying to keep the code short.
>
>And yeah - I never understood why Richie put the "break" in there.
>Seemed unnecessary to me.
>

So Tom Duff could invent Duff's device.


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

Date: 20 Dec 2000 23:51:42 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: switch/case in Perl?
Message-Id: <91s5pe$ftq$1@boomer.cs.utexas.edu>

In article <91r27e$i1t$1@nnrp1.deja.com>,
Adam Levenstein  <cleon42@my-deja.com> wrote:
>And yeah - I never understood why Richie put the "break" in there.
>Seemed unnecessary to me.

I always assumed it was because it made the compiler easier to
implement, although I don't really know if it did.

  - Logan


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

Date: Thu, 21 Dec 2000 07:14:37 GMT
From: Andrew N McGuire <anmcguire@my-deja.com>
Subject: Re: switch/case in Perl?
Message-Id: <91sakr$if1$1@nnrp1.deja.com>

In article <ura24tkgasil7hkvn1l8rg4vpr8i5o3t6l@4ax.com>,
  Bart Lateur <bart.lateur@skynet.be> wrote:
> Andrew N McGuire wrote:
>
> >> And yeah - I never understood why Richie put the "break" in there.
>
> >  Well, you may want to have a default case.  If you decide
> >in your switch that a default case is necessary, then you
> >must use a break in case you have a case where you do not
> >want to fall through to the default case.
                               ^^^^^^^^
                               next

> >  Of course one could argue that you never need a default
> >case

  OK, I should not have said that..  I don't think one could
make that argument at all.

> Nope. You should never need to "fall through". That is like an "if"
> statement, where you do the "else" part as well.

I see nothing wrong with falling through cases, what is wrong with
something like this?

[ besides C being off-topic here, sorry ]

#include <stdio.h>

int main() {

  char option = 'A';

  switch (option) {
    case 'a':
    case 'A':
      printf("a\n");
      break;
    case 'b':
    case 'B':
      printf("b\n");
      break;
    case 'c':
    case 'C':
      printf("c\n");
      break;
    default:
      printf("not a, b or c\n");
      break;
  }

}

granted, you fell from case 'a' to case 'A', and not to
the default case, but there may be some times when a fall from
the last case to the default case is warranted, I would think.
In that example, I want 'a' and 'A' to be equivalent, so the
fall through is natural.

  I should have said "You must have a break in a case, if you do
not want to fall through to the NEXT case, be it the default case
or otherwise."

  Most certainly though, I am not Gods gift to C, so I will correct
myself, and shut my mouth, lest I make more of a fool of myself than
I already have.


anm

--
$ENV{PAGER} = 'perl -wpe0';
system perldoc => '-t', '-F', $0;

=head1 Just Another Perl Hacker


Sent via Deja.com
http://www.deja.com/


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

Date: Thu, 21 Dec 2000 07:39:13 GMT
From: fhinchey@my-deja.com
Subject: trying to count words - not working
Message-Id: <91sc32$ja3$1@nnrp1.deja.com>

Hi,
I'm trying to count the number of time <left> appears in some text. I'm
using the regex shown below but not getting a true count. What am I
missing? Tnx.

-Frank

    $leftcnt = 0;

    for ($body =~ m/<left>/g) {
      $leftcnt++;
    }




Sent via Deja.com
http://www.deja.com/


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

Date: 21 Dec 2000 04:52:00 GMT
From: The WebDragon <nospam@nospam.com>
Subject: Re: Understanding interpolation
Message-Id: <91s29g$f0a$0@216.155.33.36>

In article <91q2ec$7ms$1@diana.bcn.ttd.net>, "tim allen" 
<timallen449@coldmail.com> wrote:


 | I forgot to add the program I used to see how this worked:
 | 
 | @cell_contents = qw(First Second Third Fourth Fifth);
 | for (my $row=0;$row<=$#cell_contents;++$row) {
 |   # if this row number is odd, the cell is Green, otherwise it is Yellow
 |   print <<EOF;
 |       <TD BGCOLOR = "${\($row%2? 'Green':
 | 'Yellow')}">$cell_contents[$row]</TD>
 | EOF
 | }
 | 
 | OUTPUTS:
 | <TABLE>
 |       <TR><TD BGCOLOR = "Yellow">First</TD></TR>
 |       <TR><TD BGCOLOR = "Green">Second</TD></TR>
 |       <TR><TD BGCOLOR = "Yellow">Third</TD></TR>
 |       <TR><TD BGCOLOR = "Green">Fourth</TD></TR>
 |       <TR><TD BGCOLOR = "Yellow">Fifth</TD></TR>
 | </TABLE>
 
sort of :)

#!perl -w
use strict;

my @cell_contents = 
  qw(First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth);

print qq{<table>\n};

for (my $row=0;$row<=$#cell_contents;++$row) {
  # if this row number is odd, the cell is Green, otherwise it is Yellow
  print qq{  <tr><td bgcolor="${\($row%2? 'green':'yellow')}"> 
$cell_contents[$row] </td></tr>\n};
}

print qq{</table>\n};

exit 0;

I very nearly forgot that <<EOF assumes <<"EOF" :) thanks for reminding 
me. 

How would one re-write this to alternate every n lines.. so that instead 
of every other line being different, it could be 2 and 2 or 3 and 3 and 
3 etc. ?

it's a nice trick, but what if you want more? :) embed the bgcolor in a 
subroutine?

-- 
send mail to mactech (at) webdragon (dot) net instead of the above address. 
this is to prevent spamming. e-mail reply-to's have been altered 
to prevent scan software from extracting my address for the purpose 
of spamming me, which I hate with a passion bordering on obsession.  


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

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 V9 Issue 5168
**************************************


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