Re: Syswrite Function in Perl

2007-01-03 Thread D. Bolliger
D. Bolliger am Dienstag, 2. Januar 2007 12:55:
 Dharshana Eswaran am Dienstag, 2. Januar 2007 08:02:
[snip]
  $seq = STDIN;
  chop($seq);
  @seq = split(/ +/, $seq);
  $seq_len = @seq;
[snip]
  for($i=0; $i$seq_len; $i++) {
  $read1[$i] = $table{$structure{$seq[$j]}};
  syswrite (OUT, $new, $read1[$i]);
  print OUT (\n);
  $j++;
  }
 
  In the above code, i m trying to read the input in bytes and display them
  in another output file. The reading is done in different sizes (4 or 8 or
  32bytes), as per the sequence specifed by the User. The input file with
  filehandle IN contains data as shown below:
 
  F1 2F 8A 02 05 09 00 00 00 04 2B 48 00 00 00 68
[snip]

 Then you can inspect the values in $seq[$j] (with a warn statement), and
 you'll see what's going wrong :-)
[snip]

Apologies to all :-( - and a happy new year :-)

John's answer pointed me to the fact that I did an error while trimming down 
Dharshana Eswaran code; I put the IN file data into @seq, not the STDIN data.
How stupid of me.

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




create symlinks to thousands of files using perl

2007-01-03 Thread Michael Alipio
I have a folder named myfolder
Inside myfolder, I have several files named ft1, ft2, ft3, ft4.

I need to create symbolic links to all of those files into my current working 
directory.

I tried creating a shell script:

#!/bin/sh
for i in /myfolder/ft* ; do ln -s ${i} ./; done

But what it did was to create a symbolic link ft* pointing to /myfolder/ft*...

#ls

 0 lrwxrwxrwx  1 root root   9 2007-01-03 15:55 ft* - /myfolder/ft*

When all I wanted was to have:

#ls
  0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft1 - myfolder/ft1
   0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft2 - myfolder/ft2
   0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft3 - myfolder/ft3


Do you know a quick perl script to accomplish this thing.
I shouldn't be needing this script but xargs in this OS(debian) doesn't have 
the -J flags like in BSD..

so far all I got is this: :-)

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




Thank you very much for any help or pointers..







__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: create symlinks to thousands of files using perl

2007-01-03 Thread Robin Sheat
On Wednesday 03 January 2007 21:02, Michael Alipio wrote:
 #!/bin/sh
 for i in /myfolder/ft* ; do ln -s ${i} ./; done

 But what it did was to create a symbolic link ft* pointing to
 /myfolder/ft*...
Not perl, but:
for i in /myfolder/ft* ; do ln -s ${i}; done
should do it, if I read you right. (i.e. without the ./, it should create a 
link in the current dir with the same name as the file you pointed to)

-- 
Robin [EMAIL PROTECTED] JabberID: [EMAIL PROTECTED]

Hostes alienigeni me abduxerunt. Qui annus est?

PGP Key 0xA99CEB6D = 5957 6D23 8B16 EFAB FEF8  7175 14D3 6485 A99C EB6D


pgpE0oC474MPP.pgp
Description: PGP signature


Re: create symlinks to thousands of files using perl

2007-01-03 Thread John W. Krahn
Michael Alipio wrote:
 I have a folder named myfolder
 Inside myfolder, I have several files named ft1, ft2, ft3, ft4.
 
 I need to create symbolic links to all of those files into my current working 
 directory.
 
 I tried creating a shell script:
 
 #!/bin/sh
 for i in /myfolder/ft* ; do ln -s ${i} ./; done
 
 But what it did was to create a symbolic link ft* pointing to 
 /myfolder/ft*...
 
 #ls
 
  0 lrwxrwxrwx  1 root root   9 2007-01-03 15:55 ft* - /myfolder/ft*
 
 When all I wanted was to have:
 
 #ls
   0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft1 - myfolder/ft1
