[25760] in Perl-Users-Digest
Perl-Users Digest, Issue: 7999 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 21 06:05:44 2005
Date: Thu, 21 Apr 2005 03:05:16 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 21 Apr 2005 Volume: 10 Number: 7999
Today's topics:
$q->self_url Unreliable <jds@myob.com>
Re: $q->self_url Unreliable <jurgenex@hotmail.com>
Re: capture output from print command to variable in pe strepxe@yahoo.co.uk
Re: Cgi.pm and CSS <jwkenne@attglobal.net>
Re: Cgi.pm and CSS <horner.john@gmail.com>
Re: Delete characters <someone@example.com>
Re: Delete characters <alex@fortworks.com>
Re: Delete characters <alex@fortworks.com>
Re: Delete characters <emschwar@pobox.com>
Re: Delete characters <jl_post@hotmail.com>
Re: Delete characters <axel@white-eagle.invalid.uk>
Re: Delete characters <postmaster@castleamber.com>
Re: Delete characters (Anno Siegel)
Re: Delete characters <someone@example.com>
Re: Delete characters <postmaster@castleamber.com>
Re: Handling constants in Perl? newsbot@cox.net
Re: hotmail password request tool (intranet usage) <markbtownsend@comcast.net>
Re: How to call the remains of a pattern match <jure.simsic@mobitel.si>
Re: how to evaluate values in a hash <asolkar@gmail.com>
Re: how to evaluate values in a hash <monte_b1@yahoo.com>
Re: how to evaluate values in a hash <tadmc@augustmail.com>
Re: how to evaluate values in a hash <pilkowsk@informatik.uni-marburg.de>
Re: how to evaluate values in a hash <postmaster@castleamber.com>
How to test if a string is already in a array (Marc Eggenberger)
Re: How to test if a string is already in a array <postmaster@castleamber.com>
Re: How to test if a string is already in a array <m.sloyko@mostcom.ru>
Re: How to test if a string is already in a array <ebohlman@omsdev.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 21 Apr 2005 08:20:20 GMT
From: "Julia De Silva" <jds@myob.com>
Subject: $q->self_url Unreliable
Message-Id: <8nJ9e.11218$gz2.10812@fe3.news.blueyonder.co.uk>
Hi there all,
Is there a 100% reliable way to return the current URL of the script.
I have used $q->self_url but this doesn't seem to work on all servers. I'd
rather use cgi.pm than $ENV{} if poss.
TIA
J
------------------------------
Date: Thu, 21 Apr 2005 08:50:37 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: $q->self_url Unreliable
Message-Id: <xPJ9e.23072$ox3.19614@trnddc03>
Julia De Silva wrote:
> Is there a 100% reliable way to return the current URL of the script.
> I have used $q->self_url but this doesn't seem to work on all
> servers. I'd rather use cgi.pm than $ENV{} if poss.
Did you check the specification of the Common Gateway Interface if this
information is required?
jue
------------------------------
Date: 20 Apr 2005 16:12:52 -0700
From: strepxe@yahoo.co.uk
Subject: Re: capture output from print command to variable in perl
Message-Id: <1114038772.287031.289060@g14g2000cwa.googlegroups.com>
thanks all - appreciate the very helpful answers for a newbie .....
------------------------------
Date: Wed, 20 Apr 2005 22:09:06 -0400
From: "John W. Kennedy" <jwkenne@attglobal.net>
Subject: Re: Cgi.pm and CSS
Message-Id: <bXD9e.16052$ZQ1.8260@fe11.lga>
J. Gleixner wrote:
> When using CSS correctly, your final HTML will not contain any font or
> align elements or attributes, and if done really well, you won't have
> any table elements at all.
Whoa! CSS can replace abusive use of <TABLE>, but there are still such
things in this world as tables.
--
John W. Kennedy
"Those in the seat of power oft forget their failings and seek only the
obeisance of others! Thus is bad government born! Hold in your heart
that you and the people are one, human beings all, and good government
shall arise of its own accord! Such is the path of virtue!"
-- Kazuo Koike. "Lone Wolf and Cub: Thirteen Strings" (tr. Dana Lewis)
------------------------------
Date: 20 Apr 2005 19:37:40 -0700
From: "johnh" <horner.john@gmail.com>
Subject: Re: Cgi.pm and CSS
Message-Id: <1114051060.143820.244740@g14g2000cwa.googlegroups.com>
Nikos, you seem like a nice guy, but you keep asking questions which
make us think you don't understand what's really going on.
I want to tell you what everyone else has told you: if you want to
display web pages with Perl, use a templating system, and
HTML::Template is recommended.
If I was working with you, I'd tell you this:
* get your HTML and CSS right first;
* don't produce your HTML via CGI.pm and Perl;
* produce them as an HTML file;
That's stage one. And, you know what? Get someone else to do it if you
want. They don't have to understand Perl at all. Just get a friend
who's good at HTML/CSS to do you a good-looking page.
* when that file is the way you want it, come back to the Perl
newsgroups;
* then we will transform your HTML file into an HTML::Template file;
* then we will write a Perl script to display the Template with the
information in it;
This is the whole point of a Templating system. The HTML and the CSS
are in one place, and the script is in another. Different people can
work on different parts.
------------------------------
Date: Wed, 20 Apr 2005 22:11:14 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Delete characters
Message-Id: <6sA9e.45618$VF5.12966@edtnps89>
imphasing@gmail.com wrote:
> I need to delete the first five characters of a string. Is there an
> easy way to do this? It doesn't matter what character, I just need to
> delete the first 5. I have a string like this:
>
> 45ghsHello There!
>
> or maybe
>
> hf745Hello There!
>
> and I want to get rid of the 45ghs or hf745.
my $string = '45ghsHello There!';
$string = substr $string, 5;
Or:
$string = unpack 'x5 a*', $string
Or:
$string =~ s/^.{5}//s;
John
--
use Perl;
program
fulfillment
------------------------------
Date: 20 Apr 2005 15:16:38 -0700
From: "imphasing" <alex@fortworks.com>
Subject: Re: Delete characters
Message-Id: <1114035398.530605.129320@f14g2000cwb.googlegroups.com>
John W. Krahn wrote:
> imphasing@gmail.com wrote:
> > I need to delete the first five characters of a string. Is there an
> > easy way to do this? It doesn't matter what character, I just need
to
> > delete the first 5. I have a string like this:
> >
> > 45ghsHello There!
> >
> > or maybe
> >
> > hf745Hello There!
> >
> > and I want to get rid of the 45ghs or hf745.
>
> my $string = '45ghsHello There!';
>
> $string = substr $string, 5;
>
> Or:
>
thanks for all you're help guys! I got it to work.
Alex
> $string = unpack 'x5 a*', $string
>
> Or:
>
> $string =~ s/^.{5}//s;
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
------------------------------
Date: 20 Apr 2005 15:25:02 -0700
From: "imphasing" <alex@fortworks.com>
Subject: Re: Delete characters
Message-Id: <1114035902.252314.191500@l41g2000cwc.googlegroups.com>
now, does anyone know how I would delete all but the LAST 500 lines of
a text file?
I was thinking of a while loop, but my brain fizzled out, and I
couldn't figure out how to do it...any suggestions?
Thanks,
Alex
------------------------------
Date: Wed, 20 Apr 2005 16:31:02 -0600
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: Delete characters
Message-Id: <eto4qe1dqs9.fsf@wilson.emschwar>
"imphasing" <alex@fortworks.com> writes:
> now, does anyone know how I would delete all but the LAST 500 lines of
> a text file?
Use a circular buffer. Or File::ReadBackwards, I guess.
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
------------------------------
Date: 20 Apr 2005 16:07:50 -0700
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: Delete characters
Message-Id: <1114038470.762200.50940@o13g2000cwo.googlegroups.com>
imphasing wrote:
>
> now, does anyone know how I would delete all but
> the LAST 500 lines of a text file?
Here's a "quick and dirty" way to do it:
# Read the file contents into $text.
$text = $1 if $text =~ m/((.*\n){500})$/;
# Print $text back out to a file.
(This may not be the most efficient way to do what you want, but it
works.)
-- Jean-Luc
------------------------------
Date: Thu, 21 Apr 2005 01:45:27 -0500
From: Axel <axel@white-eagle.invalid.uk>
Subject: Re: Delete characters
Message-Id: <0I-dnSC6FPia0frfRVn-jg@adelphia.com>
imphasing <alex@fortworks.com> wrote:
> now, does anyone know how I would delete all but the LAST 500 lines of
> a text file?
> I was thinking of a while loop, but my brain fizzled out, and I
> couldn't figure out how to do it...any suggestions?
Maybe something like:
my $file = "qb";
my @line = split /\s+/, `wc -l $file`;
open INFILE, $file or die "Can't open file";
<INFILE> for (1 .. $line[1] - 500);
while (<INFILE>) {
print; # Or do something else
}
Axel
------------------------------
Date: 21 Apr 2005 08:37:42 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Delete characters
Message-Id: <Xns963F24978E956castleamber@130.133.1.4>
Axel wrote:
> imphasing <alex@fortworks.com> wrote:
>> now, does anyone know how I would delete all but the LAST 500 lines of
>> a text file?
>> I was thinking of a while loop, but my brain fizzled out, and I
>> couldn't figure out how to do it...any suggestions?
>
> Maybe something like:
>
> my $file = "qb";
> my @line = split /\s+/, `wc -l $file`;
Uhm, you have heard of tail?
man tail
[...]
-n, --lines=N
output the last N lines, instead of the last 10
[...]
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: 21 Apr 2005 08:38:47 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Delete characters
Message-Id: <d47oqn$hvf$3@mamenchi.zrz.TU-Berlin.DE>
jl_post@hotmail.com <jl_post@hotmail.com> wrote in comp.lang.perl.misc:
> imphasing wrote:
> >
> > now, does anyone know how I would delete all but
> > the LAST 500 lines of a text file?
>
>
> Here's a "quick and dirty" way to do it:
>
> # Read the file contents into $text.
> $text = $1 if $text =~ m/((.*\n){500})$/;
> # Print $text back out to a file.
>
> (This may not be the most efficient way to do what you want, but it
> works.)
It is more effort to read a file in a scalar variable than to read
the lines into an array. Since the problem is line-oriented, that
would be a better solution:
my @text = <FILE>;
print @text[ -500 .. -1];
Anno
------------------------------
Date: Thu, 21 Apr 2005 09:39:20 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Delete characters
Message-Id: <cxK9e.47242$vt1.37900@edtnps90>
Axel wrote:
> imphasing <alex@fortworks.com> wrote:
>
>>now, does anyone know how I would delete all but the LAST 500 lines of
>>a text file?
>>I was thinking of a while loop, but my brain fizzled out, and I
>>couldn't figure out how to do it...any suggestions?
>
>
> Maybe something like:
>
> my $file = "qb";
> my @line = split /\s+/, `wc -l $file`;
Why split() and the array?
chomp( my $line = `wc -l < $file` );
> open INFILE, $file or die "Can't open file";
> <INFILE> for (1 .. $line[1] - 500);
> while (<INFILE>) {
> print; # Or do something else
> }
John
--
use Perl;
program
fulfillment
------------------------------
Date: 21 Apr 2005 10:04:55 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Delete characters
Message-Id: <Xns963F336093750castleamber@130.133.1.4>
John W. Krahn wrote:
> Axel wrote:
>> imphasing <alex@fortworks.com> wrote:
>>
>>>now, does anyone know how I would delete all but the LAST 500 lines of
>>>a text file?
>>>I was thinking of a while loop, but my brain fizzled out, and I
>>>couldn't figure out how to do it...any suggestions?
>>
>>
>> Maybe something like:
>>
>> my $file = "qb";
>> my @line = split /\s+/, `wc -l $file`;
>
> Why split() and the array?
>
> chomp( my $line = `wc -l < $file` );
why chomp, why < ?, why using a scalar?
>> open INFILE, $file or die "Can't open file";
>> <INFILE> for (1 .. $line[1] - 500);
<INFILE> for 1 .. `wc -l $file` - 500;
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: 20 Apr 2005 18:34:07 -0700
From: newsbot@cox.net
Subject: Re: Handling constants in Perl?
Message-Id: <1114047247.958169.109070@f14g2000cwb.googlegroups.com>
Ok, this is what I get for retyping and not cutting and pasting... A
cardinal rule broken... (How many FAQs does it take?) Sorry.
TestMod.pm should have read:
=== TestK.pm ===
package TestMod;
use strict;
use warnings qw( all );
use TestK;
sub Test {
print __PACKAGE__ . ": TEST1 = " . TEST1 . "\n";
print __PACKAGE__ . ": TEST2 = " . TEST2 . "\n";
}
1;
__END__
(Cut and pasted this time.)
Which is how I ran it. THEN it gave me the error I originally
described.
I am using ActiveState Perl for Windows v5.8.4.
-ceo
------------------------------
Date: Wed, 20 Apr 2005 19:09:26 -0700
From: Mark Townsend <markbtownsend@comcast.net>
Subject: Re: hotmail password request tool (intranet usage)
Message-Id: <HYKdnenatPi2lvrfRVn-tQ@comcast.com>
>
> Just deleting all zips (or encrypted ones) is bloody stupid though.
>
Strangely enough a certain large software company relevant to at least
one of the ngs on this thread bans zip attachments in their email.
Instead the SOP is to drop the file onto a central database masquerading
as a file system, and then simply embed the link in the email rather
than attach it. This SOP works well for a number of reasons.
------------------------------
Date: Thu, 21 Apr 2005 06:52:19 GMT
From: "Jure Simsic" <jure.simsic@mobitel.si>
Subject: Re: How to call the remains of a pattern match
Message-Id: <D4I9e.11602$F6.2309329@news.siol.net>
Tad McClellan wrote:
> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
> > In article <Fzv9e.11590$F6.2306405@news.siol.net>, Jure Simsic
>
> >> &sub( ${what's left of $a} )
>
>
> > 2. There is no need to put ampersand (&) in front of function call.
>
>
> Unless you've been so silly as to use a reserved word as the
> function name, like this OP did.
In fact I didn't. It was just a code example, to make it as clear as
possible..
Thanx to everyone..
Jure
------------------------------
Date: Wed, 20 Apr 2005 15:15:53 -0700
From: Mahesh A <asolkar@gmail.com>
Subject: Re: how to evaluate values in a hash
Message-Id: <116dl4pgit9910e@corp.supernews.com>
monte wrote:
> i need to evalue hte values of a hash for a given key. I am new to perl
> and I need a little help. I am doing a hash where the key is the cell
> and the value is the lib but now that I got the values how to eval the
> values and have them print only once in the output.
> INPUT file format:
> lib0 cell1
> lib2 cell3
> lib2 cell4
> lib3 cell3
> lib0 cell1
> lib2 cell4
> lib2 cell4
> etc..
>
> DESIRED ouput:
> cell1 lib0 (lib0 should only appears once in output)
> cell3 lib2 lib3 (this is an error)
> cell4 lib2
>
Like you ensured the uniqueness of cells by making them keys of a hash,
why not also make libs keys of a nested hash, like so:
-----
#!/usr/bin/perl
use strict;
use warnings;
my $rec = {};
while (<DATA>) {
my ($lib, $cell) = split;
$rec->{$cell}->{$lib} = 1;
}
foreach my $cell (sort keys %$rec) {
print "$cell (" . (join ", ", sort keys %{$rec->{$cell}}) . ")",
(keys %{$rec->{$cell}} > 1) ? " - Error?" : "",
"\n";
}
__END__
lib0 cell1
lib2 cell3
lib2 cell4
lib3 cell3
lib0 cell1
lib2 cell4
lib2 cell4
-----
Output:
cell1 (lib0)
cell3 (lib2, lib3) - Error?
cell4 (lib2)
-----
'$rec->{$cell}->{$lib} = 1' will make sure that cells and libs are unique.
Assuming that I read the error scenario correctly - if a cell has more
than one keys (more than one libs associated) an error can be flagged.
HTH,
Mahesh.
--
Mahesh Asolkar
asolkar-at-gmail-dot-com
------------------------------
Date: 20 Apr 2005 17:28:19 -0700
From: "monte" <monte_b1@yahoo.com>
Subject: Re: how to evaluate values in a hash
Message-Id: <1114043299.798543.311900@o13g2000cwo.googlegroups.com>
yes, Mahesh, I see what you're doing, sorry for not putting as much of
my code as possible, will do next time I ask a basic question to you. I
will make the libs to be a hash, too. I will keep you updated if i ran
into more trouble but for now, thanks for all of you guys help! I know
it is too late to post my code but here it is.
my code:
while(<>)
{
chomp;
$show_file=$_;
next if ( $show_file=~/\#/);
#print $show_file;
if ($show_file=~/^\S+\s+\d\.\s+(\S+)\s+(\S+)/) #this is the parsing
pice to get my cell and lib
{
$lib=$1 if defined ($1);
$cell=$2 if defined ($2);
#checking if the same cell exist in the same heirarchy.key=>cell,
value=>lib
($rec{$cell}= "Cell:$cell Lib:") if (! exists($rec{$cell}));
$rec{$cell}.="$lib\t";
}}
foreach $entry (keys %rec)
{
print" $rec{$entry}\n";
}
------------------------------
Date: Wed, 20 Apr 2005 20:14:27 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: how to evaluate values in a hash
Message-Id: <slrnd6dvjj.6il.tadmc@magna.augustmail.com>
monte <monte_b1@yahoo.com> wrote:
> sorry for not putting as much of
> my code as possible, will do next time
Have you seen the Posting Guidelines that are posted here frequently?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 21 Apr 2005 11:19:26 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: how to evaluate values in a hash
Message-Id: <3cp9fgF6lqhgnU1@individual.net>
* monte schrieb:
> yes, Mahesh, I see what you're doing, sorry for not putting as much of
> my code as possible, will do next time I ask a basic question to you. I
> will make the libs to be a hash, too. I will keep you updated if i ran
> into more trouble but for now, thanks for all of you guys help! I know
> it is too late to post my code but here it is.
>
> my code:
> while(<>) {
> chomp;
You don't need to chomp here. You're just matching against your string
without paying attention to any line endings.
> $show_file=$_;
> next if ( $show_file=~/\#/);
Do you really need a named var $show_file, just a copy of $_? Consider,
the m//-operator you're using here matchs against $_ per default. Btw,
there's no need to escape »#«. Hence you could simply write:
next if /#/;
> #print $show_file;
> if ($show_file=~/^\S+\s+\d\.\s+(\S+)\s+(\S+)/) #this is the parsing
> pice to get my cell and lib
Delete the »$show_file=~« part in this line. Then you're matching
against $_ as described above. It's the same, but shorter ;-)
> {
> $lib=$1 if defined ($1);
> $cell=$2 if defined ($2);
By enclosing your regex by an »if« you already guarantee that the regex
has matched. Furthermore the vars $1 and $2 are defined, surely. There's
no need to check this again. Btw, are you using »strict« and »warnings«?
my( $lib, $cell ) = ( $1, $2 );
>
> #checking if the same cell exist in the same heirarchy.key=>cell,
> value=>lib
>
> ($rec{$cell}= "Cell:$cell Lib:") if (! exists($rec{$cell}));
You could remove some parantheses to provide more readability.
$rec{$cell} = "Cell:$cell Lib:" if ! exists $rec{$cell};
Btw, you don't need to check if the key already exists(). You could use
the operator »||« (»or«) to assign a value if there isn't one.
$rec{$cell} = $rec{cell} || "Cell:$cell Lib:";
But, in Perl that's not short enough -- we could shorten to:
$rec{$cell} ||= "Cell:$cell Lib:";
> $rec{$cell}.="$lib\t";
> }}
Ok, the closing brackets for while loop and if condition. In one line.
Btw, your coding style isn't the best I ever read. Consider to indent
your lines to separate different blocks. Use more spaces and newlines
wherever you think they could improve the readability.
> foreach $entry (keys %rec) {
> print" $rec{$entry}\n";
> }
You know about values() to get the values and not the keys?
print " $_\n" for values %rec;
regards,
fabian
------------------------------
Date: 21 Apr 2005 09:44:42 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: how to evaluate values in a hash
Message-Id: <Xns963F2FF4034B9castleamber@130.133.1.4>
Fabian Pilkowski wrote:
> * monte schrieb:
>> ($rec{$cell}= "Cell:$cell Lib:") if (! exists($rec{$cell}));
>
> You could remove some parantheses to provide more readability.
>
> $rec{$cell} = "Cell:$cell Lib:" if ! exists $rec{$cell};
$rec{$cell} = "Cell:$cell Lib:" unless exists $rec{$cell};
I love unless :-)
or:
exists $rec{$cell} or $rec{$cell} = "Cell:$cell Lib:";
OTOH, just overwriting is also possible in this case.
> Btw, you don't need to check if the key already exists(). You could
> use the operator »||« (»or«) to assign a value if there isn't one.
>
>
> $rec{$cell} = $rec{cell} || "Cell:$cell Lib:";
>
> But, in Perl that's not short enough -- we could shorten to:
>
> $rec{$cell} ||= "Cell:$cell Lib:";
There are cases that this use of "or" gives very unexpected results.
This is not such a case, but be aware, e.g.
$filename ||= 'default.txt';
now imagine $filename is 0, and 0 is a legal name...
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: 21 Apr 2005 02:48:16 -0700
From: marc.eggenberger@itc.alstom.com (Marc Eggenberger)
Subject: How to test if a string is already in a array
Message-Id: <f0f59aa9.0504210148.3519f832@posting.google.com>
Hi there.
I havent done perl coding for some years now and forgot a lot so bear
with me ...
I open a text file, read it line by line and do some splitting and
substr to get an emailadress of the line. Now the textfile has some
10k lines and a lot of dublicate mail addresses. I only need each
emailaddress once .. a bit like a select distinct emailadress would be
in SQL.
My though now was to create a array and test if the address is already
in the array and if not push it into the array. I dont need to have
the position of the address in the array ... so I thought of something
like
if(! exists $address_array[$address]=
{
push(@address_array,$address);
}
this of course does not work ...
how would I achive my goal?
Thanks for any help
Marc
------------------------------
Date: 21 Apr 2005 09:57:34 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: How to test if a string is already in a array
Message-Id: <Xns963F322287CC7castleamber@130.133.1.4>
Marc Eggenberger wrote:
> Hi there.
>
> I havent done perl coding for some years now and forgot a lot so bear
> with me ...
> I open a text file, read it line by line and do some splitting and
> substr to get an emailadress of the line. Now the textfile has some
> 10k lines and a lot of dublicate mail addresses. I only need each
> emailaddress once .. a bit like a select distinct emailadress would be
> in SQL.
>
> My though now was to create a array and test if the address is already
> in the array and if not push it into the array. I dont need to have
> the position of the address in the array ... so I thought of something
> like
>
> if(! exists $address_array[$address]=
> {
> push(@address_array,$address);
> }
>
> this of course does not work ...
>
> how would I achive my goal?
my %address_hash;
and in your loop:
$address_hash{ $address } = 1;
( no need for the test thing )
keys %address_hash gives the unique addresses.
BTW: put use strict; use warnings; on top of your script.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Thu, 21 Apr 2005 14:01:27 +0400
From: Maxim <m.sloyko@mostcom.ru>
Subject: Re: How to test if a string is already in a array
Message-Id: <d47tlr$r6a$1@domitilla.aioe.org>
>
> if(! exists $address_array[$address])
> {
> push(@address_array,$address);
> }
Checking every time the string in array would yield O(n^2) complexity.
The easiest way (I guess) is to do the following: (which is O(n*log n) )
my %address_hash;
if( ! exists $address_hash{$address} )
{
$address_hash{$address} = 1;
}
my @address_array = keys %address_hash;
Hope this helps
--
Maxim Sloyko
------------------------------
Date: 21 Apr 2005 10:02:24 GMT
From: Eric Bohlman <ebohlman@omsdev.com>
Subject: Re: How to test if a string is already in a array
Message-Id: <Xns963F34AFB9280ebohlmanomsdevcom@130.133.1.4>
marc.eggenberger@itc.alstom.com (Marc Eggenberger) wrote in
news:f0f59aa9.0504210148.3519f832@posting.google.com:
> I havent done perl coding for some years now and forgot a lot so bear
> with me ...
>
> I open a text file, read it line by line and do some splitting and
> substr to get an emailadress of the line. Now the textfile has some
> 10k lines and a lot of dublicate mail addresses. I only need each
> emailaddress once .. a bit like a select distinct emailadress would be
> in SQL.
When you start saying words like "duplicate" or "distinct," you should be
immediately thinking "hash."
> My though now was to create a array and test if the address is already
> in the array and if not push it into the array. I dont need to have
> the position of the address in the array ... so I thought of something
> like
>
> if(! exists $address_array[$address]=
> {
> push(@address_array,$address);
> }
>
> this of course does not work ...
>
> how would I achive my goal?
my %addresses;
...
$addresses{$address}=1;
...
foreach my $address (keys %addresses) {
#do something with the address
}
------------------------------
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 7999
***************************************