[29998] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1241 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 30 00:09:41 2008

Date: Tue, 29 Jan 2008 21:09:07 -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, 29 Jan 2008     Volume: 11 Number: 1241

Today's topics:
        "negative" regexp <stoupa@practisoft.cz>
    Re: "negative" regexp <abigail@abigail.be>
        domains, top level, collecting a list of wardbayern@yahoo.com
    Re: domains, top level, collecting a list of <usenet@davidfilmer.com>
    Re: domains, top level, collecting a list of <someone@example.com>
    Re: Get an arbitrary hash key, quickly. <ben@morrow.me.uk>
        List of directories within a directory <nospam@nospam.com>
    Re: List of directories within a directory <someone@example.com>
    Re: List of directories within a directory <ben@morrow.me.uk>
    Re: List of directories within a directory <nospam@nospam.com>
    Re: List of directories within a directory <uri@stemsystems.com>
    Re: List of directories within a directory <jurgenex@hotmail.com>
    Re: Odd (undocumented?) behavior of RAM file within a l <john@castleamber.com>
    Re: Odd (undocumented?) behavior of RAM file within a l <1usa@llenroc.ude.invalid>
    Re: Odd (undocumented?) behavior of RAM file within a l <ben@morrow.me.uk>
    Re: scalar to array? <jimsgibson@gmail.com>
    Re: scalar to array? <jurgenex@hotmail.com>
        ssh into remote nodes, do mulitple commands <rodbass63@gmail.com>
    Re: uniq without sort  <-------------- GURU NEEDED <bik.mido@tiscalinet.it>
    Re: uniq without sort  <-------------- GURU NEEDED <jurgenex@hotmail.com>
    Re: Using Imager module to resample images <mgjv@tradingpost.com.au>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 30 Jan 2008 02:10:04 +0100
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: "negative" regexp
Message-Id: <fnoj8a$1hin$1@ns.felk.cvut.cz>

I have problem to construct regexp, this is out of my brain ;-) please help.

I have string

$string="<a href='big.gif'><img src='small.gif'></a><br><a 
href='abc.htm'><b>click</b></a>";

and I need to remove all html tags except <img ...>. The result should be

$string="<img src='small.gif'>click";

Now I do it this way

# replace < with Ctrl-B and > with Ctrl-E for all <img> tags
$string=~s/<img\s+(.+?)>/\cb$1\ce/g;
# remove all html tags
$string=~s/<.+?>//g;
# replace back all Ctrl-B with <
$string=~s/\cb/</g;

# replace back all Ctrl-E with >
$string=~s/\ce/>/g;

but maybe exist another way.
-- 
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail from 
another non-spammer site please.)

Please reply to <petr AT practisoft DOT cz>



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

Date: 30 Jan 2008 02:32:46 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: "negative" regexp
Message-Id: <slrnfpvoee.bfu.abigail@alexandra.abigail.be>

                                            _
Petr Vileta (stoupa@practisoft.cz) wrote on VCCLXV September MCMXCIII in
<URL:news:fnoj8a$1hin$1@ns.felk.cvut.cz>:
;;  I have problem to construct regexp, this is out of my brain ;-) please help.
;;  
;;  I have string
;;  
;;  $string="<a href='big.gif'><img src='small.gif'></a><br><a 
;;  href='abc.htm'><b>click</b></a>";
;;  
;;  and I need to remove all html tags except <img ...>. The result should be
;;  
;;  $string="<img src='small.gif'>click";
;;  
;;  Now I do it this way
;;  
;;  # replace < with Ctrl-B and > with Ctrl-E for all <img> tags
;;  $string=~s/<img\s+(.+?)>/\cb$1\ce/g;
;;  # remove all html tags
;;  $string=~s/<.+?>//g;
;;  # replace back all Ctrl-B with <
;;  $string=~s/\cb/</g;
;;  
;;  # replace back all Ctrl-E with >
;;  $string=~s/\ce/>/g;
;;  
;;  but maybe exist another way.


Well, for your example,

    s/<(?!img)[^>]*>//g

ought to do it (untested).

But that assumes no '>' is present inside a tag, which doesn't have
to be the case.

The "right" way to do it is to use a proper HTML parser.

Get one from your nearest CPAN.



Abigail
-- 
perl -swleprint -- -_=Just\ another\ Perl\ Hacker


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

