How can this be done?

R

Robert TV

I am trying to manipulate the data of my @rray based on the first character
of each array element. My array contains many names sorted alphabetically.
EG:

Adam
Aron
Brian
Bert
Chris
Daniel
Derk
Ernie
Frank

and so on ....

Now, I want my script to dected the first letter of the name and take action
based on it like this:

foreach $name (@names) {
if (first letter of $name eq "A" or "a") {
$condition = "True";
$name = "A:$name\n";
} else {
$condition = "False";
}
}

So if the above syntax was valid, the names in the @rray would now print
like this:

A:Adam
A:Aron
Brian
Bert
Chris
Daniel
Derk
Ernie
Frank

I realize the script above will only do "A" ... and i'll need to figure out
how to do the whole alphabet ... but one step at a time, still need some
help how to detect the first letter and take action if conditions meet. TIA

Randy
 
J

John Bokma

Robert said:
I am trying to manipulate the data of my @rray based on the first character
of each array element. My array contains many names sorted alphabetically.
EG:

Adam
Aron
Brian
Bert
Chris
Daniel
Derk
Ernie
Frank

and so on ....

Now, I want my script to dected the first letter of the name and take action
based on it like this:

foreach $name (@names) {
if (first letter of $name eq "A" or "a") {
$condition = "True";
$name = "A:$name\n";
} else {
$condition = "False";
}
}

So if the above syntax was valid, the names in the @rray would now print
like this:

A:Adam
A:Aron
Brian
Bert
Chris
Daniel
Derk
Ernie
Frank

I realize the script above will only do "A" ... and i'll need to figure out
how to do the whole alphabet ... but one step at a time, still need some
help how to detect the first letter and take action if conditions meet. TIA

Not enough info :-D. If you want different actions, you can make a hash
with references to subs, and use the first letter (perldoc -f substr) to
obtain (perldoc -f lc) the sub ref, and execute its sub.

But from the above it's really unclear what you want. For example,
$condition. Do you want 26 different ones? If you just want to prefix it
with caps first letter followed by :, use substr.

PS: use strict and use warnings on top of your script. I guess you don't
use those...
 
R

Robert TV

Not enough info :-D. If you want different actions, you can make a hash
with references to subs, and use the first letter (perldoc -f substr) to
obtain (perldoc -f lc) the sub ref, and execute its sub.

But from the above it's really unclear what you want. For example,
$condition. Do you want 26 different ones? If you just want to prefix it
with caps first letter followed by :, use substr.

PS: use strict and use warnings on top of your script. I guess you don't
use those...

What I am trying to do is take action based on the first letter of the @rray
ellement. Ok, forget about the "A:" etc ect. Lets just say I wanted to do
this ... no @rrays ... just a normal string ...

$mystring = "Grettings fellow programmer";

if (first letter of $mystring eq "G") {
do this
} else {
do that
}

Once I have this basic piece of the puzzle, I can make the rest work. TIA

R
 
B

Bill

Robert said:
What I am trying to do is take action based on the first letter of the @rray
ellement. Ok, forget about the "A:" etc ect. Lets just say I wanted to do
this ... no @rrays ... just a normal string ...

$mystring = "Grettings fellow programmer";

if (first letter of $mystring eq "G") {
do this
} else {
do that
}

Once I have this basic piece of the puzzle, I can make the rest work. TIA

untested--get Switch from CPAN:

#!/usr/bin/perl
use strict;
use warnings;
use Switch;

foreach my $phrase (@array) {
my $firstchar = lc substr($phrase, 0, 1);

switch($firstchar) {
case 'a' : { do_a($phrase) }
case 'b' : { do_b() }
case 'c' : { do_c($etc_and_so_forth) }
else { do_else_stuff() }
}

}
 
P

Petri

What I am trying to do is take action based on the first letter of
the @rray ellement. Ok, forget about the "A:" etc ect. Lets just say
I wanted to do this ... no @rrays ... just a normal string ...
Once I have this basic piece of the puzzle, I can make the rest work.

Sure, that's easy.
Either a substring as has been mentioned, or a regex, like in this
oversimplified example:
---8<---
use strict;
use warnings;

my @a=qw(alfa bravo charlie delta);
my $s;
for('a'..'d'){
# Anchor to beginning of string, use /i to make case insensitive.
print "$_:$s\n" if ($s = <@a>) =~ /^$_/i;
}
---8<---

Read up on regular expressions here:
perldoc perlre

You seem like you'll be able to proceed on your own from here.

Hope this helps,


Petri
 
J

Jim Cochrane

untested--get Switch from CPAN:
...

Or how about something like this:

#!/usr/bin/perl

use strict;
use warnings;

my @names = <>;
chomp @names;

initialize();
process_names(@names);

my %handlers = ();

sub process_names {
my (@names) = @_;
for my $n (@names) {
my $handler = $handlers{lc substr $n, 0, 1};
if ($handler) {
$handler->($n);
} else {
print "Could not process $n\n";
}
}
}

