[25357] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7602 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 4 14:06:04 2005

Date: Tue, 4 Jan 2005 11:05:25 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 4 Jan 2005     Volume: 10 Number: 7602

Today's topics:
        ANNOUNCE: Graph-ReadWrite 2.00 neil@bowers.com
    Re: calling a .dll <bart.lateur@pandora.be>
        cross-platform shell scripts <claudio_greco@yahoo.com>
    Re: cross-platform shell scripts <jurgenex@hotmail.com>
    Re: cross-platform shell scripts ioneabu@yahoo.com
    Re: cross-platform shell scripts <phaylon@dunkelheit.at>
    Re: cross-platform shell scripts <claudio_greco@yahoo.com>
    Re: cross-platform shell scripts <phaylon@dunkelheit.at>
    Re: cross-platform shell scripts <matternc@comcast.net>
    Re: cross-platform shell scripts <botfood@yahoo.com>
    Re: cross-platform shell scripts mickg@optonline.net
    Re: cross-platform shell scripts <nun@example.com>
    Re: Curses and getch problem -- stdin, initscr and newt spambox@volja.net
    Re: FAQ 1.4: What are perl4 and perl5? <flavell@ph.gla.ac.uk>
    Re: FAQ 1.4: What are perl4 and perl5? <uri@stemsystems.com>
    Re: FAQ 1.4: What are perl4 and perl5? <matternc@comcast.net>
    Re: FAQ 1.4: What are perl4 and perl5? <flavell@ph.gla.ac.uk>
    Re: file rename help (Anno Siegel)
    Re: file rename help <bik.mido@tiscalinet.it>
    Re: file rename help <bik.mido@tiscalinet.it>
    Re: file rename help <bik.mido@tiscalinet.it>
        Looking at a character ipellew@pipemedia.co.uk
    Re: Looking at a character <noreply@gunnar.cc>
    Re: Looking at a character <jurgenex@hotmail.com>
    Re: Looking at a character <shawn.corey@sympatico.ca>
    Re: Looking at a character <spamtrap@dot-app.org>
    Re: Looking at a character <nospam@nospam.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 2 Jan 2005 20:17:08 GMT
From: neil@bowers.com
Subject: ANNOUNCE: Graph-ReadWrite 2.00
Message-Id: <I9sMzL.9ou@zorch.sf-bay.org>

Version 2.00 of the Graph-ReadWrite distribution is making its way
round CPAN:

file: $CPAN/authors/id/N/NE/NEILB/Graph-ReadWrite-2.00.tar.gz
size: 24093 bytes
md5: e82da37e1202dcf431f03375e5e553e0

The main change in this version is support for the changes in the
recent 0.5 release of Jarkko Hietaniemi's Graph classes.

Other changes:
- support for new Dot attributes
- node IDs are now quoted in Graph::Writer::Dot, which means
you can safely use spaces and other non-alphanumerics
(hyphens, colons, etc) in node names.
- fixed warnings from Graph::Reader::Dot

What is Graph-ReadWrite?

This is a collection of perl classes for reading and writing
directed graphs in a variety of file formats. The graphs are
represented in Perl using Jarkko Hietaniemi's Graph classes.

The package includes readers for Dot (the graphviz file format),
HTK, and a simple XML format. It includes writers for Dot, VCG,
daVinci, HTK, and the simple XML format.

Neil




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

Date: Tue, 04 Jan 2005 11:32:13 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: calling a .dll
Message-Id: <idvkt012kuk5koqqhb86ibhujo83gs9v0g@4ax.com>

Larry wrote:

>How can I do this in Perl:
>
>Declare Function waveInOpen Lib "winmm.dll" (lphWaveIn As Long, ByVal 
>uDeviceID As Long, lpFormat As WAVEFORMATEX, ByVal dwCallback As Long, 
>ByVal dwInstance As Long, ByVal dwFlags As Long) As Long

My normal answer would be to use Win32::API. "byval ... as long"
translates to "N", the others as "P". Provide scalars with enough room
(assign strings to them) to hold the structures, in particular for
WAVEFORMATEX. Return value type is "N".

However, with a name like "dwCallback", I have doubts it is possible to
do. Well, there is Win32::API::Callback... I've never used it, though.

-- 
	Bart.


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

Date: 4 Jan 2005 07:56:53 -0800
From: "Claudio" <claudio_greco@yahoo.com>
Subject: cross-platform shell scripts
Message-Id: <1104854213.158927.107260@c13g2000cwb.googlegroups.com>