Date: Tue, 29 Jan 2008 12:37:08 -0800 (PST)
From: wardbayern@yahoo.com
Subject: domains, top level, collecting a list of
Message-Id: <3e980cbe-c7fd-48ef-9474-6e443e4f7fc7@t1g2000pra.googlegroups.com>

Is there a perl way, verses finding a published list, to collect
current top level domain names?

Thanks.


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

Date: Tue, 29 Jan 2008 13:14:46 -0800
From: David Filmer <usenet@davidfilmer.com>
Subject: Re: domains, top level, collecting a list of
Message-Id: <FJ6dne1GurebCgLaRVn_vwA@giganews.com>

wardbayern@yahoo.com wrote:
> Is there a perl way, verses finding a published list, to collect
> current top level domain names?

http://search.cpan.org/~alexp/Net-Domain-TLD-1.67/lib/Net/Domain/TLD.pm

-- 
David Filmer (http://DavidFilmer.com)


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

Date: Tue, 29 Jan 2008 23:34:55 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: domains, top level, collecting a list of
Message-Id: <zKOnj.1432$cc3.676@edtnps82>

wardbayern@yahoo.com wrote:
> Is there a perl way, verses finding a published list, to collect
> current top level domain names?

$ perl -MSocket -le'
# Search from 1.0.0.1 through 255.255.255.254
for ( my $ip = 16_777_217; $ip < 4_294_967_295; ++$ip ) {
     # Skip 127.0.0.0 through 127.255.255.255 

     next if $ip >= 2_130_706_432 && $ip <= 2_147_483_647; 

     # Skip 10.0.0.0 through 10.255.255.255 -- RFC1918 

     next if $ip >= 167_772_160 && $ip <= 184_549_375; 

     # Skip 172.16.0.0 through 172.31.255.255 -- RFC1918 

     next if $ip >= 2_886_729_728 && $ip <= 2_887_778_303; 

     # Skip 192.168.0.0 through 192.168.255.255 -- RFC1918 

     next if $ip >= 3_232_235_520 && $ip <= 3_232_301_055; 

 

     $TLD{ lc( $1 ) }++ if gethostbyaddr( pack( "N", $ip ), AF_INET ) =~ 
/\.(\w+)\z/;
     }
print for sort keys %TLD;
'


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


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

Date: Tue, 29 Jan 2008 17:32:02 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Get an arbitrary hash key, quickly.
Message-Id: <ih4475-opc.ln1@osiris.mauzo.dyndns.org>


Quoth xhoster@gmail.com:
> 
> while (keys %hash) {
>    foreach my $to_do (keys %hash) {
>       delete $hash{$to_do};
>       ## do stuff with $to_do, which might
>       # make new entries in %hash
>    }
> }
> (Causes one extra scalar "keys %hash", but that shouldn't be a problem)
> 
> The "do stuff" can only add new entries, not remove entries (other than
> $to_do itself) without causing problems.  Deleting wasn't part of the
> original specification, and that limitation is not a problem in this
> particular case.

Fixing that is just a matter of

    delete $hash{$to_do} or next;

isn't it? Of course, if you delete lots of entries you're going to waste
time spinning round that loop.

Ben



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

Date: Tue, 29 Jan 2008 21:00:27 -0500
From: Al Moodie <nospam@nospam.com>
Subject: List of directories within a directory
Message-Id: <jgmvp3ts5b68nd361clv2lbaf216u0a7j8@4ax.com>

I have a directory with 200 sub directories in it. How do I create a
list of the sub directory names?

I know how create a list of all the files in a directory:

opendir(DIR, $dirname) or die "can't open $dirname: $!";
while (defined($file = readdir(DIR))) {
	next if($file =~ m/^\./);
	next if($file eq "");
	push (@filenames, $file);
}
closedir(DIR);

but how do I do it for the directories within a directory

Al Moodie.


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

Date: Wed, 30 Jan 2008 02:30:16 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: List of directories within a directory
Message-Id: <YiRnj.52$C61.11@edtnps89>

Al Moodie wrote:
> I have a directory with 200 sub directories in it. How do I create a
> list of the sub directory names?
> 
> I know how create a list of all the files in a directory:
> 
> opendir(DIR, $dirname) or die "can't open $dirname: $!";
> while (defined($file = readdir(DIR))) {
> 	next if($file =~ m/^\./);
> 	next if($file eq "");

"" is not a valid file name so this test will *always* fail.

$ mkdir ''
mkdir: cannot create directory `': No such file or directory
$ touch ''
touch: cannot touch `': No such file or directory

> 	push (@filenames, $file);

Since you didn't test the file type your @filenames array contains all 
types including subdirectories.

> }
> closedir(DIR);
> 
> but how do I do it for the directories within a directory

my @dir_names = grep -d "$dirname/$_", readdir DIR;



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


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

Date: Wed, 30 Jan 2008 02:26:43 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: List of directories within a directory
Message-Id: <3s3575-srt.ln1@osiris.mauzo.dyndns.org>


Quoth Al Moodie <nospam@nospam.com>:
> I have a directory with 200 sub directories in it. How do I create a
> list of the sub directory names?

Recursively, or just the immediate subdirectories?

> I know how create a list of all the files in a directory:
> 
> opendir(DIR, $dirname) or die "can't open $dirname: $!";
> while (defined($file = readdir(DIR))) {
> 	next if($file =~ m/^\./);
> 	next if($file eq "");

For just the immediate subdirs you need

        next if !-d "$dirname/$file";

or possibly

        next if -l "$dirname/$file" or !-d _;

depending on your definition of 'directory', or the equivalent with
File::Spec if you want to be more portable (your exclusion of .*
suggests you are on Unix). For finding files (directories are just
files) recursively, use File::Find or one of the more modern
alternatives like File::Find::Rule.

Ben



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

Date: Tue, 29 Jan 2008 21:41:57 -0500
From: Al Moodie <nospam@nospam.com>
Subject: Re: List of directories within a directory
Message-Id: <gqovp3l0ndes1sv7rqoc74jb22p2nru49i@4ax.com>

On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
<someone@example.com> wrote:

>Al Moodie wrote:
>> I have a directory with 200 sub directories in it. How do I create a
>> list of the sub directory names?

>> 
>> but how do I do it for the directories within a directory
>
>my @dir_names = grep -d "$dirname/$_", readdir DIR;

That is what I need. I want to go into each sub directory in turn and
delete specific files.

Thanks also for the general education

Al.


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

Date: Wed, 30 Jan 2008 04:22:12 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: List of directories within a directory
Message-Id: <x7tzkwkknf.fsf@mail.sysarch.com>

>>>>> "AM" == Al Moodie <nospam@nospam.com> writes:

  AM> On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
  AM> <someone@example.com> wrote:

  >> Al Moodie wrote:
  >>> I have a directory with 200 sub directories in it. How do I create a
  >>> list of the sub directory names?

  >>> 
  >>> but how do I do it for the directories within a directory
  >> 
  >> my @dir_names = grep -d "$dirname/$_", readdir DIR;

  AM> That is what I need. I want to go into each sub directory in turn and
  AM> delete specific files.

then you should use File::Find or one of the variants. scanning dirs
deeply by yourself has all sorts of little bugs waiting to happen.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Architecture, Development, Training, Support, Code Review  ------
-----------  Search or Offer Perl Jobs  ----- http://jobs.perl.org  ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Wed, 30 Jan 2008 04:36:32 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: List of directories within a directory
Message-Id: <h9vvp3lnbgpgerq5hhhfepfv7dvfljujf0@4ax.com>

Al Moodie <nospam@nospam.com> wrote:
>I have a directory with 200 sub directories in it. How do I create a
>list of the sub directory names?

Didn't I just read exactly the same question from a name sake of yours in a
different NG? You may want to read up on the difference between
cross-posting (which is rarely appropriate) and multi-posting (which is
never justified).

Same answer as there: I would use File::Find and prune at a depth of 2.

jue


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

Date: 29 Jan 2008 19:20:32 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Odd (undocumented?) behavior of RAM file within a loop
Message-Id: <Xns9A3487B99EDF3castleamber@130.133.1.4>

David Filmer <usenet@davidfilmer.com> wrote:
 
>     my $file_in_memory = '';
> 
> then I have no problems (except I bust the second rule of chapter 4 in
> Dr. Damian's _PBP_ - yikes).


my $file_in_memory = q{};  # Empty string


It will probably take quite some time before I get used to that one.

-- 
John

http://johnbokma.com/mexit/


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

Date: Tue, 29 Jan 2008 20:53:37 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Odd (undocumented?) behavior of RAM file within a loop
Message-Id: <Xns9A34A1ADFD085asu1cornelledu@127.0.0.1>

David Filmer <usenet@davidfilmer.com> wrote in
news:2eOdndq_7J5h6ALaRVn_vwA@giganews.com: 

> Greetings.  I am writing a program which processes some files within a
> loop, and I am using the ability (as of Perl 5.8) to open a filehandle
> on a scalar reference (which I recycle in the loop).  Kindly consider 
> this stripped-down trivial code example which illustrates my question:
> 
> #!/usr/bin/perl
>     use strict; use warnings;
> 
>     foreach (1..3) {
>        warn "Pass #$_\n";
>        my $file_in_memory;
>        open(my $fh, '>', \$file_in_memory) or die "Oops - $!\n";
>        close $fh;
>     }
> 
> __END__
> 
> 
> It works fine for the first pass.  However, subsequent passes emit
> warnings: 
> 
>     Pass #1
>     Pass #2
>     Use of uninitialized value in open at /perl/blah line 7.
>     Pass #3
>     Use of uninitialized value in open at /perl/blah line 7.
> 
> (line 7 is the "open" statement). Sure enough, if I make this change
> to "initialize" the scalar:

I found it curious that the warning disappears if I commented out the 
close statement (on AS Perl 5.10 on Win32).

So, I looked a little closer:

C:\Temp> cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

for my $pass ( 1 .. 3 ) {
    warn "Pass #$pass\n";
    open my $fh, '>', \(my $buffer) or die $!;
    print \$buffer, "\n";
    close $fh or die $!
}

__END__

C:\DOCUME~1\asu1\LOCALS~1\Temp> t
Pass #1
SCALAR(0x182b04c)
Pass #2
Use of uninitialized value $buffer in open at C:\Temp\t.pl line 8.
SCALAR(0x182b04c)
Pass #3
Use of uninitialized value $buffer in open at C:\Temp\t.pl line 8.
SCALAR(0x182b04c)

With the close statement commented out, I get:

C:\Temp> t
Pass #1
SCALAR(0x182b04c)
Pass #2
SCALAR(0x22ae14)
Pass #3
SCALAR(0x22ae24)

Note that the explicit close results in the reference to the same 
memory area being reused whereas, without the explicit close, the
reference changes.

I do not know if this behavior is peculiar to my platform or if it
is documented in some way. I would have expected the behavior to be
the same with or without the explicit close.

I also tried this with Cygwin Perl 5.8.8 and got the following:

Without explicit close:

> perl t.pl
Pass #1
SCALAR(0x10070f2c)
Pass #2
SCALAR(0x100671c4)
Pass #3
SCALAR(0x10070f2c)

With explicit close:

> perl t.pl
Pass #1
SCALAR(0x10070f2c)
Pass #2
Use of uninitialized value in open at t.pl line 8.
SCALAR(0x10070f2c)
Pass #3
Use of uninitialized value in open at t.pl line 8.
SCALAR(0x10070f2c)

Hmmmmm ...

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>



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

Date: Wed, 30 Jan 2008 02:11:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Odd (undocumented?) behavior of RAM file within a loop
Message-Id: <8v2575-srt.ln1@osiris.mauzo.dyndns.org>


Quoth "A. Sinan Unur" <1usa@llenroc.ude.invalid>:
> 
> I found it curious that the warning disappears if I commented out the 
> close statement (on AS Perl 5.10 on Win32).
> 
> So, I looked a little closer:
> 
> C:\Temp> cat t.pl
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> for my $pass ( 1 .. 3 ) {
>     warn "Pass #$pass\n";
>     open my $fh, '>', \(my $buffer) or die $!;
>     print \$buffer, "\n";
>     close $fh or die $!
> }
> 
> __END__
> 
> C:\DOCUME~1\asu1\LOCALS~1\Temp> t
> Pass #1
> SCALAR(0x182b04c)
> Pass #2
> Use of uninitialized value $buffer in open at C:\Temp\t.pl line 8.
> SCALAR(0x182b04c)
> Pass #3
> Use of uninitialized value $buffer in open at C:\Temp\t.pl line 8.
> SCALAR(0x182b04c)

This seems to be an bug in PerlIO::scalar. There is a check in scalar.xs
that is supposed to avoid this warning; however, the code checks
SvTYPE(sv) > SVt_NULL ('this scalar has never been allocated') rather
than SvOK(sv) ('this scalar has a defined value'). When a lexical is
reused the former is true but the latter is false, so the warning gets
through. This means that

    my $x = 3;
    undef $x;
    open my $F, '>', \$x;

will warn as well, which it also (IIUC) shouldn't.

I've done up a patch, which I'll send to p5p when I've tested it.

> With the close statement commented out, I get:
> 
> C:\Temp> t
> Pass #1
> SCALAR(0x182b04c)
> Pass #2
> SCALAR(0x22ae14)

This makes less sense... I suspect what's happening here is that the
filehandle isn't being closed until *after* $buffer gets allocated for
the next iteration, so it is forced to allocate a new scalar since the
old one's still in use. This is arguably wrong, since the whole point is
to reuse the old scalar if we can, for efficiency.

Ben



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

Date: Tue, 29 Jan 2008 11:13:52 -0800
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: scalar to array?
Message-Id: <290120081113521615%jimsgibson@gmail.com>

In article <MKadnS_EYNTD6ALanZ2dnUVZ_vyinZ2d@insightbb.com>, monkeys
paw <user@example.net> wrote:

> What is the easiest way to push a scalar variable containing text into 
> an array?

So close! If only you had asked ' ... to split a scalar variable ...'.
Then you would have asked a "self-answering question" (SAQ).
<http://www.ginini.com/perlsaq.html>

> 
> i.e.
> 
> $text = 'here is
> a line
> that has
> four lines';
> 
> @lines = ???
> 
> the end result is:
> 
> @lines =('here is', 'a line', 'that has', 'four lines');

  @lines = split(/\n/,$text);

-- 
Jim Gibson

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: Tue, 29 Jan 2008 19:39:52 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: scalar to array?
Message-Id: <povup3lm427dpe1i5sqdi4iklg38jvc6n9@4ax.com>

monkeys paw <user@example.net> wrote:
>What is the easiest way to push a scalar variable containing text into 
>an array?

That is a SAQ: perldoc -f push 

>i.e.
>
>$text = 'here is
>a line
>that has
>four lines';
>
>@lines = ???
>
>the end result is:
>
>@lines =('here is', 'a line', 'that has', 'four lines');

But this has nothing to do with pushing a single scalar into an array as you
asked for at the beginning.

Just split// the text at newline and then push() or splice() the new
elements into the array. Or you could use an array slice instead, too. 

jue


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

Date: Tue, 29 Jan 2008 17:32:41 -0800 (PST)
From: Nene <rodbass63@gmail.com>
Subject: ssh into remote nodes, do mulitple commands
Message-Id: <f9671ce2-57e0-4faa-8e7b-b131641e3c73@l32g2000hse.googlegroups.com>

Hello,

I'm trying to use Net::SSH to ssh into 3 nodes and move/copy files
with File::Copy. For now, I scripted it  to find files of a certain
age and move them.
The $cmd variable is working, but when I try to move the files, it
errors with copy failed. Please critique or suggest a better way.
Thanks in advance!!!!
######################################################

#!/usr/bin/perl -w
use Net::SSH::Perl qw(sshopen2);
use strict;


my %LIST = (

1 => "node1",
2 => "node1",
3 => "node1"

                 );

foreach my $ESP (sort values %LIST) {


my $user = "user1";
my $host = $ESP;
my $cmd = "sudo find /somedir -name file.err  -mtime 1 -exec ls -lt {}
';'";



sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
print "Connected to $host\n";

while (<READER>) {

if  ( $_ ) {
use File::Copy;

move("/tmp/rod","/tmp/nasty") or die "Copy failed: $!";

}

}
}



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

Date: Tue, 29 Jan 2008 23:47:38 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: uniq without sort  <-------------- GURU NEEDED
Message-Id: <p4bvp316noodrltggsvldl1do526ru3oiu@4ax.com>

On Tue, 29 Jan 2008 17:03:42 +0100, Michael Tosch
<eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:

>This is "print first unique".
>For "print last unique" you really need perl:
>
>perl -ne '$s{$_}=++$i; END {print sort {$s{$a}<=>$s{$b}} keys %s}'

Incidentally, you may want to know about $., see perldoc perlvar.


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, 29 Jan 2008 23:38:37 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: uniq without sort  <-------------- GURU NEEDED
Message-Id: <81evp3t8f97l2i4sls32k0kuk7ht50c8ng@4ax.com>

Michael Tosch <eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:
>Michele Dondi wrote:
>> On Thu, 24 Jan 2008 18:45:24 -0800 (PST), gnuist006@gmail.com wrote:
>>> I want uniq without sorting the initial order.

>> In shell I don't know. In Perl it's well known to be as trivial as
>> 
>>   perl -ne 'print unless $saw{$_}++' file
[...]
>This is "print first unique".
>For "print last unique" you really need perl:
>
>perl -ne '$s{$_}=++$i; END {print sort {$s{$a}<=>$s{$b}} keys %s}'

Or just read slurp the whole file into an array, reverse(), and then use
Michele's suggestion.

jue


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

Date: Wed, 30 Jan 2008 08:32:59 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Using Imager module to resample images
Message-Id: <slrnfpv6sb.re9.mgjv@martien.heliotrope.home>

On Tue, 29 Jan 2008 03:08:11 -0800 (PST),
	Jason Carlton <jwcarlton@gmail.com> wrote:
> I've been forcing my visitors to resample their images manually to
> keep them below 50kb, but this has only had so-so results. A lot of my
> visitors don't understand how to resample their image, so I'm getting
> high-resolution images that are something like 20px x 20px, but still
> take up 50kb!

There's always the possibility to advise people to use one of the many
online services that are available for image resizing. Typing 'image
resize' in a well known search engine gave me quite a few links. 

> So, I've been thinking about resampling the images on the fly. The
> images would be restricted to JPG, JPEG, GIF, and PNG.
>
> I was planning to use a combination of Image::Resize and Imager, which
> I've never used before, but since the load on the server is ALWAYS
> high, I had a few questions first:
>
> 1. Will the use of Imager take up a lot of CPU resources?

Image manipulation always takes up a reasonable amount of CPU, as well
as memory. Whether it takes up "a lot" depends on both your expectations
and definition of a lot, and on the size of the images.

> 2. How do I recognize an animated GIF from a static GIF?

I don't know of a way with Imager, but there might be one. There's
informaton in the Imager::Files documentation about the tags Imager
will write, but I am not sure whether it will tell you that the input
image was an animated one. Imager combines animated GIFs into a single
RGB image.

You can do this with Image::Magick, by looking at the number of images
in the object, but you're not using that module, and it would be silly
to install it only for this.

Depending on what you want to do, there are other possible alternatives:

A quick search on CPAN showed up Image::DeAnim, which you could use
first to get rid of any animations, if you don't like the way Imager
does it, and if that's all you really want. 

Alternatively, Image::ParseGIF should possibly be able to give you
the information directly, after which you can take whatever action you
would like to take. Note that if that action is to simply combine the
images, you might as well skip this step, and load it into Imager right
away.

Otherwise you can simply write your own miniparser with unpack to parse
the file yourself. The GIF file formats are described well all over the
Internet. www.wotsit.org is not a bad place to start, I generally find.
I doubt you would have to resort to this. I have forgotten whether this
information can be read from the image header, or whether you actually
have to parse the whole filei, or at least until the second frame.

> 3. Does Imager compress JPG images automatically? I didn't see an
> option to set a compression level in the docs, but it may be called
> something else that I'm not recognizing.

Check the Imager::Files documentation, which documents the file formats
Imager takes, and specific methods and options for each.

> 4. I assume that I'll need to use Image::Resize to get the current
> width and height, and if it exceeds the maximum that I input then I'll
> use Imager to resample it. Is there a smarter (read: simpler and/or
> less resource-heavy) option that this?

Why not simply use Imager for the whole lot? If you're trying to avoid
the overhead of that, Image::Size is probably the faster method. If you
don't want to use that module, it's not hard to write your own image
header parsing routines if you only support a small number of formats.

Regards,
Martien
-- 
                        | 
Martien Verbruggen      | 
                        | "Mr Kaplan. Paging Mr Kaplan..."
                        | 


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

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 V11 Issue 1241
***************************************


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