0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft2 - myfolder/ft2
0 lrwxrwxrwx  1 root root   8 2007-01-03 15:58 ft3 - myfolder/ft3
 
 
 Do you know a quick perl script to accomplish this thing.

perl -MFile::Basename -e'symlink $_, basename $_ for /myfolder/ft*'



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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




detecting a UTF-8 string

2007-01-03 Thread Octavian Rasnita

Hi,

I want to check if a certain string is UTF-8 or not.

I have tried using is_utf8 from the Encode module, and utf8::is_utf8() but 
the string is detected wrong.


For example, if I have a UTF-8 encoded file and an ANSI encoded file, if I 
open them both without :utf8, is_utf8 shows that they are not UTF-8 
strings, and if I open the files using :utf8, then is_utf8 shows that 
they both are UTF-8 strings.


I want to detect which file is UTF-8 encoded and which is not.

Actually, I want to get a text from a database and check if it is UTF-8 
encoded, and if it is not, to encode it as UTF-8, because I don't want to 
encode a text as UTF-8 twice.


Can you tell me how can this be done?

Thank you.

Octavian


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: detecting a UTF-8 string

2007-01-03 Thread Jay Savage

On 1/3/07, Octavian Rasnita [EMAIL PROTECTED] wrote:

Hi,

I want to check if a certain string is UTF-8 or not.

I have tried using is_utf8 from the Encode module, and utf8::is_utf8() but
the string is detected wrong.

For example, if I have a UTF-8 encoded file and an ANSI encoded file, if I
open them both without :utf8, is_utf8 shows that they are not UTF-8
strings, and if I open the files using :utf8, then is_utf8 shows that
they both are UTF-8 strings.

I want to detect which file is UTF-8 encoded and which is not.

Actually, I want to get a text from a database and check if it is UTF-8
encoded, and if it is not, to encode it as UTF-8, because I don't want to
encode a text as UTF-8 twice.

Can you tell me how can this be done?

Thank you.

Octavian


Try to unpack the data--or a chunk of data you feel is large enough to
be representative--with the pattern U0U*. If the unpack succeeds with
no warnings, you have valid utf8. You could try the same thing with
Encode's 'decode_utf8' routine. See perluniintro for details. in both
cases, though, you need to make sure that you've grabbed well-formed
utf8 from the source file in the first place. If the data cuts off in
the middle of a multi-byte character, you'll get an error.

This may sound like a kludge, and it is. Perl has no way of knowing
whether your data is utf8 data; it's just a stream of bytes, or maybe
just bits. You have to tell perl whether to interpret those bytes as a
particular character encoding, or just let it guess. Why? Because file
formats aren't mutually exclusive. There is nothing to prevent unicode
or ascii characters from appearing in other file types. You could have
a JPEG image composed entirely of bytes that correspond to unicode
characters. Encodings like uuencode are designed to ascii armor binary
files. Only the programmer knows what the input is supposed to be, and
what sort of conversion should take place.

HTH,

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!


Re: XML in, XML out