Hi,

I'm almost new to Perl and I only know the basics.
I would like to use it for creating cross-platform shell scripts
(Windows and Unix ).
I suppose Perl is appropriate for this task.
Should I look at any module in particular?

Thanks!

-Claudio



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

Date: Tue, 04 Jan 2005 16:07:46 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: cross-platform shell scripts
Message-Id: <mbzCd.23314$rL3.12996@trnddc03>

Claudio wrote:
> I would like to use it for creating cross-platform shell scripts
> (Windows and Unix ).

Well, do you know for certain that your Windows users installed a shell 
(e.g. tsch, bash, or whatever) on their boxes?

> I suppose Perl is appropriate for this task.

Somewhere you lost me. Where would you use _Perl_ for writing _shell_ 
scripts?
It is like asking if Pascal is appropriate for writing COBOL programs.

jue 




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

Date: 4 Jan 2005 08:29:12 -0800
From: ioneabu@yahoo.com
Subject: Re: cross-platform shell scripts
Message-Id: <1104856152.339942.251410@c13g2000cwb.googlegroups.com>

I think I see what you mean.  There are a lot of tools that are
available on Unix systems by default that are either non-existent for
Windows or have to be installed by the user.  Perl is said to replace
Sed and Awk and probably a lot of other Unix programs.  That would make
Perl ideal for portable scripts that you might otherwise write with one
of these other programs.

I was wondering if there is any point in learning Sed, Awk or other
programming tools if you are already learning Perl.  I started reading
the O'Reilly Sed/Awk book a little and I keep thinking with each
example, "I could just do this in Perl."

wana



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

Date: Tue, 04 Jan 2005 17:41:43 +0100
From: Robert Sedlacek <phaylon@dunkelheit.at>
Subject: Re: cross-platform shell scripts
Message-Id: <pan.2005.01.04.16.41.43.759701@dunkelheit.at>

Jürgen Exner wrote:

> Somewhere you lost me. Where would you use _Perl_ for writing _shell_
> scripts?

I think he meant 'Shell _started_ scripts/programs'. But I would suggest a
first look on cygwin, before reinventing wheels or start using lesser-good
reinvented wheels.

g,
Robert

-- 
http://www.dunkelheit.at/
codito, ergo sum.



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

Date: 4 Jan 2005 08:51:57 -0800
From: "Claudio" <claudio_greco@yahoo.com>
Subject: Re: cross-platform shell scripts
Message-Id: <1104857517.877901.25110@c13g2000cwb.googlegroups.com>

Said in another way, I would like to write Perl programs instead of
Unix shell scripts and Windows batch files.

Try to imagine tasks that you would perform using a shell script...
 ...ok, now try to imagine how you would do the same using Perl.

Of course I am not talking about language constructs, I'm talking about
modules that implement portable libraries to emulate the usual tasks
that you do in shell scripts.

Using your context, I didn't ask whether Pascal is good for writing
COBOL programs, I asked whether there are Pascal libraries that could
simplify my job of writing programs to deal with fixed-length
records...

-Claudio



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

Date: Tue, 04 Jan 2005 17:55:18 +0100
From: Robert Sedlacek <phaylon@dunkelheit.at>
Subject: Re: cross-platform shell scripts
Message-Id: <pan.2005.01.04.16.55.18.149413@dunkelheit.at>

Claudio wrote:

> Of course I am not talking about language constructs, I'm talking about
> modules that implement portable libraries to emulate the usual tasks that
> you do in shell scripts.

Ah, i see. Maybe you want to watch out for Pure-Perl[0] modules on cpan.

g,
Robert

[0] If I understood them right.

-- 
http://www.dunkelheit.at/

The mind is its own place, and in itself
Can make a heaven of hell, a hell of heaven.  -- Milton, »Paradise Lost«



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

Date: Tue, 04 Jan 2005 12:11:40 -0500
From: Chris Mattern <matternc@comcast.net>
Subject: Re: cross-platform shell scripts
Message-Id: <qO2dnZ6jO4TQU0fcRVn-tA@comcast.com>

Claudio wrote:

> Hi,
> 
> I'm almost new to Perl and I only know the basics.
> I would like to use it for creating cross-platform shell scripts
> (Windows and Unix ).
> I suppose Perl is appropriate for this task.

Perl is excellent for this task.

> Should I look at any module in particular?
> 
That depends entirely on what you want to do.  Many things will
simply work on both Unix and Windows without any need to change
anything.  Other things will run into differences between the
systems that Perl can't hide.