sub initialize {
my $a_handler = sub { print "a_handler handling @_\n"; };
my $b_handler = sub { print "b_handler handling @_\n"; };
my $c_handler = sub { print "c_handler handling @_\n"; };
my $other_handler = sub { print "other_handler handling @_\n"; };

# Set up a table of subroutine references to be keyed on first letter.
%handlers = (
a => $a_handler,
b => $b_handler,
c => $c_handler
);
# Example of using a generic 'handler', e.g., for the remaining letters:
for my $l ('d' .. 'z') {
$handlers{$l} = $other_handler;
}
}
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

I am trying to manipulate the data of my @rray based on the first character
of each array element. My array contains many names sorted alphabetically.
EG:

Adam
Aron
Brian
Bert
Chris
Daniel
Derk
Ernie
Frank

and so on ....

Now, I want my script to dected the first letter of the name and take action
based on it like this:

foreach $name (@names) {
if (first letter of $name eq "A" or "a") {
$condition = "True";
$name = "A:$name\n";
} else {
$condition = "False";
}
}

So if the above syntax was valid, the names in the @rray would now print
like this:

A:Adam
A:Aron
Brian
Bert
Chris
Daniel
Derk
Ernie
Frank

I realize the script above will only do "A" ... and i'll need to figure out
how to do the whole alphabet ... but one step at a time, still need some
help how to detect the first letter and take action if conditions meet. TIA

Randy

$letter1 = substr $name,0,1; # get 1st letter of name
if ( "a" eq lc $letter1 ) {
$condition = "True";
$name = "A:$name\n";
}
 
A

Ala Qumsieh

Robert said:
if (first letter of $mystring eq "G") {
do this
} else {
do that
}

TMTOWTDI, but I'd do it this way:

if ($name =~ /^[A-Z]/) {
$condition = 'True';
$name = "$&: $name";
} else {
$condition = 'False';
}

--Ala
 
U

Uri Guttman

AQ> TMTOWTDI, but I'd do it this way:

AQ> if ($name =~ /^[A-Z]/) {
AQ> $condition = 'True';
AQ> $name = "$&: $name";

gack! stay away from $& and friends. its use will slow down all regexes
as it forces an extra copy to be saved. just do anormal grab with ().

uri
 
A

Ala Qumsieh

Uri said:
gack! stay away from $& and friends. its use will slow down all regexes
as it forces an extra copy to be saved. just do anormal grab with ().

True. I know about the effect of $& et al, but I thought they were
optimized in recent Perls so that their effect isn't as bad as it used
to be (but still worse than $1 et al). Btw, how much of a penalty does
their use incur? I have never noticed any extra run-time delay due to
their use, and unless you're building a mission-critical app where
real-time response is very important, I doubt any one else will notice
their effect. I might be wrong, but from my experience I don't see any
reason to avoid $& if it makes the code simpler and easier to read
(oxymoron?). Building real-time apps is hardly Perl's niche.

--Ala
 
U

Uri Guttman

AQ> True. I know about the effect of $& et al, but I thought they were
AQ> optimized in recent Perls so that their effect isn't as bad as it
AQ> used to be (but still worse than $1 et al). Btw, how much of a
AQ> penalty does their use incur? I have never noticed any extra
AQ> run-time delay due to their use, and unless you're building a
AQ> mission-critical app where real-time response is very important, I
AQ> doubt any one else will notice their effect. I might be wrong, but
AQ> from my experience I don't see any reason to avoid $& if it makes
AQ> the code simpler and easier to read (oxymoron?). Building
AQ> real-time apps is hardly Perl's niche.

if you want to know the speed penalty, benchmark it. besides that, it is
better code IMO to do an explicit grab than an implict one. even if $&
didn't have a speed issue, i would never use it. as for perl and speed,
you ain't seen my boss who is a perl speed freak. his web template
engine and massive cache (1GB) takes 20 million hits a day. all in
perl. and real time apps don't necessarily have to run that fast. the
key is whether they respond in some way relative to real time. you can
run things with timeouts on the order of seconds and still call it real
time.

uri
 
R

Richard Morse

Not enough info :-D. If you want different actions, you can make a hash
with references to subs, and use the first letter (perldoc -f substr) to
obtain (perldoc -f lc) the sub ref, and execute its sub.

But from the above it's really unclear what you want. For example,
$condition. Do you want 26 different ones? If you just want to prefix it
with caps first letter followed by :, use substr.

PS: use strict and use warnings on top of your script. I guess you don't
use those...

What I am trying to do is take action based on the first letter of the @rray
ellement. Ok, forget about the "A:" etc ect. Lets just say I wanted to do
this ... no @rrays ... just a normal string ...

$mystring = "Grettings fellow programmer";

if (first letter of $mystring eq "G") {
do this
} else {
do that
}[/QUOTE]

perldoc -f substr
perldoc -f uc

if (uc(substr($mystring, 0, 1)) uq 'G') {
do_this;
} else {
do_that;
}

Also, see `perldoc -q switch`.

HTH,
Ricky
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top