2007-01-03 Thread Beginner
On 24 Dec 2006 at 12:23, Rob Dixon wrote:

 Randal L. Schwartz wrote:
  Rob == Rob Dixon [EMAIL PROTECTED] writes:
  
  Rob my @bad = $doc-findnodes(q{//address[starts-with(code, BJPU)]});
  
  Actualy, doesn't that require code immediately below addess?
 
 Yes
 
  Don't you want .//code there ?
 
 Almost certainly not. In the example XML code appeared only as a child 
 element
 of address. In the general case it is unlikely to reappear at a lower level
 and // would force the XSLT engine to search further than was necessary.
 
 By the way the dot is also unnecessary as it is implicit before // anywhere
 except at the beginning of a path expression. (A path starting with // covers
 all descendants of the document root.)


Sorry to tread on an old thread. 

I was hoping to try XML::LibXML but am having trouble with the 
install. Not sure if this is the right place to ask but

My installed version of XML2 is 2.6.20, th readme for XML::LibXML 
says a mimimum of version 2.6.16 is required. But it complains when 
creating the makefile that 

running xml2-config...failed
The installed version of libxml2 not compatible with XML::LibXML.

It's a long-shot but does anyone know what the problem is here? There 
isn't anything relevant on via searcb engine or at 
http://xmlsoft.org/FAQ.html

TIA.
Dp.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: detecting a UTF-8 string

2007-01-03 Thread Igor Sutton

2007/1/3, Octavian Rasnita [EMAIL PROTECTED]:


Hi,

I want to check if a certain string is UTF-8 or not.



Maybe you want Encode::Guess[1].

[1] http://search.cpan.org/~dankogai/Encode-2.18/lib/Encode/Guess.pm

--
Igor Sutton Lopes [EMAIL PROTECTED]


Hello and a question

2007-01-03 Thread Tom Messmer

Hello everyone,
Just joined this list and I have a doozie I've been working on for a  
bit here to no avail. The entire problem is this; I have a list of  
files, say that they are named flynn.foo, flynn_something.foo,  
flaherty.foo flaherty_something.foo and so forth. Each of these  
files must live(be moved to)  an individual directory named for the  
author(flynn, flaherty, etc) and then be symlinked to an entirely  
different directory in another part of the filesystem identical to  
the first one(flynn, flaherty...) If I was doing this on the command  
line I'd do


cp /usr/blah/flaherty.foo /usr/blah/blahagain/flaherty/ 
 ln -s /usr/blah/blahagain/flaherty/flaherty.foo /usr/blah/ 
blahoncemore/flaherty.foo


 So far I've gotten to the point where I can strip out the  names  
from the files and create the two sets of directories, but I'm  
stumped on how to them copy each of these files into the correct  
directory and then symlink from the storage directory to the other  
name directory. Now I know how to copy files and symlink files with  
perl, but the logic involved in doing this to 30 files is beyond me  
at the moment. Anyone have a clue?

Thanks,
Tom

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: detecting a UTF-8 string

2007-01-03 Thread Jay Savage

On 1/3/07, Octavian Rasnita [EMAIL PROTECTED] wrote:

From: Jay Savage [EMAIL PROTECTED]
 Try to unpack the data--or a chunk of data you feel is large enough to
 be representative--with the pattern U0U*. If the unpack succeeds with
 no warnings, you have valid utf8. You could try the same thing with
 Encode's 'decode_utf8' routine. See perluniintro for details. in both
 cases, though, you need to make sure that you've grabbed well-formed
 utf8 from the source file in the first place. If the data cuts off in
 the middle of a multi-byte character, you'll get an error.

I have tried verifying the entire string, using the following:

my $result = unpack(U0U*, $content);
print $result;

Well, it gave no errors even though the string was UTF-8 or not, but an
interesting thing is that the result printed was always 65279 if the string
was UTF-8 and 112 or 116 if the string was not UTF-8.

Do you know what represent these numbers? I am curious why sometimes it
prints 112 and sometimes 116 when using some ansi strings.
I hope the result is consistent and I can base on it to use the code in my
program for checking if a string is UTF-8.

Thank you.

Octavian


Unpack returns a list, so $result gets the value of the first itme of
the list. Offhand, I'd say the first character of your utf-8 string
was the three-byte character 0xfeff (zero-width no-break space).
That also happens to be the two-byte byte order mark (BOM) for the
beginning of a big-endian utf-16 stream (if you see 65534 [0xfffe]
it's little-endian). If all of your data behaves so nicely, you can
just look for the BOM. Note, though, that according to the standard,
this data is really big-endian utf-16, not utf-8, although it may only
use utf-8 code points.

As for 112 and 116, I'd say all you ascii data began with p ot t
(or something else that perl interpreted as those code points). Keep
in mind that most ascii data is perfectly well-formed utf-8. If what
you want to do is separate ascii from utf-8, test for asciiness and
treat the rest as utf-8.

HTH,

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!


[query] Using a scalar variable with match operator

2007-01-03 Thread Deepak Barua

Hi,
   I want to use a scalar variable with OR | operator embedded in
the variable in a statement like
$start_pattern = \/\*   \|   \/\/;

if(m/$start_pattern/ || $continue == 1) {
  $chosen_pattern = $;
Please inform me on how it is supposed to work..?

Regards
Deepak

--
Code Code Code Away

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Hello and a question

2007-01-03 Thread Beginner
On 3 Jan 2007 at 8:02, Tom Messmer wrote:

 Hello everyone,

Hello and welcome,

 Just joined this list and I have a doozie I've been working on for a  
 bit here to no avail. The entire problem is this; I have a list of  
 files, say that they are named flynn.foo, flynn_something.foo,  
 flaherty.foo flaherty_something.foo and so forth. Each of these  
 files must live(be moved to)  an individual directory named for the  
 author(flynn, flaherty, etc) and then be symlinked to an entirely  
 different directory in another part of the filesystem identical to  
 the first one(flynn, flaherty...) If I was doing this on the command  
 line I'd do
 
 cp /usr/blah/flaherty.foo /usr/blah/blahagain/flaherty/ 
   ln -s /usr/blah/blahagain/flaherty/flaherty.foo /usr/blah/ 
 blahoncemore/flaherty.foo
 
   So far I've gotten to the point where I can strip out the  names  
 from the files and create the two sets of directories, but I'm  
 stumped on how to them copy each of these files into the correct  
 directory and then symlink from the storage directory to the other  
 name directory. Now I know how to copy files and symlink files with  
 perl, but the logic involved in doing this to 30 files is beyond me  
 at the moment. Anyone have a clue?


This is a hard one to test. I haven't created the file structure you 
described so YOU MUST TEST IT. I think you'll get better suggestions 
from the list in time but I thought I'd have a shot anyway (I await 
the rebukes).

I'll leave the symlink for you (perldoc -f symlink). Try this... it 
might get you started.
Good luck,
Dp.


#!/usr/bin/perl # or where ever your perl is installed.

use strict;
use warnings;
use File::Copy;
use File::Basename;

my $source_dir = '/usr/blah';
my $distin_dir = '/usr/blany/blanagain/';

# Open the dir for reading or die.
opendir(DIR,$source_dir) or die Can't open $source_dir: $!\n;

# Collect the author directories (and names).
my @auth_dir = grep { -f $source_dir/$_ } readdir(DIR);

foreach my $i (@auth_dir) {

# Get the filename of the file from /usr/blab/flynn.foo
my $pathname = $source_dir/$i;

# Grab the author from flynn.foo, splitting on the period.

# method 1 via regex
my ($author,$file) = ($i =~ /(\w+)\.(\w+)/);
# method 2 via split
my @f = split(/\./,$i);


# Going with method 2 for the moment.
my $dist = $distin_dir.$f[0];
print Copying $pathname- $dist.$f[1]\n;

#Keep commented until your sure.
#   copy($i,$dist) or die Can't copy $i - $dist: $!\n;

}

print Done...\n;






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: [query] Using a scalar variable with match operator

2007-01-03 Thread John W. Krahn
Deepak Barua wrote:
 Hi,

Hello,

I want to use a scalar variable with OR | operator embedded in
 the variable in a statement like
 $start_pattern = \/\*   \|   \/\/;
 
 if(m/$start_pattern/ || $continue == 1) {
   $chosen_pattern = $;
 Please inform me on how it is supposed to work..?

my $start_pattern = qr!/\*|//!;

if ( /($start_pattern)/ || $continue == 1 ) {
  $chosen_pattern = $1;



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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Syswrite Function in Perl

2007-01-03 Thread John W. Krahn
Dharshana Eswaran wrote:

 The output what i m getting is
 
 The input file contains message as follows:
 
 D0 1A 81 03 01 21 80 82 02 81 02 8D 0F 04 54 6F 6F 6C 6B 69 74 20 54 65 73
 
 Sample output:
 
 ENTER THE SEQUENCE between[1-3]:
 2 1 1 3
 
 The output file contains:
 
 D01A8103012180820281028D0F04546F
 
 I am unable to give spaces between each entry in the output.I need the
 output in steps...
 
 Something like:
 D01A8103
 0121
 8082
 0281028D0F04546F
 
 I tried printing a new line after the syswrite instruction, but i am unable
 to achieve that :-(

use warnings;
use strict;

open my $IN,  '', 'in.txt' or die Cannot open 'in.txt' $!\n;
open my $OUT. '', 'output.txt' or die Cannot open 'output.txt' $!\n;

my %structure = qw( 1 A 2 B 3 C );
my %table = qw( A 4 B 8 C 32 );

( my $data = $IN ) =~ s/\s+//g;

print ENTER THE SEQUENCE between[1-3]: ;
( my $regex = STDIN ) =~ 
s/[^123]*([123])[^123]*/(.{$table{$structure{$1}}})/g;

print $OUT map $_\n, $data =~ /$regex/;

__END__




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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: [query] Using a scalar variable with match operator

2007-01-03 Thread Deepak Barua

Hi John,
   I don't  understand the script seems very cryptic, could
you explain
my $start_pattern = qr!/\*|//!;

Regards
Deepak

On 1/3/07, John W. Krahn [EMAIL PROTECTED] wrote:

Deepak Barua wrote:
 Hi,

Hello,

I want to use a scalar variable with OR | operator embedded in
 the variable in a statement like
 $start_pattern = \/\*   \|   \/\/;

 if(m/$start_pattern/ || $continue == 1) {
   $chosen_pattern = $;
 Please inform me on how it is supposed to work..?

my $start_pattern = qr!/\*|//!;

if ( /($start_pattern)/ || $continue == 1 ) {
  $chosen_pattern = $1;



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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/






--
Code Code Code Away

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: [query] Using a scalar variable with match operator

2007-01-03 Thread John W. Krahn
Deepak Barua wrote:
 
 On 1/3/07, John W. Krahn [EMAIL PROTECTED] wrote:
 Deepak Barua wrote:

 I want to use a scalar variable with OR | operator embedded in
  the variable in a statement like
  $start_pattern = \/\*   \|   \/\/;
 
  if(m/$start_pattern/ || $continue == 1) {
$chosen_pattern = $;
  Please inform me on how it is supposed to work..?

 my $start_pattern = qr!/\*|//!;

 if ( /($start_pattern)/ || $continue == 1 ) {
   $chosen_pattern = $1;
 
I don't  understand the script seems very cryptic, could
 you explain
 my $start_pattern = qr!/\*|//!;

I assumed that you wanted to match EITHER '/*' OR '//'.  The qr// operator
creates a regular expression:

perldoc perlop



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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: detecting a UTF-8 string

2007-01-03 Thread Octavian Rasnita
Ok, thank you all.
Finally I've done it like this:

use Encode;
use Encode::Guess;

my $decoder = guess_encoding($content);
print UTF-8 if ref($decoder) eq 'Encode::utf8';

Octavian

  - Original Message - 
  From: Igor Sutton 
  To: Octavian Rasnita 
  Cc: beginners@perl.org 
  Sent: Wednesday, January 03, 2007 5:46 PM
  Subject: Re: detecting a UTF-8 string


  2007/1/3, Octavian Rasnita [EMAIL PROTECTED]:
Hi,

I want to check if a certain string is UTF-8 or not.

  Maybe you want Encode::Guess[1].

  [1] http://search.cpan.org/~dankogai/Encode-2.18/lib/Encode/Guess.pm 


  -- 
  Igor Sutton Lopes  [EMAIL PROTECTED] 

Re: XML in, XML out

2007-01-03 Thread Randal L. Schwartz
 Beginner == Beginner  [EMAIL PROTECTED] writes:

Beginner running xml2-config...failed
Beginner The installed version of libxml2 not compatible with XML::LibXML.

It's not just a minimum.  There are some versions of libxml2 that are
sufficiently broken that the the author of XML::LibXML is protecting you from
them.  You probably have one of those.  See the source of the Makefile.PL
for details.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Hello and a question

2007-01-03 Thread Tom Messmer
Thanks for the reply. I tried it out and it merely copies the file  
from one directory to another, rather than to a directory named for  
the author: in this case from

my $source_dir = '/usr/blah'; to
my $distin_dir = '/usr/blany/blanagain/';

with output like this:

Copying /usr/blah/htdocs/media/events/blah06/epic_struggle/mp3/ 
cuchailain.mp3- /home/messmer/test/fake_mp3dir/cuchailain.mp3


instead of
Copying /usr/blah/htdocs/media/events/blah06/epic_struggle/mp3/ 
cuchailain.mp3- /home/messmer/test/fake_mp3dir/cuchailain/ 
cuchailain.mp3


In other words, there will be a directory full of mp3s like:
/usr/whatever/cuchailain.mp3
/usr/whatever/ulysses.mp3
/usr/whatever/beowulf.mp3
/usr/whatever/Sigurðr,mp3 (Ok, i'd need unicode for that one, never  
mind...)


and I want to copy them into already existing directories like
/home/messmer/mp3s/cuchailain/
/home/messmer/mp3s/ulysses/
/home/messmer/mp3s/beowulf/


Maybe I missed some nuance of your script that would have done  
this...? I'm just starting out with perl really so please excuse my  
ignorance.

Thanks!
Tom




On Jan 3, 2007, at 9:25 AM, Beginner wrote:


On 3 Jan 2007 at 8:02, Tom Messmer wrote:


Hello everyone,


Hello and welcome,


Just joined this list and I have a doozie I've been working on for a
bit here to no avail. The entire problem is this; I have a list of
files, say that they are named flynn.foo, flynn_something.foo,
flaherty.foo flaherty_something.foo and so forth. Each of these
files must live(be moved to)  an individual directory named for the
author(flynn, flaherty, etc) and then be symlinked to an entirely
different directory in another part of the filesystem identical to
the first one(flynn, flaherty...) If I was doing this on the command
line I'd do

cp /usr/blah/flaherty.foo /usr/blah/blahagain/flaherty/ 
  ln -s /usr/blah/blahagain/flaherty/flaherty.foo /usr/blah/
blahoncemore/flaherty.foo

  So far I've gotten to the point where I can strip out the  names
from the files and create the two sets of directories, but I'm
stumped on how to them copy each of these files into the correct
directory and then symlink from the storage directory to the other
name directory. Now I know how to copy files and symlink files with
perl, but the logic involved in doing this to 30 files is beyond me
at the moment. Anyone have a clue?



This is a hard one to test. I haven't created the file structure you
described so YOU MUST TEST IT. I think you'll get better suggestions
from the list in time but I thought I'd have a shot anyway (I await
the rebukes).

I'll leave the symlink for you (perldoc -f symlink). Try this... it
might get you started.
Good luck,
Dp.


#!/usr/bin/perl # or where ever your perl is installed.

use strict;
use warnings;
use File::Copy;
use File::Basename;

my $source_dir = '/usr/blah';
my $distin_dir = '/usr/blany/blanagain/';

# Open the dir for reading or die.
opendir(DIR,$source_dir) or die Can't open $source_dir: $!\n;

# Collect the author directories (and names).
my @auth_dir = grep { -f $source_dir/$_ } readdir(DIR);

foreach my $i (@auth_dir) {

# Get the filename of the file from /usr/blab/flynn.foo
my $pathname = $source_dir/$i;

# Grab the author from flynn.foo, splitting on the period.

# method 1 via regex
my ($author,$file) = ($i =~ /(\w+)\.(\w+)/);
# method 2 via split
my @f = split(/\./,$i);


# Going with method 2 for the moment.
my $dist = $distin_dir.$f[0];
print Copying $pathname- $dist.$f[1]\n;

#Keep commented until your sure.
#   copy($i,$dist) or die Can't copy $i - $dist: $!\n;

}

print Done...\n;






--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Formatting/presenting regex

2007-01-03 Thread Owen

I have this regex to look at an Apache log.

m/^(\S+) \S+ \S+ \[(\d{2})\/(\S+)\/(\d{4}):.+\] (\w+) (\S+)
([^]+) (\d{3}) (\d+|-) .+$/;

Would like to set it out in a bit more readable form a la Perl Cook Book and 
others

eg

m/
^(\S+)  # Comment
 \S+# Comment
 \S+# Comment

etc,

but they don't work and I suspect it is something to do with the spaces 
between the elements. I have even tried specifying spaces with \s


Can someone give me a pointer as to how to go about this. 




TIA


Owen  

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Formatting/presenting regex

2007-01-03 Thread Wiggins d'Anconia

Owen wrote:

I have this regex to look at an Apache log.



There are modules to help with that task on CPAN.


m/^(\S+) \S+ \S+ \[(\d{2})\/(\S+)\/(\d{4}):.+\] (\w+) (\S+)
([^]+) (\d{3}) (\d+|-) .+$/;

Would like to set it out in a bit more readable form a la Perl Cook Book and 
others

eg

m/
^(\S+)  # Comment
 \S+# Comment
 \S+# Comment

etc,

but they don't work and I suspect it is something to do with the spaces 
between the elements. I have even tried specifying spaces with \s



The key is to use the /x modifier on the end of the regex to allow this 
type of formatting. You then, according to perldoc perlretut, can either 
backslash real spaces or put them in a character class. See,


perldoc perlretut

for more.



Can someone give me a pointer as to how to go about this. 



HTH,

http://danconia.org





TIA


Owen  



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Formatting/presenting regex

2007-01-03 Thread John W. Krahn
Owen wrote:
 I have this regex to look at an Apache log.
 
 m/^(\S+) \S+ \S+ \[(\d{2})\/(\S+)\/(\d{4}):.+\] (\w+) (\S+)
 ([^]+) (\d{3}) (\d+|-) .+$/;
 
 Would like to set it out in a bit more readable form a la Perl Cook Book and 
 others
 
 eg
 
 m/
 ^(\S+)# Comment
  \S+  # Comment
  \S+  # Comment
 
 etc,
 
 but they don't work and I suspect it is something to do with the spaces 
 between
 the elements. I have even tried specifying spaces with \s
 
 Can someone give me a pointer as to how to go about this. 

$ perl -le'
print
$1
if
one two three four
=~
/
^ # start of line
(
\S+   # capture one or more non-whitespace
)
\ # a single space character
\S+   # match one or more non-whitespace
[ ]   # a single space character
\S+   # match one or more non-whitespace
\ # a single space character
/x
'
one




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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Regex question

2007-01-03 Thread M. Lewis


I'm trying to parse the domain name out of some URLs. In the example 
data, my regex works fine on the first two URLs, but clips off the first 
two characters of the domain on the third example. My regex probably 
could be much better.


#!/usr/bin/perl

use strict;
use warnings;

my $regex = qr'http://\w+?\.?\w+?\.?(\w+\.com)';

while(DATA){
  if (/$regex/o){print $1 \t $_}
}

__DATA__
http://www.asldkjlkwerj.com/
http://w71r2xk22q1affwp1ewpjeee.alaskjhhawe.com/?
http://qwlkjekwl.com/?IJESRKUFZedFRCVFJYQV4cUFtY

Thanks for any pointers,
Mike

--

 It is easier to change the specification to fit the program than vice 
versa.

  02:50:01 up 20 days, 23:41,  0 users,  load average: 0.21, 0.22, 0.24

 Linux Registered User #241685  http://counter.li.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/