-- 
             Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"


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

Date: 4 Jan 2005 09:34:16 -0800
From: "botfood" <botfood@yahoo.com>
Subject: Re: cross-platform shell scripts
Message-Id: <1104860056.890009.192940@f14g2000cwb.googlegroups.com>

most typical admin tasks are well handled by standard perl installs on
each platform... there are *some* differences that are pretty well laid
out by Oreilly "gecko" book learning perl on win32 systems. In the rare
case you need to do something non-transparent, you can check the OS and
make a different call from inside your "portable" script.

d



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

Date: 4 Jan 2005 09:37:52 -0800
From: mickg@optonline.net
Subject: Re: cross-platform shell scripts
Message-Id: <1104860272.827266.209030@f14g2000cwb.googlegroups.com>

Are you Ione Haskel?

Mick G



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

Date: Tue, 04 Jan 2005 19:07:38 +0100
From: Mitja <nun@example.com>
Subject: Re: cross-platform shell scripts
Message-Id: <opsj3ba0hxdob4us@localhost>

On Tue, 04 Jan 2005 16:07:46 GMT, Jürgen Exner <jurgenex@hotmail.com>  
wrote:

> Claudio wrote:
>> I would like to use it for creating cross-platform shell scripts
>> (Windows and Unix ).
>
> Well, do you know for certain that your Windows users installed a shell
> (e.g. tsch, bash, or whatever) on their boxes?

Windows user _do_ have a shell installed. It's called explorer.exe. The  
shell is an integral part of every OS, that's why it's called the way it  
is. It encloses the inner workings of the OS and provides an interface for  
users.

 From dictionary.com:
The command interpreter used to pass
commands to an operating system; so called because it is the
part of the operating system that interfaces with the outside
world.

>> I suppose Perl is appropriate for this task.
> Somewhere you lost me. Where would you use _Perl_ for writing _shell_
> scripts?
Sure, perl programs that interact with the shell, be it bash, cmd or  
anything else. He most probably wants to create file manipulation scripts  
independent of the OS, using Perl as THE layer taking care of most  
problems and OS differences.


-- 
Mitja


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

Date: 4 Jan 2005 05:00:33 -0800
From: spambox@volja.net
Subject: Re: Curses and getch problem -- stdin, initscr and newterm issues?
Message-Id: <1104843633.616188.9860@f14g2000cwb.googlegroups.com>

spambox@volja.net wrote:
> 1 while <>;
> initscr();

Upon examining urlview, which also processes standard input first, I've
found this interesting piece of code:

/* if we piped a file we can't use initscr() because it assumes
`stdin' */
fp = reopen_tty ? fopen ("/dev/tty", "r") : stdin;
scr = newterm (NULL, stdout, fp);
set_term (scr);

But for me it still doesn't work:
[...]
1 while <>
open(TTYI, "/dev/tty") or die $!;
$scr = newterm(undef, STDOUT, TTYI) or die $!;
set_term($scr);
[...]

Please, does anyone know how to make it work?

andrej



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

Date: Tue, 4 Jan 2005 14:49:26 +0000
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: FAQ 1.4: What are perl4 and perl5?
Message-Id: <Pine.LNX.4.61.0501041445360.31474@ppepc56.ph.gla.ac.uk>

On Tue, 4 Jan 2005, brian d foy wrote:

> In article <Xns95D47575A0A9ebohlmanomsdevcom@130.133.1.4>, Eric Bohlman
> <ebohlman@omsdev.com> wrote:
> 
> > Does this still belong in the FAQ as of 2005?  The only reason I can think 
> > of is poorly-written books with "Perl 5" in the title (are any of them 
> > still in print?).
> 
> it probably doesn't belong.  I'll see about removing it.

