[22779] in Perl-Users-Digest
Perl-Users Digest, Issue: 5000 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun May 18 03:05:53 2003
Date: Sun, 18 May 2003 00:05:22 -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 Sun, 18 May 2003 Volume: 10 Number: 5000
Today's topics:
Re: cmp in perl <noreply@gunnar.cc>
Re: cmp in perl (Xah Lee)
Re: Help!: Hash or Array in this Situation? ctcgag@hotmail.com
Re: Help!: Hash or Array in this Situation? (entropy123)
module for mp3 tag handling <pvdh@xs4all.nl>
Re: module for mp3 tag handling <marks.pryorSHRUB@CHENEYverizon.net>
Need help with regular expression <jacktmpN-O-S-P-A-M@hotpop.com>
Re: novice programmer <nospam4me@yahoohoo.com>
Re: oddity per sort internal <ben.goldberg@hotpop.com>
Re: perl to c <bwalton@rochester.rr.com>
Re: perl to c <a@a.com>
PHP or Perl ? <webmaster@neverseenbefore.com>
Re: PHP or Perl ? <brucie13@alt-html.org>
Re: PHP or Perl ? <e_vdvaart@yahoo.co.uk>
Re: PHP or Perl ? <sepp00@web.de>
Re: PHP or Perl ? <duende_d@hotmail.com>
Re: PHP or Perl ? <tassilo.parseval@rwth-aachen.de>
Sorting @Array from variable in middle of array <sammie@greatergreen.com>
Re: Sorting @Array from variable in middle of array <sammie@greatergreen.com>
Re: Sorting @Array from variable in middle of array (Tad McClellan)
Re: Sorting @Array from variable in middle of array <sammie@greatergreen.com>
Re: Sorting @Array from variable in middle of array <dmcbride@naboo.to.org.no.spam.for.me>
Re: Sorting @Array from variable in middle of array (Jay Tilton)
Re: Sorting @Array from variable in middle of array <sammie@greatergreen.com>
Using grep in populating list of indexes of '1' ? <dmn@box43.pl>
Re: Using grep in populating list of indexes of '1' ? <abigail@abigail.nl>
Re: Using grep in populating list of indexes of '1' ? <ben.goldberg@hotpop.com>
Re: Visual Basic Conversion to Perl.... <bwalton@rochester.rr.com>
Re: Visual Basic Conversion to Perl.... <bwalton@rochester.rr.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 18 May 2003 00:06:40 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: cmp in perl
Message-Id: <ba6bql$oun41$1@ID-184292.news.dfncis.de>
Rodney wrote:
> "Xah Lee" <xah@xahlee.org> wrote in message
> news:7fe97cc4.0305171340.56c63cd1@posting.google.com...
>>
>>what's a good way to check if two files are identical?
>
> Have your tried the CPAN module File::Compare?
Which module is also a standard Perl module.
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 17 May 2003 19:29:50 -0700
From: xah@xahlee.org (Xah Lee)
Subject: Re: cmp in perl
Message-Id: <7fe97cc4.0305171829.12aced6f@posting.google.com>
Q: how to cmp files in perl without system call?
A: File::Compare, comes with Perl at least v.6.
Thanks guys. I always learn something here.
Xah
xah@xahlee.org
http://xahlee.org/PageTwo_dir/more.html
------------------------------
Date: 18 May 2003 01:33:40 GMT
From: ctcgag@hotmail.com
Subject: Re: Help!: Hash or Array in this Situation?
Message-Id: <20030517213340.331$xk@newsreader.com>
email_entropy123@yahoo.com (entropy123) wrote:
> Another question,
>
> How do I get Perl to 'ignore' the fact that the hash has a reference
> to the first node
By:
a) copying (deeply) the data strucure, then deleteing the reference from
the copy once it's been traversed once.
b) creating a hash of links to pretend you don't see, or
c) deleting the reference from the original, assuming you don't mind
being destructive.
Xho
> C1-C2-C3 so when I check C3 it finds C2 which is of course bonded to
> C1....I'm telling it to do nothing if C2 = C1, so my routine always
> finds a ring at C3 even though there is not one....In other words, how
> do I make this routine 'directional'?
>
> Thanks ,
>
> entropy
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: 17 May 2003 21:50:47 -0700
From: email_entropy123@yahoo.com (entropy123)
Subject: Re: Help!: Hash or Array in this Situation?
Message-Id: <90cdce37.0305172050.6c9d87bf@posting.google.com>
>
> By:
>
> a) copying (deeply) the data strucure, then deleteing the reference from
> the copy once it's been traversed once.
> b) creating a hash of links to pretend you don't see, or
> c) deleting the reference from the original, assuming you don't mind
> being destructive.
>
>
I've tried c with catastrophic results. How would I implement a or b?
entropy
------------------------------
Date: 18 May 2003 03:30:32 GMT
From: Paul van der Heu <pvdh@xs4all.nl>
Subject: module for mp3 tag handling
Message-Id: <Xns937F38103268EpvdhNL@194.134.35.159>
Does anyone know of a module or script which handles V2 ID3 MP3 tags
(read/modify/write).
tnX
--
De Nederlandse geluidstechniek site is op komst!
check alvast het forum http://forum.geluidstechniek.nl/
------------------------------
Date: Sun, 18 May 2003 03:57:19 GMT
From: "Mark S Pryor" <marks.pryorSHRUB@CHENEYverizon.net>
Subject: Re: module for mp3 tag handling
Message-Id: <zwDxa.13175$Hy3.1225@nwrddc02.gnilink.net>
Hi Paul,
"Paul van der Heu" <pvdh@xs4all.nl> wrote in message
news:Xns937F38103268EpvdhNL@194.134.35.159...
>
> Does anyone know of a module or script which handles V2 ID3 MP3 tags
> (read/modify/write).
>
> tnX
>
>
> --
> De Nederlandse geluidstechniek site is op komst!
> check alvast het forum http://forum.geluidstechniek.nl/
here's a script that will add a USLT lyrics frame to
an ID3 v2 tag and write the new Mp3 to a Temp file.
Read through it and you can see the module name and
where to get it...
#--------- begin AddUSLT.pl ------------
#!/usr/bin/perl
#
# Author: Mark
# script: AddUSLT.pl
# Description: ID3v2 utility to add a USLT tag frame to an Mp3 file
# keywords: uslt xmp3 frame id3v2 lyric
# Date: 8/4/01 2:05PM
use MP3::ID3v2Tag; #CPAN in MPEG directory/originally by Mp3.com
use FileHandle;
#
# need two file handles, one for the
# target song, inorder to read it,
# and one for the resulting file that contains the lyric frame.
#
my ($buffer, $lyrics, $lyrdir, $songdir) = '';
$lyrdir = "c:\\guitar\\";
$songdir = "d:\\mp3\\Brazil";
$tmpdir = "d:\\mp3\\temp\\";
# ========== user defined ==========
my $stfile = '(joão_gilberto)-15_coisa_mais_linda.mp3';
my $lyfile = $lyrdir . 'coisa_mais_linda.txt';
my $desc = 'joao - coisa mais linda';
# ======== end user defined ==========
chdir($songdir);
# song file to alter by adding the USLT frame
# tmp~corcavado1.mp3 (tom_jobim)-corcovado.mp3
my $gh = new FileHandle "<$stfile";
#
my $tag = MP3::ID3v2Tag->parse($gh) ;
##############################
# build a new tag to write to the temp file
$newtag = MP3::ID3v2Tag->new() ;
$newtag->set_padding_size(0);
foreach $frame ( $tag->frames() ) {
# padding is treated as a frame with null id, OMG
# replace any USLT frame, if found
if ( ($frame->frameid() !~ /^\x00|^US/) )
{
# uncomment below for debug
# print 'id=' . $frame->frameid() .
# ' Text=' . $frame->text() . "\n";
$newtag->add_frame( $frame);
}
}
my $stpos = $tag->{ORIGINAL_SIZE} + 10;
print "tagsize= $tag->{ORIGINAL_SIZE} \n";
#close $gh;
undef $tag ;
#########################################
# read the lyrics in from a file
open ( IN, "<$lyfile" ) || die $! ;
binmode IN;
#
while (read(IN,$buffer,1024))
{$lyrics .= $buffer};
close IN;
$buffer = '';
#################################
#
$newtag->add_frame("USLT", 0,
"por",
$desc,
$lyrics ) ;
# name the temp file in which to insert the new tag
$out = $tmpdir . $stfile;
open ( TEMP, ">$out" ) || die $! ;
binmode TEMP;
# write the new tag to the temp file
print TEMP $newtag->as_string();
binmode $gh;
# skip to the end of the ID3 tag, at the synch header
seek($gh, 0,0);
seek($gh, $stpos ,0) or die $!;
# print the remainder of the songfile to the temp file
while (read($gh,$buffer,1024)) {
print TEMP $buffer;
}
close $gh;
close TEMP;
__END__
hth,
Mark Pryor
------------------------------
Date: Sun, 18 May 2003 00:16:33 GMT
From: "jack" <jacktmpN-O-S-P-A-M@hotpop.com>
Subject: Need help with regular expression
Message-Id: <BhAxa.196076$w7k.52856@news04.bloor.is.net.cable.rogers.com>
I am not sure I could ask a question about regular expression
in this list. I am a Delphi developer and don't know Perl at all.
However, Most discussions re regex are in Perl groups So, I'd just
try my luck here.
I want to be able to decorate URLs in an html file. For example,
I want to change
<a href="http://google.com">
to
<a href="http://myserver.com/google.com">
My plan is to write two regular expressions, one for matching the
whole thing, another one for matching the href="..." part. I'm not
a regex expert and am currently having problems constructing the
two regex, especially with the second one. I search the newsgroups
but didn't find anything useful.
I'm not sure if anyone can help me with building the two reg ex.
If this topic is not good for this list, please reply to my email
address.
Example 1 <a href="http://www.google.ca"> (absolute, with host, with quote)
Example 2 <a href=/text/index.htm> (absolute, no host, no quote)
Example 3 <a href=text/index.htm class="cat"> (relative, no host, no quote,
with other elements)
--
Thanks.
Jack
------------------------------
Date: Sun, 18 May 2003 14:01:10 +1000
From: nospam4me <nospam4me@yahoohoo.com>
Subject: Re: novice programmer
Message-Id: <3EC70586.E98810C9@yahoohoo.com>
Richard Voss wrote:
>
> nospam4me wrote:
> > Anthony wrote:
> >
> >>My objective is to
> >>rename a file named ABCDEFF.TXT to nnddmmyh.hmm
> >>
> >>nn= number 0-9
> >>dd= day
> >>mm= month
> >>y=year
> >>h.h=hour
> >>mm= minutes
> >>
> >
> > #!/bin/perl
> > use strict;
> > use warnings;
> > use Time::Local;
>
> that's not neccessary here.
>
> > ## rename a file named ABCDEFF.TXT to nnddmmyh.hmm
> > ## nn= number 0-9
> > ## dd= day
> > ## mm= month
> > ## y= year
> > ## h.h=hour
> > ## mm= minutes
> >
> > my $num = 1;
> > my $filename = "ABCDEFF.TXT";
> > my $time = time();
>
> some variables are really superfluous...
>
> > my ($min, $hour, $day, $mon, $year) = (localtime($time))[1..5]; $mon++; $year+=1900;
>
> my ($min, $hour, $day, $mon, $year) = (localtime time)[1..5];
> $mon++; $year+=1900;
>
> > $num = $num<10?"0$num":$num;
> > $min = $min<10?"0$min":$min;
> > my $h2 = $hour%10;
> > my $h1 = ($hour - $hour%10)/10;
> > $day = $day<10?"0$day":$day;
> > $mon = $mon<10?"0$mon":$mon;
> > $year = $year%1000;
> >
> > my $stamp = "${num}${day}${mon}${year}${h1}.${h2}${min}";
>
> Do you know sprintf?
>
> my $stamp = sprintf "%02d%02d%02d%1d%1d.%1d%02d",
> $num, $day , $mon , $year % 1000 , $hour%10 , int($hour/10) , $min;
>
> > # print("$stamp\n");
> > rename($filename, $stamp) or die("RENAME: $!");
>
> HTH
>
> --
> sub{use strict;local$@=sub{select($,,$,,$,,pop)};unshift@_,(45)x 24,split q=8==>
> 55.52.56.49.49.55.56.49.49.53;do{print map(chr,@_[0..(@_/2-1)]),"\r";$@->(1/6)=>
> push@_=>shift}for@_,++$|}->(map{$_+=$_%2?-1:1}map ord,split//,'u!`onuids!Qdsm!'.
> 'i`bjds') #my email-address is reversed! <http://fruiture.de>
So....
how much time did you spend on helping the "novice programmer" with code???
I know how to program. I do know of sprintf. I have a valid reason for $time=time().
But thanks for your advice. Hopefully you've taught the "novice programmer"
something though. LOL
------------------------------
Date: Sat, 17 May 2003 21:12:28 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: oddity per sort internal
Message-Id: <3EC6DDFC.5C3EE886@hotpop.com>
Xah Lee wrote:
>
> xah@xahlee.org (Xah Lee) wrote:
> > > # problem: sort a given list @aa, by a given ordered superset @os.
> > > what's a better way to write what i wanted
>
> tiltonj@erols.com (Jay Tilton) wrote:
> > my %order;
> > @order{@os} = 0 .. $#os;
> > if( my @extras = grep !exists $order{$_}, @aa ) {
> > die "not in the superset: @extras";
> > }
> > my @result = sort { $order{$a} <=> $order{$b} } @aa;
> > print Dumper \@result;
>
> i was hoping that a conspicuous check for existence can be avoided,
> assuming the call to the sort predicate should by side-effect take
> care of it.
You can easily eliminate the call to exists() with the following:
my %order;
@order{@os} = 1 .. @os;
if( my @extras = grep !$order{$_}, @aa ) {
die "not in the superset: @extras";
}
my @result = sort { $order{$a} <=> $order{$b} } @aa;
print Dumper \@result;
Or, if you really and truly want it to be done while sorting:
my %order;
@order{@os} = 1 .. @os;
my @result = do {
use warnings 'uninitialized';
local $SIG{__WARN__} = sub {
if( "@_" =~ /^Use of uninitialized value/ ) {
die "Some item us not in the \@os superset.\n";
}
warn @_;
};
sort { $order{$a} <=> $order{$b} } @aa;
};
print Dumper \@result;
Note that putting explicit checks for $a and $b not existing in %order,
while inside the sortsub, will slow you down significantly.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Sun, 18 May 2003 00:40:01 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: perl to c
Message-Id: <3EC6D5C6.2060008@rochester.rr.com>
adams wrote:
> I would like to write a perl program as a prototype for a program that i
> would eventually write in c
> in what way i need to alter when i write the perl prototype
>
> and do perl have any feature has any feature i can use to ease the process
...
Check out:
perldoc perlcompile
particularly the sections on "The Simple C Back End" and "The Optimized
C Back End". Those will write your Perl program in C for you.
--
Bob Walton
------------------------------
Date: Sun, 18 May 2003 14:49:20 +0800
From: "adams" <a@a.com>
Subject: Re: perl to c
Message-Id: <ba7a9t$2mu3$1@justice.itsc.cuhk.edu.hk>
thx a lot
"Bob Walton" <bwalton@rochester.rr.com> ¦b¶l¥ó
news:3EC6D5C6.2060008@rochester.rr.com ¤¤¼¶¼g...
> adams wrote:
>
> > I would like to write a perl program as a prototype for a program that i
> > would eventually write in c
> > in what way i need to alter when i write the perl prototype
> >
> > and do perl have any feature has any feature i can use to ease the
process
> ...
>
> Check out:
>
> perldoc perlcompile
>
> particularly the sections on "The Simple C Back End" and "The Optimized
> C Back End". Those will write your Perl program in C for you.
>
> --
> Bob Walton
>
------------------------------
Date: Sun, 18 May 2003 01:31:23 +0200
From: "Dieter D'Hoker" <webmaster@neverseenbefore.com>
Subject: PHP or Perl ?
Message-Id: <3ec6c663$0$49117$e4fe514c@news.xs4all.nl>
How would you advice someone who wants to start learning a programmign
language for developping websites ?
learning PHP or Perl ?
Or maybe somethign else like Coldfusion , or java servlets , Python ?
and why ?
--
Dieter D'Hoker
http://dieter.dhoker.com/
news:free.nl.dieter.dhoker & news:alt.nl.fan.dieter.dhoker
Tetrinet SeRVer : TSRV.COM
------------------------------
Date: Sun, 18 May 2003 09:35:00 +1000
From: brucie <brucie13@alt-html.org>
Subject: Re: PHP or Perl ?
Message-Id: <ba6gvq$q4os1$1@ID-117621.news.dfncis.de>
In post <3ec6c663$0$49117$e4fe514c@news.xs4all.nl>
Dieter D'Hoker babbled:
> How would you advice someone who wants to start learning a programmign
> language for developping websites ?
> learning PHP or Perl ?
PHP is yummy and easy.
perl is soooo 1990s
<runs for cover/>
--
brucie
------------------------------
Date: Sun, 18 May 2003 01:38:23 +0200
From: "picayunish" <e_vdvaart@yahoo.co.uk>
Subject: Re: PHP or Perl ?
Message-Id: <8Lzxa.7220$KF1.132970@amstwist00>
When Dieter D'Hoker was making a web page, a question appears and
wrote:
> How would you advice someone who wants to start learning a
> programmign language for developping websites ?
> learning PHP or Perl ?
PHP and MySQL.
PHP is easier to learn than Perl.
--
Edwin
http://www.semi-conductors.nl/ Links to Semiconductors sites
(will expire at 01 Sept '03)
http://www.semi-conductor.nl/ (under construction)
http://members.chello.nl/e.vandervaart/ Experimental site
http://host.deluxnetwork.com/~evdvaart/ Test site
------------------------------
Date: Sun, 18 May 2003 01:53:06 +0200
From: Sebastian Hungerecker <sepp00@web.de>
Subject: Re: PHP or Perl ?
Message-Id: <20030518015306.76ef5785.sepp00@web.de>
On Sun, 18 May 2003 01:31:23 +0200
"Dieter D'Hoker" <webmaster@neverseenbefore.com> wrote:
> How would you advice someone who wants to start learning a programmign
> language for developping websites ?
> learning PHP or Perl ?
Perl is not primarily a language for developing websites and for questions
about perl for website developing this is the wrong newsgroup. @topic: php
is IMHO easier for website developing than cgi/perl (or cgi in general for
that matter)
------------------------------
Date: Sun, 18 May 2003 00:34:49 GMT
From: "Wipkip" <duende_d@hotmail.com>
Subject: Re: PHP or Perl ?
Message-Id: <JyAxa.2890$Io.264113@newsread2.prod.itd.earthlink.net>
picayunish wrote:
> When Dieter D'Hoker was making a web page, a question appears and
> wrote:
>> How would you advice someone who wants to start learning a
>> programmign language for developping websites ?
>> learning PHP or Perl ?
>
> PHP and MySQL.
> PHP is easier to learn than Perl.
Also easier to spell.
--
Duende
------------------------------
Date: 18 May 2003 07:01:58 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: PHP or Perl ?
Message-Id: <ba7b56$p3$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Dieter D'Hoker:
> How would you advice someone who wants to start learning a programmign
> language for developping websites ?
> learning PHP or Perl ?
Depends on your ambitions. Considering the fact that PHP is quite good
for website development while Perl is an all-purpose language, learn PHP
if all you ever want to do is going to happen in web context. You'll
only regret this decision when you suddenly realize you want to do more.
But why not have a glance at both of them? Then pick the one that is
more to your liking.
> Or maybe somethign else like Coldfusion , or java servlets , Python ?
>
> and why ?
What I wrote in the first paragraph is actually a little simplistic. PHP
has a particular view on web-sites (HTML mangled with PHP code). In Perl
it is often the other way round (a complete Perl script that generates
the HTML code). However, Perl can also be used in the very same way as
PHP (namely through Embperl which embeds Perl into HTML). As a rule of
thumb: Perl has many ways of integrating programming logic and HTML.
This is 'less' true for PHP.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Sun, 18 May 2003 03:51:23 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Sorting @Array from variable in middle of array
Message-Id: <_qDxa.875732$3D1.499736@sccrnsc01>
I have an array @Info which contains multiple lines, and each line contains
33 different variables separated by \t. I need to sort this array by the
37th variable of each line, while keeping the array intact (I do no want to
change any info, just reorder the array lines - not variables). I have tried
a couple of different ways, both with failure.
Thanks for any help,
Brad
------------------------------
Date: Sun, 18 May 2003 04:11:54 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Re: Sorting @Array from variable in middle of array
Message-Id: <eKDxa.637039$Zo.136767@sccrnsc03>
> ...33 different variables separated by \t....
> ... 37th variable of each line,...
Typo... Should say I have 37 variables, and need to sort by the 33rd
variable.
------------------------------
Date: Sat, 17 May 2003 23:22:09 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Sorting @Array from variable in middle of array
Message-Id: <slrnbce2jh.gvf.tadmc@magna.augustmail.com>
Brad Walton <sammie@greatergreen.com> wrote:
> each line contains
> 33 different variables separated by \t. I need to sort this array by the
> 37th variable of each line,
Huh?
> I have tried
> a couple of different ways, both with failure.
Show us your code, and we can help you fix it.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 18 May 2003 04:58:53 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Re: Sorting @Array from variable in middle of array
Message-Id: <hqExa.12558$rt6.5280@sccrnsc02>
> Huh?
I meant to say sort by 33rd variable in a multi-line array with 37
variables. I deleted the code because it was not working, and it was
garbage. But this is essentially what I did:
foreach my $info (@info) {
@splitinfo = split $info /\t/, $info;
@formatinfo[0..37] = (@formatinfo[0..37], @splitinfo[33,0..32,34..37]);
}
sort(@formatinfo);
Thanks,
Brad
------------------------------
Date: Sun, 18 May 2003 05:25:08 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: Sorting @Array from variable in middle of array
Message-Id: <UOExa.12759$3C2.424284@news3.calgary.shaw.ca>
Brad Walton wrote:
>> Huh?
>
> I meant to say sort by 33rd variable in a multi-line array with 37
> variables. I deleted the code because it was not working, and it was
> garbage. But this is essentially what I did:
It would be nice to have the exact failing code. As I'm sure you were
taught as a kid, "Try, try again." And then post exactly what failed.
> foreach my $info (@info) {
> @splitinfo = split $info /\t/, $info;
> @formatinfo[0..37] = (@formatinfo[0..37], @splitinfo[33,0..32,34..37]);
> }
This is, essentially, looping through @info, setting @formatinfo each time.
When you're done the loop, @formatinfo will be whatever it is set to the
last time. And, of course, $splitinfo[33] is the 34th variable ;-)
> sort(@formatinfo);
Then you sort the last line.
Of course, there are always many ways to do it in perl. One example:
my %fields;
my $linenum = 0;
foreach my $info (@info) {
# no need to split if we just want one value
my $field33 = $info =~ /^(?:.*\t){32}([^\t]+)/;
push @{$fields{$field33}}, $linenum++;
}
# now we have field to line num mapping.
my @order = sort keys %fields;
my @ordered_info;
foreach my $pos (@order)
{ push @ordered_info, @{$fileds{$order}} }
That should work, give or take some work. But it's way too messy for me. I
would do something like this:
my @ordered_info = sort {
my $a33 = $a =~ /^(?:.*\t){32}([^\t]+)/;
my $b33 = $b =~ /^(?:.*\t){32}([^\t]+)/;
$a33 <=> $b33;
# if string, use $a33 cmp $b33;
} @info;
If you are actually going to use each field, or most fields, in most rows,
then maybe it's more efficient to split once, and use the array of arrays
everywhere else.
# split all the lines.
my @splitinfo = map {
[ split /\t/ ]
} @info;
# sort them.
my @ordered_splitinfo = sort {
$a->[32] <=> $b->[32]
} @splitinfo;
As you can see, short, concise, and much more likely to work on the first
try than the first "spelled-out" example. In fact, the comments are
basically superfluous.
------------------------------
Date: Sun, 18 May 2003 05:41:56 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Sorting @Array from variable in middle of array
Message-Id: <3ec718cb.419261592@news.erols.com>
"Brad Walton" <sammie@greatergreen.com> wrote:
: I meant to say sort by 33rd variable in a multi-line array with 37
: variables.
The terminology is confused.
You apparently have an array of strings, and each string has 37
tab-separated values. You want to sort the array on the 33rd of those
values.
: I deleted the code because it was not working, and it was
: garbage. But this is essentially what I did:
:
: foreach my $info (@info) {
: @splitinfo = split $info /\t/, $info;
: @formatinfo[0..37] = (@formatinfo[0..37], @splitinfo[33,0..32,34..37]);
: }
: sort(@formatinfo);
$splitinfo[33] is the 34th value.
The 33rd value would be $splitinfo[32].
See perlfaq4, "How do I sort an array by (anything)?"
Untested code:
my @sorted =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_, (split /\t/, $_)[32] ] }
@info;
------------------------------
Date: Sun, 18 May 2003 06:37:32 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Re: Sorting @Array from variable in middle of array
Message-Id: <MSFxa.13645$rt6.5769@sccrnsc02>
> my @sorted =
> map { $_->[0] }
> sort { $a->[1] cmp $b->[1] }
> map { [$_, (split /\t/, $_)[32] ] }
> @info;
This works great, thanks.
Brad
------------------------------
Date: Sun, 18 May 2003 00:55:41 +0200
From: "Dariusz Nowak" <dmn@box43.pl>
Subject: Using grep in populating list of indexes of '1' ?
Message-Id: <ba6efu$88g$1@atlantis.news.tpi.pl>
Hi All,
I wonder about the best solution to the following problem:
Have a map eg. '11111011011101110001' need to create a
list populated by indexes of '1' from the map.
This is simple operation so I'd like to do make it short.
I came up with the following:
#begin
my $map = '111010101010111';
my @list = ();
my $index = 0;
foreach (split $map) {
push @list, $index if $_;
$index++;
}
#end
But somehow I'm still not satisfied (use of extra var $index ...)
would use of 'grep' in here help ?
Regards
Darek
------------------------------
Date: 17 May 2003 23:09:54 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Using grep in populating list of indexes of '1' ?
Message-Id: <slrnbcdga2.kuj.abigail@alexandra.abigail.nl>
Dariusz Nowak (dmn@box43.pl) wrote on MMMDXLVI September MCMXCIII in
<URL:news:ba6efu$88g$1@atlantis.news.tpi.pl>:
"" Hi All,
""
"" I wonder about the best solution to the following problem:
"" Have a map eg. '11111011011101110001' need to create a
"" list populated by indexes of '1' from the map.
"" This is simple operation so I'd like to do make it short.
""
"" I came up with the following:
"" #begin
""
"" my $map = '111010101010111';
"" my @list = ();
"" my $index = 0;
"" foreach (split $map) {
"" push @list, $index if $_;
"" $index++;
"" }
""
"" #end
""
"" But somehow I'm still not satisfied (use of extra var $index ...)
"" would use of 'grep' in here help ?
Uhm, how would you get the index without the help of some kind of variable?
Grep would be a possibility, but you'd still need an extra variable:
my $map = '111010101010111';
my @list = grep {substr $map => $_, 1} 0 .. -1 + length $map;
Abigail
--
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
.qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
.qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'
------------------------------
Date: Sat, 17 May 2003 21:18:54 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: Using grep in populating list of indexes of '1' ?
Message-Id: <3EC6DF7E.C543FB16@hotpop.com>
Dariusz Nowak wrote:
>
> Hi All,
>
> I wonder about the best solution to the following problem:
> Have a map eg. '11111011011101110001' need to create a
> list populated by indexes of '1' from the map.
my $map = '111010101010111';
my @list;
push @list, $-[0] while $map =~ /1/g;
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Sun, 18 May 2003 01:15:32 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Visual Basic Conversion to Perl....
Message-Id: <3EC6DE16.5070701@rochester.rr.com>
Thunder9_NOSPAM@dsemail.net wrote:
...
>> 7.2 Recursion
>>
>
> sub factorial
> {
> $n = shift;
Here you want instead:
my $n = shift;
Without declaring $n lexical (by using the
my function), recursion will not work. Oh,
one could also use the local function, I
suppose. By default, all Perl variables
are package globals.
> $n *= factorial($n-1) if $n > 1;
> return $n;
> }
...
--
Bob Walton
------------------------------
Date: Sun, 18 May 2003 01:26:14 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Visual Basic Conversion to Perl....
Message-Id: <3EC6E098.9060002@rochester.rr.com>
Veky wrote:
...
> |> 5.4 Assignment to a variable based on a reg expression match within
> |> another string
> |
> | $string =~ /regular expression/$var = $&/e;
s--------------------^
>
> WT* is this:-?? Match, substitution or... :-?
When the typo is corrected, that is a match followed by a substitution,
assuming the match is successful. The /e switch on the end causes the
interpolated substitution string to be evaluated. Hence:
$string='blahblahregular expressionblahblah';
$string=~s/regular expression/$var=$&/e;
will replace the matched value ($&) with itself in $string, resulting in
no net alteration to $string, with the side effect of setting $var to
the value 'regular expression'.
--
Bob Walton
------------------------------
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.
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 5000
***************************************