For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > pack an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author pack an array
Gerard Robin

2006-01-10, 4:02 am

Hello,
I guess that one can write in more perlish fashion that I did:=20
the part between the #### of this script to pack the array @array.
=20
Please, can someone give me some hint ?

#!/usr/bin/perl
# pack_array.pl

use strict;
use warnings;

my $string =3D "h\@a###p\$p??\$y n??e#w [?! \$ \% y]e{?]a##r? 2\*0\$0'\"6\#=
! \^";
my @array =3D split / /, $string;

foreach (@array) {

s/[!?#\@\$\%"^'"\]\[\*{]//g;
}
####################################
my @pack;
my $j =3D 0;

foreach my $i (0..$#array) {

if ($array[$i] eq '') {

$i++;

} else {

$pack[$j] =3D $array[$i];

$j++;

}
}
#####################################

print "@pack\n";

__END__

tia

--=20
G=E9rard

Shawn Corey

2006-01-10, 4:02 am

Gerard Robin wrote:
> Hello,
> I guess that one can write in more perlish fashion that I did: the part
> between the #### of this script to pack the array @array.
>
> Please, can someone give me some hint ?
>
> #!/usr/bin/perl
> # pack_array.pl
>
> use strict;
> use warnings;
>
> my $string = "h\@a###p\$p??\$y n??e#w [?! \$ \% y]e{?]a##r?
> 2\*0\$0'\"6\# ! \^";
> my @array = split / /, $string;
>
> foreach (@array) {
>
> s/[!?#\@\$\%"^'"\]\[\*{]//g;
> }
> ####################################
> my @pack;
> my $j = 0;
>
> foreach my $i (0..$#array) {
>
> if ($array[$i] eq '') {
>
> $i++;
>
> } else {
>
> $pack[$j] = $array[$i];
>
> $j++;
>
> }
> }
> #####################################
>
> print "@pack\n";
>
> __END__
>
> tia
>


Is this what you mean?

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my $string = "h\@a###p\$p??\$y n??e#w [?! \$ \% y]e{?]a##r?
2\*0\$0'\"6\# ! \^";
$string =~ s/[!?#\@\$\%"^'"\]\[\*{\s]+/ /g;
print "$string\n";

my @array = split /\s+/, $string;
print "@array\n";

__END__


--

Just my 0.00000002 million dollars worth,
--- Shawn

"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/
Tom Phoenix

2006-01-10, 4:02 am

On 1/2/06, Gerard Robin <jag.robin18@wanadoo.fr> wrote:
> my @pack;
> my $j =3D 0;
>
> foreach my $i (0..$#array) {
>
> if ($array[$i] eq '') {
>
> $i++;
>
> } else {
>
> $pack[$j] =3D $array[$i];
>
> $j++;
>
> }
> }


You're right that this doesn't seem very Perl-ish. "Real Perl
programmers don't use subscripts." That's an exaggeration, but you
should generally prefer to avoid subscripting when Perl's other list
and array mechanisms are available. Maybe something like this:

my @pack;
my $empties =3D 0;
foreach my $item (@array) {
if ($item eq '') { # empty string
$empties++;
} else {
push @pack, $item;
}
}

The foreach loop avoids indexing into @array, and using push avoids
indexing into @pack.

You might be able to make another optimization in that code. Instead
of counting the empty strings as you go along, you could do some math
after building @pack:

my $empties =3D @array - @pack;

That subtracts the number of elements of @pack from the number of
elements of @array (because subtraction provides scalar context).

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Paul Lalli

2006-01-10, 4:02 am

Gerard Robin wrote:
> I guess that one can write in more perlish fashion that I did:
> the part between the #### of this script to pack the array @array.


"pack" is a perl keyword that does not seem to have anything to do with
what you are trying to do. In the future, it will help to describe
more precisely what you are trying to do. In this case, it seems you
wish to remove element of an array that are equal to the empty string.

> ####################################
> my @pack;
> my $j = 0;
>
> foreach my $i (0..$#array) {
> if ($array[$i] eq '') {
> $i++;
> } else {
> $pack[$j] = $array[$i];
> $j++;
> }
> }
> #####################################


Use the 'grep' function:

my @pack = grep { $_ ne '' } @array;

perldoc -f grep
for more information

Paul Lalli

John W. Krahn

2006-01-10, 4:02 am

Gerard Robin wrote:
> Hello,


Hello,

> I guess that one can write in more perlish fashion that I did: the part
> between the #### of this script to pack the array @array.
>
> Please, can someone give me some hint ?


You don't need to use an array for that:

my $string = "h\@a###p\$p??\$y n??e#w [?! \$ \% y]e{?]a##r? 2\*0\$0'\"6\# ! \^";

$string =~ tr/ !?#@$%"^'"][*{/ /sd;

print "$string\n";



John
--
use Perl;
program
fulfillment
Gerard Robin

2006-01-10, 4:02 am

On Mon, Jan 02, 2006 at 10:07:44AM -0800, Tom Phoenix wrote:

> my @pack;
> my $empties =3D 0;
> foreach my $item (@array) {
> if ($item eq '') { # empty string
> $empties++;
> } else {
> push @pack, $item;
> }
> }
>

Thanks, very nice. It's difficult (for me ;-)) to think in Perl ...
Thanks also for the other hints.
--=20
G=E9rard

DBSMITH@OhioHealth.com

2006-01-10, 4:02 am

what is this pack actually doing.... compressing the data in each eleme=
nt
or the entire array or both?


Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams




=

Gerard Robin =

<jag.robin18@wana =

doo.fr> =
To
beginners perl <beginners@perl.o=
rg>
01/02/2006 06:04 =
cc
PM =

Subj=
ect
Re: pack an array =

=

=

=

=

=

=





On Mon, Jan 02, 2006 at 10:07:44AM -0800, Tom Phoenix wrote:

> my @pack;
> my $empties =3D 0;
> foreach my $item (@array) {
> if ($item eq '') { # empty string
> $empties++;
> } else {
> push @pack, $item;
> }
> }
>

Thanks, very nice. It's difficult (for me ;-)) to think in Perl ...
Thanks also for the other hints.
--
G=E9rard


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


=


JupiterHost.Net

2006-01-10, 4:02 am



DBSMITH@OhioHealth.com wrote:
> what is this pack actually doing.... compressing the data in each element
> or the entire array or both?


Derek, I know you know about perldoc, I've seen it recommended to you
dozens of times :)

perldoc -f pack

Takes a LIST of values and converts it into a string using the
rules given by the TEMPLATE. The resulting string is the
concatenation of the converted values

I've also seen people ask you not to top post ;p
DBSMITH@OhioHealth.com

2006-01-10, 4:02 am


"JupiterHost.Net"
<mlists@jupiterho
st.net> To
beginners perl <beginners@perl.org>
01/03/2006 11:28 cc
AM
Subject
Re: pack an array















DBSMITH@OhioHealth.com wrote:
> what is this pack actually doing.... compressing the data in each element
> or the entire array or both?


Derek, I know you know about perldoc, I've seen it recommended to you
dozens of times :)

perldoc -f pack

Takes a LIST of values and converts it into a string using the
rules given by the TEMPLATE. The resulting string is the
concatenation of the converted values

I've also seen people ask you not to top post ;p

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


****************************************
********************************
****************************************
********************************

I know but sometimes I forget.
sorry and thx for the explan.


Dave Gray

2006-01-10, 4:02 am

On 1/2/06, John W. Krahn <krahnj@telus.net> wrote:
> Gerard Robin wrote:
>
> Hello,
>
>
> You don't need to use an array for that:
>
> my $string =3D "h\@a###p\$p??\$y n??e#w [?! \$ \% y]e{?]a##r? 2\*0\$0'\"6=

\# ! \^";
>
> $string =3D~ tr/ !?#@$%"^'"][*{/ /sd;
>
> print "$string\n";


I find it's easier to specify what I want to keep instead of what I
need to delete:

$string =3D~ s/[^A-Za-z0-9 ]//g;

So the above is pronounced "get rid of everything that isn't A-Z or
a-z or 0-9 or a space".
Paul Lalli

2006-01-10, 4:02 am

Dave Gray wrote:
> I find it's easier to specify what I want to keep instead of what I
> need to delete:
>
> $string =~ s/[^A-Za-z0-9 ]//g;
>
> So the above is pronounced "get rid of everything that isn't A-Z or
> a-z or 0-9 or a space".


Which, of course, would be better written:
$string =~ tr/A-Za-z0-9 //cd;

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com