As I recall, both Digital Unix (or whatever it's called today) and 
Solaris make use of Perl 4 scripts in their maintenance procedures, 
and come with a version of Perl 4 installed for that reason.  In fact, 
a Solaris user brought this up on the group recently, as I dimly 
recall.

Maybe it's worth retaining some kind of mention that a modern Perl can 
still be installed without upsetting the maintenance procedures.  I'm 
afraid that nowadays I'm not working with D.U nor with Solaris, so the 
details would have to come from someone else.


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

Date: Tue, 04 Jan 2005 16:20:30 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: FAQ 1.4: What are perl4 and perl5?
Message-Id: <x71xd1yxm9.fsf@mail.sysarch.com>

>>>>> "AJF" == Alan J Flavell <flavell@ph.gla.ac.uk> writes:

  AJF> As I recall, both Digital Unix (or whatever it's called today) and 
  AJF> Solaris make use of Perl 4 scripts in their maintenance procedures, 
  AJF> and come with a version of Perl 4 installed for that reason.  In fact, 
  AJF> a Solaris user brought this up on the group recently, as I dimly 
  AJF> recall.

solaris 9 at least comes with 5.6.1. i don't think solaris ever came
with perl4. it does have some perl scripts but not perl4 ones. very few
modern unix flavors (i wouldn't call dec unix modern :) come with perl4.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Tue, 04 Jan 2005 12:07:32 -0500
From: Chris Mattern <matternc@comcast.net>
Subject: Re: FAQ 1.4: What are perl4 and perl5?
Message-Id: <qO2dnZ-jO4TJUEfcRVn-tA@comcast.com>

Uri Guttman wrote:

>>>>>> "AJF" == Alan J Flavell <flavell@ph.gla.ac.uk> writes:
> 
>   AJF> As I recall, both Digital Unix (or whatever it's called today) and
>   AJF> Solaris make use of Perl 4 scripts in their maintenance procedures,
>   AJF> and come with a version of Perl 4 installed for that reason.  In
>   fact, AJF> a Solaris user brought this up on the group recently, as I
>   dimly AJF> recall.
> 
> solaris 9 at least comes with 5.6.1. i don't think solaris ever came
> with perl4. it does have some perl scripts but not perl4 ones. very few
> modern unix flavors (i wouldn't call dec unix modern :) come with perl4.
> 
> uri
> 
Solaris is weird.  9 ships with 5.6.1...and 5.00503.  However, it doesn't
seem to come with any flavor of Perl 4.
-- 
             Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"


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

Date: Tue, 4 Jan 2005 17:31:25 +0000
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: FAQ 1.4: What are perl4 and perl5?
Message-Id: <Pine.LNX.4.61.0501041727520.31474@ppepc56.ph.gla.ac.uk>

On Tue, 4 Jan 2005, Chris Mattern wrote:

> Uri Guttman wrote:
> 
> >>>>>> "AJF" == Alan J Flavell <flavell@ph.gla.ac.uk> writes:
> > 
> >   AJF> As I recall, both Digital Unix (or whatever it's called today) and
> >   AJF> Solaris make use of Perl 4 scripts in their maintenance procedures,
> > 
> > solaris 9 at least comes with 5.6.1. i don't think solaris ever came
> > with perl4. it does have some perl scripts but not perl4 ones. very few
> > modern unix flavors (i wouldn't call dec unix modern :) come with perl4.
>
> Solaris is weird.  9 ships with 5.6.1...and 5.00503.  However, it doesn't
> seem to come with any flavor of Perl 4.

Thanks - apologies for the misinformation.


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

Date: 4 Jan 2005 12:19:25 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: file rename help
Message-Id: <cre1kd$668$1@mamenchi.zrz.TU-Berlin.DE>

Peter Sundstrom <peter@invalid.invalid> wrote in comp.lang.perl.misc:
> 
> "Shawn" <spedwards@x.qwest.net> wrote in message 
> news:iDkCd.32$od2.19979@news.uswest.net...

> Given that a / is not a valid character for a Unix filename, then why are 
> you checking for it?

The OP is looking for the character in the *content* of the file,
not in its name...

> >                        chomp($csv_client_id = `cut -c1-6 $cfile 
> > |sort -u`);

 ...as this snippet clearly shows.

Anno


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

Date: Tue, 04 Jan 2005 15:51:18 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: file rename help
Message-Id: <tm4lt0p8q4ikbbnqap5lrs03o3kunuqgqu@4ax.com>

On Mon, 3 Jan 2005 16:33:17 -0700, "Shawn" <spedwards@x.qwest.net>
wrote:

>#!/usr/local/bin/perl5.6

  use strict;
  use warnings;

always!!

>use lib "lib";

Why, BTW?

>use MIME::Lite;

Why, BTW?

>open(FILE_LIST,"ls f* | grep -v txt|" ) || die "Cannot do \n" ;

It's better to use lexical fhs. It's better to use low precedence
logical operators for flow control. It's better to give informative
error messages (at least include $!).

Said this, a shell script is a shell script and a Perl program is...
err, a Perl program! Each of them can be the best solution for a given
task, but there's little to no point at all in a perl script that
works like a shell script relying massively on external utilities.

Please note that I'm not saying one should *never* do that. Indeed in
some situations an open() like yours or a system() can be give you an
advantage, which can largely depend from one's subjective point of
view (e.g. less keystrokes to type!).

In this case Perl has both a glob() function, which even has a
shortcut (but many people think it's not a good practice to resort to
it) and a grep() one.

So in this case...

>        while ($cfile = <FILE_LIST>) {

 ...I'd use

  for (grep !/txt/, <f*>) {

instead.

>                if ( -z "$cfile" ) {
>                        system("/bin/mv $cfile $cfile.zero.txt");

You can use Perl's rename() here. (File::Copy can be preferrable in
some situations).

>                $charcount = system("cut -c9 $cfile |sort -u |grep '/' 
>|wc -l");

You do not want system() here: you want qx// instead, aka backticks
(but I don't like them! ;-)

Also, what if $cfile was empty and hence moved to "$cfile.zero.txt"?!?

However what you wrote above takes the 9th char from each line of
$cfile and sorts with -u, so that you either get exactly one or zero
line(s) with '/' (or better: being exactly '/') according to wether
there's at least one line with '/' in 9th position or not. Or am I
mistaken?

Perl code for the above _could_ be:

  open my $fh, '<', $cfile or
    die "Can't open `$cfile': $!\n";
  my $charcount=0;
  while (<$fh>) {
      $charcount=1 and last if m|(?<=^.{8})/|;
  }

(but I guess you really wanted something else!)

>                if ( $charcount > 0 ) {

You can use

  if ($charcount) {

instead.

>                        chomp($csv_client_id = `cut -c1-6 $cfile |sort -u`);

Perl's own code for this _could_ be:

  open my $fh, '<', $cfile or
    die "Can't open `$cfile': $!\n";
  my %saw;
  @csv_client_ids = grep !$saw{$_}++, 
    map { chomp; substr $_, 0, 6 } <$fh>;

Please note that I used an array instead of what you wrote because I
_think_ that what you wrote in the first place is not what you really
want.

Of course, since you read the contents of $cfile twice, then you'd
better open() it once and the either slurp it into an array all at
once (and then process it separately) if the file is small enough or
use two separate C<while> loops seek()ing in between them.

I think that it would be better for you to provide a description of
the task you're aiming at (or a suitably simplified version of it) and
we may hopefully provide a minimal hopefully working example script.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Tue, 04 Jan 2005 15:51:19 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: file rename help
Message-Id: <389lt09r1bb0jpl0una6eofig2o90vjgna@4ax.com>

On Tue, 4 Jan 2005 11:40:03 +1100, "Andrew Hamm" <ahamm@mail.com>
wrote:

>## no warnings 'uninitialized';
>
>The 3rd commented-out line is a personal favourite of mine, but that's
>because I am not afraid of undef values, and the warnings about undef
>values usually irritate me far more than coding for them. However, I do

Indeed that is the single warning that most often needs to be disabled
out of good reason. But since it can still be useful, it should be
really disabled *locally* only in those situations in which you know
you could get it but you know you do not want to be warned as you
expected it.

Incidentally had I known you (and others, now I see) wrote this
followup, probably I would have not written mine. However the OP's
script is so full of bizarre constructs that I've not paid extreme
attention to each single one of them and so he will probably benefit
from many people supplying their own cmts.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Tue, 04 Jan 2005 15:51:20 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: file rename help
Message-Id: <oe8lt05fhp8boqblcksm11j7l08qesdjka@4ax.com>

On Tue, 4 Jan 2005 14:38:14 +1300, "Peter Sundstrom"
<peter@invalid.invalid> wrote:

>>                $charcount = system("cut -c9 $cfile |sort -u |grep '/' 
>> |wc -l");
>
>Now the above line is just plain bizarre.
[snip]
>3.  No Unix filename is going to have a / in it.

However bizarre that line (actually!) is, while indeed no unix
*filename* will have '/' in it, this doesn't mean the *file* won't as
well.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: 4 Jan 2005 07:54:13 -0800
From: ipellew@pipemedia.co.uk
Subject: Looking at a character
Message-Id: <1104854053.559226.80000@f14g2000cwb.googlegroups.com>

Hi all;

Whats the fasted way to look at a single character?

my $s = "a1b2";
if ( $s[0] eq "a" ) {
print "we got it";
} else {
print "Not to start";
}

Of course this is not valid.

Regards
Ian



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

Date: Tue, 04 Jan 2005 16:58:23 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Looking at a character
Message-Id: <33vsvlF43cgpuU1@individual.net>

ipellew@pipemedia.co.uk wrote:
> Whats the fasted way to look at a single character?

     perldoc -q "characters of a string"

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Tue, 04 Jan 2005 16:02:14 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Looking at a character
Message-Id: <a6zCd.23312$rL3.295@trnddc03>

ipellew@pipemedia.co.uk wrote:
> Whats the fasted way to look at a single character?

There are no single characters in Perl.
The closest thing to such a concept would be a string of length 1.

> my $s = "a1b2";
> if ( $s[0] eq "a" ) {

You don't have an array @s, therefore $s[0] should give you an error message 
under strictures.

Now, if you would explain what you mean by "look at a single character", 
them maybe we could suggest a solution.

- Do you want to retrieve the character at position n to print it (I guess 
this is the only way to "look at a character")?
- Do you want to check if the character at position n matches/equals/doesn't 
equal a given character/RE/character class/... (that's what your code 
suggests)?
- Something completely different?

jue




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

Date: Tue, 04 Jan 2005 11:59:46 -0500
From: Shawn Corey <shawn.corey@sympatico.ca>
Subject: Re: Looking at a character
Message-Id: <EWzCd.6917$7n1.507439@news20.bellglobal.com>

ipellew@pipemedia.co.uk wrote:
> Hi all;
> 
> Whats the fasted way to look at a single character?
> 
> my $s = "a1b2";

my @s = split //, $s;

> if ( $s[0] eq "a" ) {
> print "we got it";
> } else {
> print "Not to start";
> }
> 
> Of course this is not valid.
> 
> Regards
> Ian
> 


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

Date: Tue, 04 Jan 2005 13:15:38 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Looking at a character
Message-Id: <n-ednTobx9PXQEfcRVn-sQ@adelphia.com>

ipellew@pipemedia.co.uk wrote:

> Whats the fasted way to look at a single character?
> 
> my $s = "a1b2";
> if ( $s[0] eq "a" ) {

In the above example (which is more C than Perl, by the way, but I think 
you knew that) you check to see if the first character of $s is "a". You 
can do that with a simple regular expression, with no need to "look at" 
the first character as a separate variable:

if ($s =~ /^a/) { ... }

Or, if the character is part of a pattern that might appear at any point 
within the string, you could use "(.)" within a regex to capture the 
character in a subexpression:

my ($first, $last) = ($s =~ /^(.).*(.)$/);

If you want an array with all of the characters as elements, split() the 
string with an empty delimiter:

my $chars = split(//, $s);

Of course, you could use substr() to extract a single character from a 
string, if you know the index at which it appears within the string:

my $first = substr($s, 0);

And that's without considering the handful of oddball functions for 
doing various things to single characters within a string, for example 
ucfirst() or lcfirst().

In short, there are many ways to "look at" a single character. Which one 
is best is very much dependent on *why* you want to look at it.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


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

Date: Tue, 4 Jan 2005 13:28:16 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Looking at a character
Message-Id: <1104863288.468300@nntp.acecape.com>

<ipellew@pipemedia.co.uk> wrote in message
news:1104854053.559226.80000@f14g2000cwb.googlegroups.com...
> Hi all;
>
> Whats the fasted way to look at a single character?
>
> my $s = "a1b2";
> if ( $s[0] eq "a" ) {


wouldn't pattern matching be the way to go here? just seems the quickest and
eaiest way

my $s = "a1b2";
if ( $s =~ /a/ )            #is true if an 'a' is anywhere in $s
 ....
if ( $s =~ /^a/ )            #is true if an 'a' is at the beginning of $s
 ....

perldoc perlrequick   or just go to
http://aspn.activestate.com/ASPN/docs/ActivePerl/lib/Pod/perlrequick.html

and sort of as a sidebar,  as for the $s[0] scenario, I came from C and
tried stuff like that.  so if you are the same be carefuly, cause sometimes
C creeps out of you and not all syntaxes will give you an obvious
error....they just don;t work, then your code just isnt behaving as you
think it should, and then you spend all this time in the error log, of your
server, etc. etc.  i THINK i did something like if ($s = = "cat")....and
didn't get an error (might have been something else), just wouldnt work
right, and took me forever to track it down...and when i finally did i
realized that i had typed in something using C instead.

so if you are coming from that backround, just wanted to say, keep a pad
with the proper ways to do "what you do reflexivly in C" in Perl....saved me
grief

daniel




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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 7602
***************************************


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