Re: help with making an sftp connection

2023-04-17 Thread Jeremy.l SR
Hi

Sorry for my misunderstanding.
password_prompt may not be your case.

Jeremy.l SR  於 2023年4月18日 週二 上午10:00寫道:

> Hi
>
> What is your password_prompt on your remote sftp server?
> ex:
> root@192.168.56.100's password:
>
> according the document, your script will hang at the login phase if the
> wrong prompt is used.
>
>
>
>
>
> Brent Wood via beginners  於 2023年4月18日 週二 上午7:54寫道:
>
>> Thanks for that suggestion, it does provide more info, and using the
>> script more as you formatted it:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *say "start";$sftp = Net::SFTP::Foreign->new($host,user => $user,
>>  password => $pass,more => [qw( -v  -o
>> PreferredAuthentications=password  -o
>> PasswordAuthentication=yes  -o BatchMode=yes )]*
>> *);*
>> *say "done";*
>>
>>
>> I get this output:
>>
>> *start*
>> *...*
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *debug1: Local version string SSH-2.0-OpenSSH_8.9p1
>> Ubuntu-3ubuntu0.1debug1: Remote protocol version 2.0, remote software
>> version OpenSSH_8.9p1 Ubuntu-3ubuntu0.1debug1: compat_banner: match:
>> OpenSSH_8.9p1 Ubuntu-3ubuntu0.1 pat OpenSSH* compat 0x0400debug1:
>> Authenticating to 127.0.0.1:22 <http://127.0.0.1:22> as 'baw'debug1:
>> SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex:
>> algorithm: curve25519-sha256debug1: kex: host key algorithm:
>> ssh-ed25519debug1: kex: server->client cipher:
>> chacha20-poly1...@openssh.com  MAC:
>>  compression: nonedebug1: kex: client->server cipher:
>> chacha20-poly1...@openssh.com  MAC:
>>  compression: nonedebug1: expecting
>> SSH2_MSG_KEX_ECDH_REPLYdebug1: SSH2_MSG_KEX_ECDH_REPLY receiveddebug1:
>> Server host key: ssh-ed25519
>> SHA256:9xuiSKoLyBrR6XdL2Ktv9Osdoq257n++/zUO0izC+7sHost key verification
>> failed.done*
>>
>> It seems to be trying to use key based authentication irrespective of the
>> -o parameters passed in. I looked for an ssh based fix & found this:
>>
>> https://superuser.com/questions/1376201/how-do-i-force-ssh-to-use-password-instead-of-key
>>
>> Which suggested the issue was possibly at the server end, but my
>> /etc/ssh/ssh_config file is set up to allow password authentication, and I
>> can ssh in directly with password only, so it does not seem that this is my
>> problem.
>>
>> As far as I can tell, it is just scripted sftp via Perl that seems to
>> fail. Even if I omit the user & password entries in the connect string,
>> Perl still gets my user as the default & never prompts for a password, just
>> gives the same message. It may be worth trying to do this with Net::SFTP
>> instead of Net::SFTP::Foreign, any advice?
>>
>>
>> Thanks again,
>>
>> Brent
>>
>>
>>
>>
>>
>> On Monday, April 17, 2023 at 10:58:49 PM GMT+12, Dermot <
>> paik...@gmail.com> wrote:
>>
>>
>> Hi,
>>
>> Can you edit the constructor to include `more => [ -v ]` so we can get
>> more feedback from the SSH connection and send the output from that.
>>
>> $sftp = Net::SFTP::Foreign->new($host,
>> user => $user,
>> password => $pass,
>> more => [
>> qw(
>> -v
>> -o PreferredAuthentications=password
>> -o PasswordAuthentication=yes
>> -o BatchMode=yes
>> )
>>]
>> )  or $sftp->error;
>>
>> The clue to what is going wrong will be in the output from ssh.
>> Thanks,
>> Dermot
>>
>> On Mon, 17 Apr 2023 at 10:13, Brent Wood via beginners <
>> beginners@perl.org> wrote:
>>
>> More detail, thanks for your time...
>>
>>
>> This is to test the Perl script on a local (Linux) system, copying a file
>> from /tmp to another directory.
>>
>> I can use command line sftp to copy a file fine with the user/password
>> connection, so there is not a problem with sftp, user/password etc on the
>> system.
>>
>> This Perl script executes without error, but hangs on $sftp = Net::...
>> (prints start, never prints done)
>>
>> If I take out the password assignment, I'm prompted for a password and it
>> then works fine. With it there, it hangs.
>>
>> Can anyone help me get this working with a password passed as a parameter?
>>
>> use Net::SFTP::Foreign;
>> use IO::Pty;
>> use feature say;
>>
>> $host = "127.0.0.1";
>> $user = "baw";
>> $pass = "..";
>>
>> say "start";
>>
>> $sftp = Net::SFTP::Foreign->new($host,
>> user => $user,
>> password => $pass,
>> more => [qw(-o PreferredAuthentications=password
>> -o PasswordAuthentication=yes -o BatchMode=yes)]
>> );
>>
>> say "done";
>>
>>


Re: help with making an sftp connection

2023-04-17 Thread Jeremy.l SR
Hi

What is your password_prompt on your remote sftp server?
ex:
root@192.168.56.100's password:

according the document, your script will hang at the login phase if the
wrong prompt is used.





Brent Wood via beginners  於 2023年4月18日 週二 上午7:54寫道:

> Thanks for that suggestion, it does provide more info, and using the
> script more as you formatted it:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *say "start";$sftp = Net::SFTP::Foreign->new($host,user => $user,
>  password => $pass,more => [qw( -v  -o
> PreferredAuthentications=password  -o
> PasswordAuthentication=yes  -o BatchMode=yes )]*
> *);*
> *say "done";*
>
>
> I get this output:
>
> *start*
> *...*
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *debug1: Local version string SSH-2.0-OpenSSH_8.9p1
> Ubuntu-3ubuntu0.1debug1: Remote protocol version 2.0, remote software
> version OpenSSH_8.9p1 Ubuntu-3ubuntu0.1debug1: compat_banner: match:
> OpenSSH_8.9p1 Ubuntu-3ubuntu0.1 pat OpenSSH* compat 0x0400debug1:
> Authenticating to 127.0.0.1:22 <http://127.0.0.1:22> as 'baw'debug1:
> SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex:
> algorithm: curve25519-sha256debug1: kex: host key algorithm:
> ssh-ed25519debug1: kex: server->client cipher:
> chacha20-poly1...@openssh.com  MAC:
>  compression: nonedebug1: kex: client->server cipher:
> chacha20-poly1...@openssh.com  MAC:
>  compression: nonedebug1: expecting
> SSH2_MSG_KEX_ECDH_REPLYdebug1: SSH2_MSG_KEX_ECDH_REPLY receiveddebug1:
> Server host key: ssh-ed25519
> SHA256:9xuiSKoLyBrR6XdL2Ktv9Osdoq257n++/zUO0izC+7sHost key verification
> failed.done*
>
> It seems to be trying to use key based authentication irrespective of the
> -o parameters passed in. I looked for an ssh based fix & found this:
>
> https://superuser.com/questions/1376201/how-do-i-force-ssh-to-use-password-instead-of-key
>
> Which suggested the issue was possibly at the server end, but my
> /etc/ssh/ssh_config file is set up to allow password authentication, and I
> can ssh in directly with password only, so it does not seem that this is my
> problem.
>
> As far as I can tell, it is just scripted sftp via Perl that seems to
> fail. Even if I omit the user & password entries in the connect string,
> Perl still gets my user as the default & never prompts for a password, just
> gives the same message. It may be worth trying to do this with Net::SFTP
> instead of Net::SFTP::Foreign, any advice?
>
>
> Thanks again,
>
> Brent
>
>
>
>
>
> On Monday, April 17, 2023 at 10:58:49 PM GMT+12, Dermot 
> wrote:
>
>
> Hi,
>
> Can you edit the constructor to include `more => [ -v ]` so we can get
> more feedback from the SSH connection and send the output from that.
>
> $sftp = Net::SFTP::Foreign->new($host,
> user => $user,
> password => $pass,
> more => [
> qw(
> -v
> -o PreferredAuthentications=password
> -o PasswordAuthentication=yes
> -o BatchMode=yes
> )
>]
> )  or $sftp->error;
>
> The clue to what is going wrong will be in the output from ssh.
> Thanks,
> Dermot
>
> On Mon, 17 Apr 2023 at 10:13, Brent Wood via beginners 
> wrote:
>
> More detail, thanks for your time...
>
>
> This is to test the Perl script on a local (Linux) system, copying a file
> from /tmp to another directory.
>
> I can use command line sftp to copy a file fine with the user/password
> connection, so there is not a problem with sftp, user/password etc on the
> system.
>
> This Perl script executes without error, but hangs on $sftp = Net::...
> (prints start, never prints done)
>
> If I take out the password assignment, I'm prompted for a password and it
> then works fine. With it there, it hangs.
>
> Can anyone help me get this working with a password passed as a parameter?
>
> use Net::SFTP::Foreign;
> use IO::Pty;
> use feature say;
>
> $host = "127.0.0.1";
> $user = "baw";
> $pass = "..";
>
> say "start";
>
> $sftp = Net::SFTP::Foreign->new($host,
> user => $user,
> password => $pass,
> more => [qw(-o PreferredAuthentications=password
> -o PasswordAuthentication=yes -o BatchMode=yes)]
> );
>
> say "done";
>
>


Re: help with making an sftp connection

2023-04-17 Thread Dermot
Hi,

Can you edit the constructor to include `more => [ -v ]` so we can get more
feedback from the SSH connection and send the output from that.

$sftp = Net::SFTP::Foreign->new($host,
user => $user,
password => $pass,
more => [
qw(
-v
-o PreferredAuthentications=password
-o PasswordAuthentication=yes
-o BatchMode=yes
)
   ]
)  or $sftp->error;

The clue to what is going wrong will be in the output from ssh.
Thanks,
Dermot

On Mon, 17 Apr 2023 at 10:13, Brent Wood via beginners 
wrote:

> More detail, thanks for your time...
>
>
> This is to test the Perl script on a local (Linux) system, copying a file
> from /tmp to another directory.
>
> I can use command line sftp to copy a file fine with the user/password
> connection, so there is not a problem with sftp, user/password etc on the
> system.
>
> This Perl script executes without error, but hangs on $sftp = Net::...
> (prints start, never prints done)
>
> If I take out the password assignment, I'm prompted for a password and it
> then works fine. With it there, it hangs.
>
> Can anyone help me get this working with a password passed as a parameter?
>
> use Net::SFTP::Foreign;
> use IO::Pty;
> use feature say;
>
> $host = "127.0.0.1";
> $user = "baw";
> $pass = "..";
>
> say "start";
>
> $sftp = Net::SFTP::Foreign->new($host,
> user => $user,
> password => $pass,
> more => [qw(-o PreferredAuthentications=password
> -o PasswordAuthentication=yes -o BatchMode=yes)]
> );
>
> say "done";
>
>


Re: help with making an sftp connection

2023-04-16 Thread Jim Gibson via beginners
Perl has a repository of user-submitted modules called Comprehensive Perl 
Archive Network (CPAN). Net::SFTP::Foreign can be installed from there, and it 
looks like a good candidate, as does Net::SFTP. The two modules use different 
methods for invoking the SFTP protocol. I would try one and, if that doesn’t 
work, the other. You can also search CPAN for other FTP and SFTP modules. Most 
modules come with their own documentation and sample code. See this link for 
Net::STFP::Foreign: 

<https://metacpan.org/pod/Net::SFTP::Foreign>

Try what that gives you, and come back here if you have any specific questions 
or problems. It is always good to give it a try for yourself before asking for 
help. Post what you try.


> On Apr 16, 2023, at 2:40 PM, Brent Wood via beginners  
> wrote:
> 
> Hi,
> 
> I'm not a perl coder, (though have written a few very simple Perl scripts 
> when necessary) but now I need to write a Perl script to transfer specified 
> files from one computer to another. I have scribbled out a script a while 
> back using FTP, but now need to use SFTP. I can re-use most of the old FTP 
> code/logic, but need some help to make the actual connection.
> 
> It is a secure (no external connectivity) network, with no public/private key 
> security available, so authentication just via user/password
> 
> Some Googling for how I might do this in Perl has thrown up several 
> possibilities, more confusing than informative, so I'm not sure how to best 
> do this.
> 
> It looks like Net::SFTP::Foreign might best fit my needs, but I'm open to 
> suggestions.
> 
> 
> Advice or a code snippet on how to make the connection should be enough, and 
> much appreciated!!
> 
> 
> 
> Thanks,
> 
> Brent Wood

Jim Gibson
j...@gibson.org




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




help with making an sftp connection

2023-04-16 Thread Brent Wood via beginners
Hi,
I'm not a perl coder, (though have written a few very simple Perl scripts when 
necessary) but now I need to write a Perl script to transfer specified files 
from one computer to another. I have scribbled out a script a while back using 
FTP, but now need to use SFTP. I can re-use most of the old FTP code/logic, but 
need some help to make the actual connection.

It is a secure (no external connectivity) network, with no public/private key 
security available, so authentication just via user/password

Some Googling for how I might do this in Perl has thrown up several 
possibilities, more confusing than informative, so I'm not sure how to best do 
this.
It looks like Net::SFTP::Foreign might best fit my needs, but I'm open to 
suggestions.

Advice or a code snippet on how to make the connection should be enough, and 
much appreciated!!


Thanks,
Brent Wood


Re: Please help: perl run out of memory

2022-04-27 Thread David Emanuel da Costa Santiago




Às 11:33 de 17/04/22, wilson escreveu:

hello the experts,

can you help check my script for how to optimize it?
currently it was going as "run out of memory".

$ perl count.pl
Out of memory!
Killed


My script:
use strict;

my %hash;
my %stat;



To be honest you don't need the %stat, however you'll need to go through 
the %hash several times for the average calculation. It will also make 
the script more unreadable.


You can also read the file several times, and on each time extract only 
the values of only one item.


The best option is for you to use a profiler to figure it out where the 
most memory is being consumed...



Good luck.

Regards,
David Santiago

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Please help: perl run out of memory

2022-04-26 Thread hw


On Sun, 2022-04-17 at 17:33 +0800, wilson wrote:
> hello the experts,
> 
> can you help check my script for how to optimize it?
> currently it was going as "run out of memory".
> 
> $ perl count.pl
> Out of memory!
> Killed

I would use a database like Mariadb for this, not only to create a
report but also to store the data.  Check out https://dbi.perl.org/
and phpmyadmin.  IIRC DBI can also work with CSV files as database
tables.  There's also Text::CSV ...

If you want to re-invent the wheel, you can do it in perl going itemId
by itemId, line by line, using another file to store the results per
itemId, and then come up with a variant of bubblesort that uses files,
too.

That's assuming that you have not as many itemIds as you have rows.
Maybe you can make it faster by using an index for the itemId field.


> 
> My script:
> use strict;
> 
> my %hash;
> my %stat;
> 
> # dataset: userId, itemId, rate, time
> # AV056ETQ5RXLN,031887,1.0,1397692800
> 
> open HD,"rate.csv" or die $!;
> while() {
>  my ($item,$rate) = (split /\,/)[1,2];
>  $hash{$item}{total} += $rate;
>  $hash{$item}{count} +=1;
> }
> close HD;
> 
> for my $key (keys %hash) {
>  $stat{$key} = $hash{$key}{total} / $hash{$key}{count};
> }
> 
> my $i = 0;
> for (sort { $stat{$b} <=> $stat{$a}} keys %stat) {
>  print "$_: $stat{$_}\n";
>  last if $i == 99;
>  $i ++;
> }
> 
> The purpose is to aggregate and average the itemId's scores, and print 
> the result after sorting.
> 
> The dataset has 80+ million items:
> 
> $ wc -l rate.csv
> 82677131 rate.csv
> 
> And my memory is somewhat limited:
> 
> $ free -m
>totalusedfree  shared  buff/cache 
> available
> Mem:   1992 152  76   01763 
>1700
> Swap:  1023 802 221
> 
> 
> 
> What confused me is that Apache Spark can make this job done with this 
> limited memory. It got the statistics done within 2 minutes. But I want 
> to give perl a try since it's not that convenient to run a spark job always.
> 
> The spark implementation:
> 
> scala> val schema="uid STRING,item STRING,rate FLOAT,time INT"
> val schema: String = uid STRING,item STRING,rate FLOAT,time INT
> 
> scala> val df = 
> spark.read.format("csv").schema(schema).load("skydrive/rate.csv")
> val df: org.apache.spark.sql.DataFrame = [uid: string, item: string ... 
> 2 more fields]
> 
> scala> 
> df.groupBy("item").agg(avg("rate").alias("avg_rate")).orderBy(desc("avg_rate")).show()
> +--++ 
> 
> >  item|avg_rate|
> +--++
> > 0001061100| 5.0|
> > 0001543849| 5.0|
> > 0001061127| 5.0|
> > 0001019880| 5.0|
> > 0001062395| 5.0|
> > 143502| 5.0|
> > 14357X| 5.0|
> > 0001527665| 5.0|
> > 000107461X| 5.0|
> > 191639| 5.0|
> > 0001127748| 5.0|
> > 791156| 5.0|
> > 0001203088| 5.0|
> > 0001053744| 5.0|
> > 0001360183| 5.0|
> > 0001042335| 5.0|
> > 0001374400| 5.0|
> > 0001046810| 5.0|
> > 0001380877| 5.0|
> > 0001050230| 5.0|
> +--++
> only showing top 20 rows
> 
> 
> I think my perl script should be possible to be optimized to run this 
> job as well. So ask for your helps.
> 
> Thanks in advance.
> 
> wilson
> 



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Please help: perl run out of memory

2022-04-22 Thread David Precious
On Thu, 21 Apr 2022 07:12:07 -0700
al...@coakmail.com wrote:

> OP maybe need the streaming IO for reading files.

Which is what they were already doing - they used:

while () {
...
}

Which, under the hood, uses readline, to read a line at a time.

(where "HD" is their global filehandle - a lexical filehandle would
have been better, but makes no difference here)


You can use B::Deparse to see that the above deparses to a use of
readline:


  [davidp@columbia:~]$ cat tmp/readline
  #!/usr/bin/env perl
  
  while () {
  print "Line: $_\n";
  }
  
  [davidp@columbia:~]$ perl -MO=Deparse tmp/readline
  while (defined($_ = readline STDIN)) {
  print "Line: $_\n";
  }
  tmp/readline syntax OK


So, they're already reading line-wise, it seems they're just
running in to memory usage issues from holding a hash of 80+million
values, which is not super suprising on a reasonably low-memory box.

Personally, if it were me, I'd go one of two ways:

* just throw some more RAM at it - these days, RAM is far cheaper than
  programmer time trying to squeeze it into the least amount of bytes,
  especially true if it's a quick "Get It Done" solution

* hand it off to a tool made for the job - import the data into SQLite
  or some other DB engine and let it do what it's designed for, as it's
  likely to be far more efficient than a hand-rolled Perl solution.
  (They already proved that Apache Spark can handle it on the same
  hardware)


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Please help: perl run out of memory

2022-04-21 Thread alice
OP maybe need the streaming IO for reading files.

Thanks 

On 2022-04-21 21:56, David Precious wrote:
> On Thu, 21 Apr 2022 17:26:15 +0530
> "M.N Thanishka sree Manikandan"  wrote:
> 
>> Hi wilson
>> Try  this module file::slurp
> 
> Given that the OP is running into memory issues processing an 80+
> million line file, I don't think suggesting a CPAN module designed to
> read the entire contents of a file into memory is going to be very
> helpful :)

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Please help: perl run out of memory

2022-04-21 Thread David Precious
On Thu, 21 Apr 2022 17:26:15 +0530
"M.N Thanishka sree Manikandan"  wrote:

> Hi wilson
> Try  this module file::slurp

Given that the OP is running into memory issues processing an 80+
million line file, I don't think suggesting a CPAN module designed to
read the entire contents of a file into memory is going to be very
helpful :)


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Please help: perl run out of memory

2022-04-21 Thread M.N Thanishka sree Manikandan
Hi wilson
Try  this module file::slurp

Regards,
Manikandan

On Sun, 17 Apr, 2022, 15:03 wilson,  wrote:

> hello the experts,
>
> can you help check my script for how to optimize it?
> currently it was going as "run out of memory".
>
> $ perl count.pl
> Out of memory!
> Killed
>
>
> My script:
> use strict;
>
> my %hash;
> my %stat;
>
> # dataset: userId, itemId, rate, time
> # AV056ETQ5RXLN,031887,1.0,1397692800
>
> open HD,"rate.csv" or die $!;
> while() {
>  my ($item,$rate) = (split /\,/)[1,2];
>  $hash{$item}{total} += $rate;
>  $hash{$item}{count} +=1;
> }
> close HD;
>
> for my $key (keys %hash) {
>  $stat{$key} = $hash{$key}{total} / $hash{$key}{count};
> }
>
> my $i = 0;
> for (sort { $stat{$b} <=> $stat{$a}} keys %stat) {
>  print "$_: $stat{$_}\n";
>  last if $i == 99;
>  $i ++;
> }
>
> The purpose is to aggregate and average the itemId's scores, and print
> the result after sorting.
>
> The dataset has 80+ million items:
>
> $ wc -l rate.csv
> 82677131 rate.csv
>
> And my memory is somewhat limited:
>
> $ free -m
>totalusedfree  shared  buff/cache
> available
> Mem:   1992 152  76   01763
>1700
> Swap:  1023 802 221
>
>
>
> What confused me is that Apache Spark can make this job done with this
> limited memory. It got the statistics done within 2 minutes. But I want
> to give perl a try since it's not that convenient to run a spark job
> always.
>
> The spark implementation:
>
> scala> val schema="uid STRING,item STRING,rate FLOAT,time INT"
> val schema: String = uid STRING,item STRING,rate FLOAT,time INT
>
> scala> val df =
> spark.read.format("csv").schema(schema).load("skydrive/rate.csv")
> val df: org.apache.spark.sql.DataFrame = [uid: string, item: string ...
> 2 more fields]
>
> scala>
>
> df.groupBy("item").agg(avg("rate").alias("avg_rate")).orderBy(desc("avg_rate")).show()
> +--++
>
> |  item|avg_rate|
> +--++
> |0001061100| 5.0|
> |0001543849| 5.0|
> |0001061127| 5.0|
> |0001019880| 5.0|
> |0001062395| 5.0|
> |143502| 5.0|
> |14357X| 5.0|
> |0001527665| 5.0|
> |000107461X| 5.0|
> |191639| 5.0|
> |0001127748| 5.0|
> |791156| 5.0|
> |0001203088| 5.0|
> |0001053744| 5.0|
> |0001360183| 5.0|
> |0001042335| 5.0|
> |0001374400| 5.0|
> |0001046810| 5.0|
> |0001380877| 5.0|
> |0001050230| 5.0|
> +--++
> only showing top 20 rows
>
>
> I think my perl script should be possible to be optimized to run this
> job as well. So ask for your helps.
>
> Thanks in advance.
>
> wilson
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Please help: perl run out of memory

2022-04-21 Thread Adriel Peng
I am not sure, but can Tie::Hash etc be used by tying hash to a local file
to reduce the memory use?

regards.


Re: Please help: perl run out of memory

2022-04-18 Thread Rob Coops
Hi Wilson,

Looking at the script I see some room for improvement. You currently
declare %hash as a global variable, and keep it around forever. With tens
of millions of rows that is quite a large structure to just have sitting
around after you have build the %stat hash. So I would start by limiting
the scope of the %hash from global to only till %stat has been filled. Of
course then there is the posibility of swapping the key and the value in
your stats hash, making the value an array of item ids or even a string of
item ids (might make sense if you do not expect to ever print all of them
in a list but only a top 1000 or so.

As for sorting a hugh hash in memory there comes a point where one has to
decide if Perl is the right tool for the job. It can probably do the job
but much like you can hammer a nail into a wall with a swiss armyknife Perl
is likely not the best tool for the job here. Looking around I see mostly
people advising to use a DB solution of some description be that Berkley
DB, SQLite or similar relatively simple database solutions you are likely
going to find this a much faster option than using pure Perl for sorting
such large hashes.
The reason why Spark is so much faster at this and more memory efficient at
the same time is that it has been designed to handle huge datasets like
this which very often need to be sorted or counted in some way. Though you
can definetely get the same output using Perl you will likely find that you
are looking at a nail while holding a swiss armyknife.

Just generally speaking having any variable sticking around for any length
of time after you are done with it is bad prectice in all languages. In an
interpeted language such as perl there is no way for an optimization step
to know you are never going to use this variable again so Perl is likely
going to hold on to the variable till the program exists where in a
compiled language an compiler might be able to see that this is the last
time this variable is used thus evict it from memory on your behalf but
that is only possible in some cases in many others such optimisations are
not going to kick in as the compiler cannot be certain that this function
that uses the variable is never going to be called again for instance. This
is why global variables in all languages are generally a bad idea but in
interpeted languages even more so.

Hope that helps a bit,

Rob

On Sun, Apr 17, 2022 at 8:00 PM David Mertens 
wrote:

> I see nothing glaringly inefficient in the Perl. This would be fine on
> your system if you were dealing with 1 million items, but you could easily
> be pushing up against your system's limits with the generic data structures
> that Perl uses, especially since Perl is probably using 64-bit floats and
> ints, and storing the hash keys twice (because you have to hashes).
>
> You could try to use the Perl Data Language, PDL, to create large typed
> arrays with minimal overhead. However, I think a more Perlish approach
> would be to use a single hash to store the data, as you do (or maybe using
> pack/unpack to store the data using 32-bit floats and integers). Then
> instead of using sort, run through the whole collection and build your own
> top-20 list (or 50 or whatever) by hand. This way the final process of
> picking out the top 20 doesn't allocate new storage for all 80 million
> items.
>
> Does that make sense? I could bang out some code illustrating what I mean
> if that would help.
>
> David
>
> On Sun, Apr 17, 2022, 5:33 AM wilson  wrote:
>
>> hello the experts,
>>
>> can you help check my script for how to optimize it?
>> currently it was going as "run out of memory".
>>
>> $ perl count.pl
>> Out of memory!
>> Killed
>>
>>
>> My script:
>> use strict;
>>
>> my %hash;
>> my %stat;
>>
>> # dataset: userId, itemId, rate, time
>> # AV056ETQ5RXLN,031887,1.0,1397692800
>>
>> open HD,"rate.csv" or die $!;
>> while() {
>>  my ($item,$rate) = (split /\,/)[1,2];
>>  $hash{$item}{total} += $rate;
>>  $hash{$item}{count} +=1;
>> }
>> close HD;
>>
>> for my $key (keys %hash) {
>>  $stat{$key} = $hash{$key}{total} / $hash{$key}{count};
>> }
>>
>> my $i = 0;
>> for (sort { $stat{$b} <=> $stat{$a}} keys %stat) {
>>  print "$_: $stat{$_}\n";
>>  last if $i == 99;
>>  $i ++;
>> }
>>
>> The purpose is to aggregate and average the itemId's scores, and print
>> the result after sorting.
>>
>> The dataset has 80+ million items:
>>
>> $ wc -l rate.csv
>> 82677131 rate.csv
>>
>> And my memory is somewhat limited:
>>
>> $ free -m
>>totalused 

Re: Please help: perl run out of memory

2022-04-17 Thread David Mertens
I see nothing glaringly inefficient in the Perl. This would be fine on your
system if you were dealing with 1 million items, but you could easily be
pushing up against your system's limits with the generic data structures
that Perl uses, especially since Perl is probably using 64-bit floats and
ints, and storing the hash keys twice (because you have to hashes).

You could try to use the Perl Data Language, PDL, to create large typed
arrays with minimal overhead. However, I think a more Perlish approach
would be to use a single hash to store the data, as you do (or maybe using
pack/unpack to store the data using 32-bit floats and integers). Then
instead of using sort, run through the whole collection and build your own
top-20 list (or 50 or whatever) by hand. This way the final process of
picking out the top 20 doesn't allocate new storage for all 80 million
items.

Does that make sense? I could bang out some code illustrating what I mean
if that would help.

David

On Sun, Apr 17, 2022, 5:33 AM wilson  wrote:

> hello the experts,
>
> can you help check my script for how to optimize it?
> currently it was going as "run out of memory".
>
> $ perl count.pl
> Out of memory!
> Killed
>
>
> My script:
> use strict;
>
> my %hash;
> my %stat;
>
> # dataset: userId, itemId, rate, time
> # AV056ETQ5RXLN,031887,1.0,1397692800
>
> open HD,"rate.csv" or die $!;
> while() {
>  my ($item,$rate) = (split /\,/)[1,2];
>  $hash{$item}{total} += $rate;
>  $hash{$item}{count} +=1;
> }
> close HD;
>
> for my $key (keys %hash) {
>  $stat{$key} = $hash{$key}{total} / $hash{$key}{count};
> }
>
> my $i = 0;
> for (sort { $stat{$b} <=> $stat{$a}} keys %stat) {
>  print "$_: $stat{$_}\n";
>  last if $i == 99;
>  $i ++;
> }
>
> The purpose is to aggregate and average the itemId's scores, and print
> the result after sorting.
>
> The dataset has 80+ million items:
>
> $ wc -l rate.csv
> 82677131 rate.csv
>
> And my memory is somewhat limited:
>
> $ free -m
>totalusedfree  shared  buff/cache
> available
> Mem:   1992 152  76   01763
>1700
> Swap:  1023 802 221
>
>
>
> What confused me is that Apache Spark can make this job done with this
> limited memory. It got the statistics done within 2 minutes. But I want
> to give perl a try since it's not that convenient to run a spark job
> always.
>
> The spark implementation:
>
> scala> val schema="uid STRING,item STRING,rate FLOAT,time INT"
> val schema: String = uid STRING,item STRING,rate FLOAT,time INT
>
> scala> val df =
> spark.read.format("csv").schema(schema).load("skydrive/rate.csv")
> val df: org.apache.spark.sql.DataFrame = [uid: string, item: string ...
> 2 more fields]
>
> scala>
>
> df.groupBy("item").agg(avg("rate").alias("avg_rate")).orderBy(desc("avg_rate")).show()
> +--++
>
> |  item|avg_rate|
> +--++
> |0001061100| 5.0|
> |0001543849| 5.0|
> |0001061127| 5.0|
> |0001019880| 5.0|
> |0001062395| 5.0|
> |143502| 5.0|
> |14357X| 5.0|
> |0001527665| 5.0|
> |000107461X| 5.0|
> |191639| 5.0|
> |0001127748| 5.0|
> |791156| 5.0|
> |0001203088| 5.0|
> |0001053744| 5.0|
> |0001360183| 5.0|
> |0001042335| 5.0|
> |0001374400| 5.0|
> |0001046810| 5.0|
> |0001380877| 5.0|
> |0001050230| 5.0|
> +--++
> only showing top 20 rows
>
>
> I think my perl script should be possible to be optimized to run this
> job as well. So ask for your helps.
>
> Thanks in advance.
>
> wilson
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: regex help - only one value returned

2020-12-02 Thread Jim Gibson
In your original example:

print "match1='$1' '$2'\n" if ($T=~/^((mr|mrs|miss|dr|prof|sir) .{5,}?)\n/smi);
print "match2='$1' '$2'\n" if ($T=~/^(mr|mrs|miss|dr|prof|sir .{5,}?)\n/smi);

the interior parentheses in example one terminates the alternation, so the last 
string is ’sir’.

In example two, the alternation is not terminated until the first ‘)', so the 
last string is ’sir .{5,}?’. followed in the regular expression by the “\n” 
character. Since in $T ‘miss’ is not followed by an \n, the match fails. Vlado 
has explained how to group and terminate the alternation without capturing the 
match result.


> On Dec 2, 2020, at 6:08 AM, Gary Stainburn  
> wrote:
> 
> On 02/12/2020 13:56, Vlado Keselj wrote:
>> Well, it seems that the first one is what you want, but you just need to
>> use $1 and ignore $2.
>> 
>> You do need parentheses in '(mr|mrs|miss|dr|prof|sir)' but if you do not
>> want for them to be captured in $2, you can use:
>> '(?:mr|mrs|miss|dr|prof|sir)'.  For example:
>> 
>> print "match3='$1' '$2'\n" if
>> ($T=~/^((?:mr|mrs|miss|dr|prof|sir) .{5,}?)\n/smi);
>> 
>> would give output:
>> 
>> match3='Miss Jayne Doe' ''
> Perfect, thank you.
> 
> I can't ignore $2 as it's in a loop with other regex that genuinely returns 
> multiple matches.  The amendment to the REGEX worked perfectly.

It is always best to save the results of a match with capturing in another 
variable. The capturing variables $1, $2, etc. are not reassigned if a match 
fails, so if you use them after a failed match, they will be the values left 
over from a previous match. So do this:

my $salutation = $1;
my $name = $2;

If you don’t want a possible undefined value, so this instead:

my $name = $2 || '';


Jim Gibson
j...@gibson.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex help - only one value returned

2020-12-02 Thread Gary Stainburn

On 02/12/2020 13:56, Vlado Keselj wrote:

Well, it seems that the first one is what you want, but you just need to
use $1 and ignore $2.

You do need parentheses in '(mr|mrs|miss|dr|prof|sir)' but if you do not
want for them to be captured in $2, you can use:
'(?:mr|mrs|miss|dr|prof|sir)'.  For example:

print "match3='$1' '$2'\n" if
($T=~/^((?:mr|mrs|miss|dr|prof|sir) .{5,}?)\n/smi);

would give output:

match3='Miss Jayne Doe' ''

Perfect, thank you.

I can't ignore $2 as it's in a loop with other regex that genuinely 
returns multiple matches.  The amendment to the REGEX worked perfectly.


Gary

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex help - only one value returned

2020-12-02 Thread Vlado Keselj


Well, it seems that the first one is what you want, but you just need to 
use $1 and ignore $2.

You do need parentheses in '(mr|mrs|miss|dr|prof|sir)' but if you do not 
want for them to be captured in $2, you can use:
'(?:mr|mrs|miss|dr|prof|sir)'.  For example:

print "match3='$1' '$2'\n" if
($T=~/^((?:mr|mrs|miss|dr|prof|sir) .{5,}?)\n/smi);

would give output:

match3='Miss Jayne Doe' ''

On Wed, 2 Dec 2020, Gary Stainburn wrote:

> I have an array of regex expressions that I apply to text returned from
> tesseract.
> 
> Each match that I get then gets stored for future processing. However, I'm
> struggling with one regex.
> 
> The problem is that:
> 
> 1) with brackets round the titles it returns two matches.
> 2) without brackets, it returns nothing.
> 
> Can anyone point me at the correct syntax please.
> 
> Gary
> 
> [root@dev dev]# ./t
> match1='Miss Jayne Doe' 'Miss'
> [root@dev dev]# cat t
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my $T=< Customer name and address
> Miss Jayne Doe
> 19 Their Street
> Somewehere
> In Yorkshire
> IN1 3YY
> EOF
> 
> print "match1='$1' '$2'\n" if ($T=~/^((mr|mrs|miss|dr|prof|sir)
> .{5,}?)\n/smi);
> print "match2='$1' '$2'\n" if ($T=~/^(mr|mrs|miss|dr|prof|sir .{5,}?)\n/smi);
> [root@dev dev]#
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




regex help - only one value returned

2020-12-02 Thread Gary Stainburn
I have an array of regex expressions that I apply to text returned from 
tesseract.


Each match that I get then gets stored for future processing. However, 
I'm struggling with one regex.


The problem is that:

1) with brackets round the titles it returns two matches.
2) without brackets, it returns nothing.

Can anyone point me at the correct syntax please.

Gary

[root@dev dev]# ./t
match1='Miss Jayne Doe' 'Miss'
[root@dev dev]# cat t
#!/usr/bin/perl

use strict;
use warnings;

my $T=

Re: Help with REST API interaction.

2020-04-24 Thread Gil Magno
Hi Francisco,

2020-04-22 11:21:31 -0300 Francisco Acuña:
> Good day, I've been getting familiarized with Perl for the last couple of
> days due to a RT integration project I've been handed. I've also been doing
> a lot of research and a lot of asking around in forums for pointers on how
> to properly achieve this.
> 
> I'm glad to say that I managed to get all my basic scrips up and running,
> retrieving all the values I needed and updating all the fields I wanted to
> update. I now need to get to the part of actually interacting with the
> external RESTful API.
> 
> If what I understood from my research is correct, I will be working with
> the REST::Client and JSON modules. And what I need to do is:
> 
>1. Encode the values I want to pass over to the API into a JSON object.
>2. Do some coding on the other side in order to achieve my desired
>funcionalities and pass some values back to RT (for this I won't need your
>help since the coding won't be done in Perl, sadly)
>3. Decode the response I get back to Perl and use those values in RT to
>do some more updating.
> 
> I would greatly appreciate if you could provide me with some pointers and
> code examples of this being done. Choosing the values I need to pass,
> encoding them, passing them over to the API, and then decoding the response.
> 
> Needless to say, I am documenting all my work and will gladly share my
> experience in this project and all the coding work in any forum.

One way to do it is like this:

use strict;
use warnings;
use feature 'say';
use LWP::UserAgent;
use JSON;

my $ua = LWP::UserAgent->new;
my $uri = 'https://example.com/path/to/something';
my %headers = ('Content-Type' => 'application/json');

# here are the values to pass to the API
my %values = (number => 1, name => 'etc');

my $res = $ua->post(
$uri,
%headers,
Content => JSON::encode_json(\%values)
);

if ($ua->is_success) {
# if you're receiving a JSON
my $data = JSON::decode_json $res->content;
# now $data is a Perl data structure; it's the decoded JSON

# but if you're receiving (say) HTML
say $res->decoded_content;
}
else {
# handle errors here
}

See these docs for the modules' details:
https://metacpan.org/pod/LWP::UserAgent
https://metacpan.org/pod/JSON


signature.asc
Description: PGP signature


Re: Help with REST API interaction.

2020-04-24 Thread Gil Magno
Correction: we should use $res->is_success in the 'if' below, not
$ua->is_success.

2020-04-24 19:42:32 -0300 Gil Magno:
> if ($ua->is_success) {


signature.asc
Description: PGP signature


Help with REST API interaction.

2020-04-24 Thread Francisco Acuña
Good day, I've been getting familiarized with Perl for the last couple of
days due to a RT integration project I've been handed. I've also been doing
a lot of research and a lot of asking around in forums for pointers on how
to properly achieve this.

I'm glad to say that I managed to get all my basic scrips up and running,
retrieving all the values I needed and updating all the fields I wanted to
update. I now need to get to the part of actually interacting with the
external RESTful API.

If what I understood from my research is correct, I will be working with
the REST::Client and JSON modules. And what I need to do is:

   1. Encode the values I want to pass over to the API into a JSON object.
   2. Do some coding on the other side in order to achieve my desired
   funcionalities and pass some values back to RT (for this I won't need your
   help since the coding won't be done in Perl, sadly)
   3. Decode the response I get back to Perl and use those values in RT to
   do some more updating.

I would greatly appreciate if you could provide me with some pointers and
code examples of this being done. Choosing the values I need to pass,
encoding them, passing them over to the API, and then decoding the response.

Needless to say, I am documenting all my work and will gladly share my
experience in this project and all the coding work in any forum.


Re: Help me with a regex problem

2019-10-26 Thread Dermot
You might consider using Regexp::Common::net. It provides a convenient set
of functions for matching IP v4, v6 and mac addresses.


https://metacpan.org/pod/Regexp::Common::net

On Fri, 25 Oct 2019 at 19:43, John W. Krahn  wrote:

> On 2019-10-25 3:23 a.m., Maggie Q Roth wrote:
> >   Hello
>
> Hello.
>
> > There are two primary types of lines in the log:
>
> What are those two types?  How do you define them?
>
>
> > 60.191.38.xx/
> > 42.120.161.xx   /archives/1005
>
>  From my point of view those two lines have two fields, the first looks
> like an IP address and the second looks like a file path.  In other
> words I can't distinguish the difference between these two "types".
>
>
> > I know how to write regex to match each line, but don't get the good
> result
> > with one regex to match both lines.
> >
> > Can you help?
>
> Perhaps if you could describe the problem better?
>
>
> John
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Help me with a regex problem

2019-10-25 Thread John W. Krahn

On 2019-10-25 3:23 a.m., Maggie Q Roth wrote:

  Hello


Hello.


There are two primary types of lines in the log:


What are those two types?  How do you define them?



60.191.38.xx/
42.120.161.xx   /archives/1005


From my point of view those two lines have two fields, the first looks 
like an IP address and the second looks like a file path.  In other 
words I can't distinguish the difference between these two "types".




I know how to write regex to match each line, but don't get the good result
with one regex to match both lines.

Can you help?


Perhaps if you could describe the problem better?


John

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help me with a regex problem

2019-10-25 Thread Andy Bach
/(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(?\/.*)/

To avoid the "leaning toothpick" problem, Perl lets use different match
delimiters, so the above is the same as:
m#(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(?/.*)#

I assume you want to capture the IP and the path, right?
if ( $entry =~ m#([\d.]+)\s+(/\S+)# ) {
   my ($ip, $path) = ($1, $2);
   print "IP $ip asked for path $path\n";

On Fri, Oct 25, 2019 at 5:28 AM Илья Рассадин  wrote:

> For example, this regex
>
> /(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(?\/.*)/
>
> On 25.10.2019 13:23, Maggie Q Roth wrote:
> > Hello
> >
> > There are two primary types of lines in the log:
> >
> > 60.191.38.xx/
> > 42.120.161.xx   /archives/1005
> >
> > I know how to write regex to match each line, but don't get the good
> > result with one regex to match both lines.
> >
> > Can you help?
> >
> > Thanks,
> > Maggie
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: Help me with a regex problem

2019-10-25 Thread Benjamin S Pendygraft II
That is a backslash followed by a forward slash. The backslash tells the
regex parser to treat the next character as a literal character. Useful for
matching periods, question marks, brackets, etc.
A period matches any character once and an asterisk matches the previous
character any number of times. .* basically means match everything.

Apologies if this is formatted incorrectly. Sending from my phone.

On Fri, Oct 25, 2019 at 06:37 Maggie Q Roth  wrote:

> what's V.*?
>
> Maggie
>
> On Fri, Oct 25, 2019 at 6:28 PM Илья Рассадин  wrote:
>
>> For example, this regex
>>
>> /(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(?\/.*)/
>>
>> On 25.10.2019 13:23, Maggie Q Roth wrote:
>> > Hello
>> >
>> > There are two primary types of lines in the log:
>> >
>> > 60.191.38.xx/
>> > 42.120.161.xx   /archives/1005
>> >
>> > I know how to write regex to match each line, but don't get the good
>> > result with one regex to match both lines.
>> >
>> > Can you help?
>> >
>> > Thanks,
>> > Maggie
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>> For additional commands, e-mail: beginners-h...@perl.org
>> http://learn.perl.org/
>>
>>
>> --
Benjamin Pendygraft


Re: Help me with a regex problem

2019-10-25 Thread X Dungeness
my $n = '[0-9]{1,3}';
if  (  =~ (  m[ (?:$n\.){3} $n \s+ \S+ ]x )
{
   # match
}


On Fri, Oct 25, 2019 at 3:37 AM Maggie Q Roth  wrote:

> what's V.*?
>
> Maggie
>
> On Fri, Oct 25, 2019 at 6:28 PM Илья Рассадин  wrote:
>
>> For example, this regex
>>
>> /(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(?\/.*)/
>>
>> On 25.10.2019 13:23, Maggie Q Roth wrote:
>> > Hello
>> >
>> > There are two primary types of lines in the log:
>> >
>> > 60.191.38.xx/
>> > 42.120.161.xx   /archives/1005
>> >
>> > I know how to write regex to match each line, but don't get the good
>> > result with one regex to match both lines.
>> >
>> > Can you help?
>> >
>> > Thanks,
>> > Maggie
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>> For additional commands, e-mail: beginners-h...@perl.org
>> http://learn.perl.org/
>>
>>
>>


Re: Help me with a regex problem

2019-10-25 Thread Maggie Q Roth
what's V.*?

Maggie

On Fri, Oct 25, 2019 at 6:28 PM Илья Рассадин  wrote:

> For example, this regex
>
> /(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(?\/.*)/
>
> On 25.10.2019 13:23, Maggie Q Roth wrote:
> > Hello
> >
> > There are two primary types of lines in the log:
> >
> > 60.191.38.xx/
> > 42.120.161.xx   /archives/1005
> >
> > I know how to write regex to match each line, but don't get the good
> > result with one regex to match both lines.
> >
> > Can you help?
> >
> > Thanks,
> > Maggie
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Help me with a regex problem

2019-10-25 Thread Илья Рассадин

For example, this regex

/(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(?\/.*)/

On 25.10.2019 13:23, Maggie Q Roth wrote:

Hello

There are two primary types of lines in the log:

60.191.38.xx        /
42.120.161.xx       /archives/1005

I know how to write regex to match each line, but don't get the good 
result with one regex to match both lines.


Can you help?

Thanks,
Maggie


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Help me with a regex problem

2019-10-25 Thread Maggie Q Roth
 Hello

There are two primary types of lines in the log:

60.191.38.xx/
42.120.161.xx   /archives/1005

I know how to write regex to match each line, but don't get the good result
with one regex to match both lines.

Can you help?

Thanks,
Maggie


Re: Help with Perl 6 script

2019-08-12 Thread Ralph Mellor
  the > I have this Perl 6 script from Rosetta, which I wanted to run on Perl 5
> (due to the Active Sate Perl and App version that I have).

If ActiveState have packaged https://metacpan.org/pod/Inline::Perl6
then please install and use that. (If they haven't, please ask them to
do so.)

> However, several syntax errors appear given the difference (some) in language.

For some simple examples P5 and P6 code is very similar and so your
"(some)" qualifier is about as appropriate as saying there's (some) difference
between Python 2 and Python 3. (There is, but not a lot.)

For code like you've picked the difference is halfway to the difference
between English and German (both are West Germanic languages but
saying there's (some) difference in language doesn't really do justice to
the difference).

With that understood...

> I'm having problem specially in the "multi infix" and even in the "norm" sub 
> routine.



# Simple Vector implementation
multi infix:<+>(@a, @b) { @a Z+ @b }

multi is short for multiple dispatch.

In P5, perhaps using the multi features of https://metacpan.org/pod/Dios
is the way to go, or maybe there's some other P5 package that implements
multiple dispatch.

Alternatively you could manually write the logic for routing certain
argument combinations to particular code. (That would drive me crazy.)

If there's no sub or method after the multi then it's a sub.

Reminder before continuing:

# Simple Vector implementation
multi infix:<+>(@a, @b) { @a Z+ @b }

infix:<+> names the sub.
It requires two Positional arguments (@ as a parameter).
Positional is a role; the arguments must do that role.
(Sorta like a signature expecting two arrayrefs.)
It zips them by calling infix + on pairings of their elements.

For example:

say infix:<+> (1,2), (3,4); # (4,6)

Because it's called infix:<+> the declaration also immediately
installs it in the grammar as an infix operator, so you can
instead just write:

say (1,2) + (3,4); # (4,6)

Note that this means it's the same as:

say (1,2) Z+ (3,4); # (4,6)

In this latter case the infix Z metaop means  to call
the + on pairings of its two lists, i.e. 1 + 3 and 2 + 4.
Because this latter call passes two *scalar* arguments
to the infix + the multiple dispatch doesn't recursively
call the infix:<+> we defined above but instead the built
in infix:<+> which is something vaguely like:

multi infix:<+> ($a, $b) { ... treats args as two numbers ... }



[+] arg1, arg2, arg3, ...

Does the same as:

arg1 + arg2 + arg3 ...

which, if args are scalars, is same as:

sum arg1, arg2, arg3



return -> \t, \y, \δt {

-> ... { ... }

is similar to an anonymous sub routine.

The first ... is its parameter list.

\foo in a parameter list declares a sigil-free "single static binding variable",
a *run-time* constant value.

# gravitational constant
constant G = 6.674e-11;

That's a *compile-time* constant value.

The e-11 means the number is a floating point number.

constant year = 365.25*24*60*60;

The 365.25 (no trailing e) means the number is a rational number.

my @a = @ABC[0..2];

sigils are "invariant". @ABC[0..2] is a slice, a list of the first
three elements of the Positional variable @ABC.

For convenience I'll call a Positional variable a "list" from now on.

my $ab = norm(@a - @b);

Without a multi infix:<-> defined the above code would norm
the difference between the length (number of elements) of the
two lists.

flat

P5 generally autoflattens sub lists into one long list. You have to
explicitly mark when you want an arrayref instead. In contrast P6
generally treats @foo as an arrayref. You have to explicitly flatten
if you want that.

map G * *,

The first * is in infix operator position. It denotes the infix
multiply operator.

The second is in term position. It denotes a Whatever.

When a Whatever appears with an operator, the operator decides
whether it cares (by having a multi that includes it as a parameter).

If it doesn't care, the compiler converts the expression it's in into an
anonymous routine. So the above code fragment is the same as:

map { G * $_ },

If you'd like to thank me, send love to Larry Wall and either install
Inline::Perl6 or ask ActiveState to package it.

love, ralph

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help with Perl 6 script

2019-08-12 Thread Ralph Mellor
Oops, missed the ending.

loop with a parens argument is like a C for with
3 args, init, test, next.

The next bit is:

($t, @ABC) »+=« (.01, dABC($t, @ABC, .01))

The » and « opops (or metaops) pack a whole lot of power but
can also be used for relatively simple cases like this one in which
they're just distributing the += to pairings of the left and right sides.

(But remember the infix + has also been redefined to distribute
over lists. All of » and « and Z and X in infix op slots are op ops
or metaops that distribute their ops to pairings of (elements of)
their arguments.)

.fmt("%+.3e")

is a method call, like ->fmt in P5.

Here is its doc:

https://docs.perl6.org/routine/fmt

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help with Perl 6 script

2019-08-12 Thread Marc Chantreux
hello Rui,

> I have this Perl 6 script from Rosetta, which I wanted to run on Perl 5
> (due to the Active Sate Perl and App version that I have).

Perl6 and Perl5 are very different. you need to download a perl6
interpretor if you want to run perl6 code.

please check https://rakudo.org/.

regards
marc

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help with Perl 6 script

2019-08-12 Thread Andy Bach
Yes, Perl 5 and Perl 6 are quite different in many ways.  Some suggested that 
Perl 6 be looked at as not the next iteration of Perl but a new language that 
can be made backward compatible with Perl 5 (there are switches etc that let 
you run P5 code unchanged) but it is not the case of P5 being able to run P6 
code.  You'll have to translate what the p6 code is doing into valid syntax for 
p5.  Or find p6 for windows.  There is a lot of p6 info out there, but not so 
much on the Perl for Beginners list. See:
 https://docs.perl6.org/<https://docs.perl6.org/language/quoting>

and there is the p6 users list
perl6-us...@perl.org

at:
https://lists.perl.org/all.html#p


From: Rui Fernandes 
Sent: Thursday, August 8, 2019 3:07 PM
To: Perl Beginners 
Subject: Help with Perl 6 script

Greetings

I have this Perl 6 script from Rosetta, which I wanted to run on Perl 5 (due to 
the Active Sate Perl and App version that I have). However, several syntax 
errors appear given the difference (some) in language.Here's the script:

---


# Simple Vector implementation
multi infix:<+>(@a, @b) { @a Z+ @b }
multi infix:<->(@a, @b) { @a Z- @b }
multi infix:<*>($r, @a) { $r X* @a }
multi infix:(@a, $r) { @a X/ $r }
sub norm { sqrt [+] @_ X** 2 }

# Runge-Kutta stuff
sub runge-kutta(&yp) {
return -> \t, \y, \δt {
my $a = δt * yp( t, y );
my $b = δt * yp( t + δt/2, y + $a/2 );
my $c = δt * yp( t + δt/2, y + $b/2 );
my $d = δt * yp( t + δt, y + $c );
($a + 2*($b + $c) + $d) / 6;
}
}

# gravitational constant
constant G = 6.674e-11;
# astronomical unit
constant au = 150e9;

# time constants in seconds
constant year = 365.25*24*60*60;
constant month = 21*24*60*60;

# masses in kg
constant $ma = 2e30; # Sun
constant $mb = 6e24; # Earth
constant $mc = 7.34e22;  # Moon

my &dABC = runge-kutta my &f = sub ( $t, @ABC ) {
my @a = @ABC[0..2];
my @b = @ABC[3..5];
my @c = @ABC[6..8];

my $ab = norm(@a - @b);
my $ac = norm(@a - @c);
my $bc = norm(@b - @c);

return [
flat
@ABC[@(9..17)],
map G * *,
$mb/$ab**3 * (@b - @a) + $mc/$ac**3 * (@c - @a),
$ma/$ab**3 * (@a - @b) + $mc/$bc**3 * (@c - @b),
$ma/$ac**3 * (@a - @c) + $mb/$bc**3 * (@b - @c);
];
}

loop (
my ($t, @ABC) = 0,
0, 0, 0, # Sun position
au, 0, 0,# Earth position
0.998*au, 0, 0,  # Moon position
0, 0, 0, # Sun speed
0, 2*pi*au/year, 0,  # Earth speed
0, 2*pi*(au/year + 0.002*au/month), 0# Moon speed
;
$t < .2;
($t, @ABC) »+=« (.01, dABC($t, @ABC, .01))
) {
printf "t = %.02f : %s\n", $t, @ABC.fmt("%+.3e");
}

---

I'm having problem specially in the "multi infix" and even in the "norm" sub 
routine. The problem is that I do not understand the construction of these in 
Perl 5 (otherwise, I would translate this easely, and I wouldn't be asking for 
help...)

Any help is apreciated.

Clear skies

Rui Fernandes


Re: Help with Perl 6 script

2019-08-12 Thread William Michels via beginners
Hi Rui, Have you considered just installing Perl 6 ?

https://rakudo.org
https://www.perl6.org

Spoiler alert: the Perl6 code you posted works with no errors on my
Perl6 install.

Best Regards, Bill.



On Thu, Aug 8, 2019 at 1:07 PM Rui Fernandes  wrote:
>
> Greetings
>
> I have this Perl 6 script from Rosetta, which I wanted to run on Perl 5 (due 
> to the Active Sate Perl and App version that I have). However, several syntax 
> errors appear given the difference (some) in language.Here's the script:
>
> ---
>
> # Simple Vector implementation
> multi infix:<+>(@a, @b) { @a Z+ @b }
> multi infix:<->(@a, @b) { @a Z- @b }
> multi infix:<*>($r, @a) { $r X* @a }
> multi infix:(@a, $r) { @a X/ $r }
> sub norm { sqrt [+] @_ X** 2 }
>
> # Runge-Kutta stuff
> sub runge-kutta(&yp) {
> return -> \t, \y, \δt {
> my $a = δt * yp( t, y );
> my $b = δt * yp( t + δt/2, y + $a/2 );
> my $c = δt * yp( t + δt/2, y + $b/2 );
> my $d = δt * yp( t + δt, y + $c );
> ($a + 2*($b + $c) + $d) / 6;
> }
> }
>
> # gravitational constant
> constant G = 6.674e-11;
> # astronomical unit
> constant au = 150e9;
>
> # time constants in seconds
> constant year = 365.25*24*60*60;
> constant month = 21*24*60*60;
>
> # masses in kg
> constant $ma = 2e30; # Sun
> constant $mb = 6e24; # Earth
> constant $mc = 7.34e22;  # Moon
>
> my &dABC = runge-kutta my &f = sub ( $t, @ABC ) {
> my @a = @ABC[0..2];
> my @b = @ABC[3..5];
> my @c = @ABC[6..8];
>
> my $ab = norm(@a - @b);
> my $ac = norm(@a - @c);
> my $bc = norm(@b - @c);
>
> return [
> flat
> @ABC[@(9..17)],
> map G * *,
> $mb/$ab**3 * (@b - @a) + $mc/$ac**3 * (@c - @a),
> $ma/$ab**3 * (@a - @b) + $mc/$bc**3 * (@c - @b),
> $ma/$ac**3 * (@a - @c) + $mb/$bc**3 * (@b - @c);
> ];
> }
>
> loop (
> my ($t, @ABC) = 0,
> 0, 0, 0, # Sun position
> au, 0, 0,# Earth position
> 0.998*au, 0, 0,  # Moon position
> 0, 0, 0, # Sun speed
> 0, 2*pi*au/year, 0,  # Earth speed
> 0, 2*pi*(au/year + 0.002*au/month), 0# Moon speed
> ;
> $t < .2;
> ($t, @ABC) »+=« (.01, dABC($t, @ABC, .01))
> ) {
> printf "t = %.02f : %s\n", $t, @ABC.fmt("%+.3e");
> }
>
>
> ---
>
> I'm having problem specially in the "multi infix" and even in the "norm" sub 
> routine. The problem is that I do not understand the construction of these in 
> Perl 5 (otherwise, I would translate this easely, and I wouldn't be asking 
> for help...)
>
> Any help is apreciated.
>
> Clear skies
>
> Rui Fernandes

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help with Perl 6 script

2019-08-12 Thread Bruce Gray
[ Executive Summary: Install Perl 6; it will not disturb your Activestate Perl 
5 : https://rakudo.org/files ]


> On Aug 8, 2019, at 3:07 PM, Rui Fernandes  wrote:
> 
> Greetings

Hi Rui!

> 
> I have this Perl 6 script from Rosetta, which I wanted to run on Perl 5 (due 
> to the Active Sate Perl and App version that I have).

I think you have an all-too-common misconception. 
Perl 6 is not the next version of Perl 5; Perl 6 is a “sister language”; the 
first Perl language to completely break backwards compatibility with 1..5.

> However, several syntax errors appear given the difference (some) in language.

The compiler is only showing you the *initial* syntax errors; the total number 
of changes needed to make this run in Perl 5 will be many more than “several”.
Translating from Perl 5 to Perl 6 often only requires minimal syntax changes. 
After that, you have the option to refactor the Perl 5 idioms in the converted 
code into more concise expressions.

When code has been written in idiomatic Perl 6 (as your code has been), 
translating back to Perl 5 can be quite effortful.
For example, just the first line that defines a plus operator for lists/arrays 
representing vectors:
multi infix:<+>(@a, @b) { @a Z+ @b }
, which would be simply invoked in Perl 6 as:
my @foo = @this + @that;
However, translated it into Perl 5, it would look something like this 
(untested):
sub vector_add {
die if @_ != 2;
my ($x_aref, $y_aref) = @_;
# warn if @{$x_aref} != @{$y_aref};
my $r = [];
for my $i ( keys @{$x_aref} ) {
$r->[$i] = $x_aref->[$i] + $y_aref->[$i];
}
return $r;
}
my @foo = @{ vector_add( \@this, \@that };
Even `norm` takes 4 SLOCs:
sub norm { sqrt [+] @_ X** 2 }
vs:
sub norm {
my $r = 0;
$r += $_ ** 2 for @_;
return sqrt $r;
}

I am curious as to why you need to use this Perl 6 code in Perl 5.
You could just run it in Perl 6, since the “Rakudo Star” Perl 6 distribution 
co-exists just fine with Activestate; you can get it here: 
https://rakudo.org/files .
Or, you could use the Perl 5 version of runge_kutta from just above where you 
found the Perl 6 version, on RosettaCode: 
http://rosettacode.org/wiki/Runge-Kutta_method#Perl .
Or, you could use one of the CPAN modules that provides the same function for 
Perl 5, like Math::RungeKutta or PDL::RungeKutta .

BTW, I think your `month` constant is wrong; You have it as 21 days, where I 
usually see it as either 28 or 29.53 or 30.4375.
Also, I have no idea what the line `$t < .2;` is supposed to do; it looks like 
dead code.

(LATE EDIT)
Oh, I just saw that the entire code that you posted comes from 
https://rosettacode.org/wiki/N-body_problem#Perl_6 , and that no one has made a 
Perl 5 solution for that RosettaCode task.
If you are intending to translate the Perl 6 code to Perl 5 in order to start 
contributing to RosettaCode, please contact me directly and I will be glad to 
work with you on it. (This will be more than appropriate for “Perl Beginners”)
If you are wanting to play with it for your own purposes, definitely just 
install Perl 6 as linked above.

> Here's the script:
—code snipped--
> I'm having problem specially in the "multi infix" and even in the "norm" sub 
> routine. The problem is that I do not understand the construction of these in 
> Perl 5 (otherwise, I would translate this easely, and I wouldn't be asking 
> for help...)
> 
> Any help is apreciated.
> 
> Clear skies
> 
> Rui Fernandes


— 
Hope this helps,
Bruce Gray (Util of PerlMonks)



Help with Perl 6 script

2019-08-08 Thread Rui Fernandes
Greetings

I have this Perl 6 script from Rosetta, which I wanted to run on Perl 5
(due to the Active Sate Perl and App version that I have). However, several
syntax errors appear given the difference (some) in language.Here's the
script:

---

# Simple Vector implementation
multi infix:<+>(@a, @b) { @a Z+ @b }
multi infix:<->(@a, @b) { @a Z- @b }
multi infix:<*>($r, @a) { $r X* @a }
multi infix:(@a, $r) { @a X/ $r }
sub norm { sqrt [+] @_ X** 2 }

# Runge-Kutta stuff
sub runge-kutta(&yp) {
return -> \t, \y, \δt {
my $a = δt * yp( t, y );
my $b = δt * yp( t + δt/2, y + $a/2 );
my $c = δt * yp( t + δt/2, y + $b/2 );
my $d = δt * yp( t + δt, y + $c );
($a + 2*($b + $c) + $d) / 6;
}
}

# gravitational constant
constant G = 6.674e-11;
# astronomical unit
constant au = 150e9;

# time constants in seconds
constant year = 365.25*24*60*60;
constant month = 21*24*60*60;

# masses in kg
constant $ma = 2e30; # Sun
constant $mb = 6e24; # Earth
constant $mc = 7.34e22;  # Moon

my &dABC = runge-kutta my &f = sub ( $t, @ABC ) {
my @a = @ABC[0..2];
my @b = @ABC[3..5];
my @c = @ABC[6..8];

my $ab = norm(@a - @b);
my $ac = norm(@a - @c);
my $bc = norm(@b - @c);

return [
flat
@ABC[@(9..17)],
map G * *,
$mb/$ab**3 * (@b - @a) + $mc/$ac**3 * (@c - @a),
$ma/$ab**3 * (@a - @b) + $mc/$bc**3 * (@c - @b),
$ma/$ac**3 * (@a - @c) + $mb/$bc**3 * (@b - @c);
];
}

loop (
my ($t, @ABC) = 0,
0, 0, 0, # Sun position
au, 0, 0,# Earth position
0.998*au, 0, 0,  # Moon position
0, 0, 0, # Sun speed
0, 2*pi*au/year, 0,  # Earth speed
0, 2*pi*(au/year + 0.002*au/month), 0# Moon speed
;
$t < .2;
($t, @ABC) »+=« (.01, dABC($t, @ABC, .01))
) {
printf "t = %.02f : %s\n", $t, @ABC.fmt("%+.3e");
}


---

I'm having problem specially in the "multi infix" and even in the "norm"
sub routine. The problem is that I do not understand the construction of
these in Perl 5 (otherwise, I would translate this easely, and I wouldn't
be asking for help...)

Any help is apreciated.

Clear skies

Rui Fernandes


Re: Help with "install_driver(Oracle) failed" error message

2019-03-12 Thread Uri Guttman

On 3/12/19 11:59 AM, Frank K. wrote:

Greeting,

In the infinite wisdom our company, without warning completely 
uninstalled Active Perl on all of our Windows servers (I believe it 
was v5.8).. They claimed it was a security risk, but I suspect new 
licensing fees were the main incentive..


Regardless,  I installed Strawberry Perl, 64 bit, and MOST everything 
is running “okay”.  I am getting the following error in one of our 
scripts (this was not a problem when Active State perl was installed):





hi,

i am sorry for your pain (in multiple dimensions! :). i can't help you 
but i would say this is somewhat beyond the purview of the beginner's 
list. there could be someone who could help but i would suggest finding 
other resources. try p5p (the perl developer's list) or a DBI list. 
below are links to how to subscribe to those lists.


https://lists.perl.org/list/perl5-porters.html
https://lists.perl.org/list/dbi-users.html

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Help with "install_driver(Oracle) failed" error message

2019-03-12 Thread Frank K.
Greeting,

In the infinite wisdom our company, without warning completely uninstalled
Active Perl on all of our Windows servers (I believe it was v5.8).. They
claimed it was a security risk, but I suspect new licensing fees were the
main incentive..

Regardless,  I installed Strawberry Perl, 64 bit, and MOST everything is
running “okay”.  I am getting the following error in one of our scripts
(this was not a problem when Active State perl was installed):

=

D:\ALM tools\Atlassian>echo off

perl atlassianuserlister.pl recordatlassianappusers prod

Getting JIRA User access info...

install_driver(Oracle) failed: Can't load
'C:/Strawberry/perl/vendor/lib/auto/DBD/Oracle/Oracle.xs.dll' for module
DBD::Oracle: load_file:%1 is not a valid Win32 application at
C:/Strawberry/perl/lib/DynaLoader.pm line 193.

at (eval 10) line 3.

Compilation failed in require at (eval 10) line 3.

Perhaps a required shared library or dll isn't installed where expected

at atlassianuserlister.pl line 1565.
=

We have 64 bit Perl, and ORACLE_HOME is pointing to the 64 bit drivers so I
am at a bit of a loss as to why it’s expecting a 32 bit dll.  I have
verified the dll is in that path.  Any ideas?  I’ve Googled a bit and can
find a bunch of hacks but….Any help would be most appreciated.. flk k


Re: help with another regex

2018-07-17 Thread Lauren C.
I think reading the official tutorial from begin is not that comfortable 
to a newbie. I bought a book "Learning Perl, 6th Edition" for studying 
step by step. thanks.



On 2018/7/18 星期三 AM 9:08, Uri Guttman wrote:
also i always recommend reading the entire perl FAQ as there are many 
regex tips and plenty of other useful stuff in them. and i mean the 
entire FAQ!! again, skip if you don't get something but come back later 
on to read it.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with another regex

2018-07-17 Thread Uri Guttman

On 07/17/2018 08:57 PM, Lauren C. wrote:

I did read them, but got no deep impression unless I met the issue. :)



not sure what kind of deep impression you need! :)

a key thing with docs is rereading them. read them once quickly all the 
way through to get a sense of the whole picture. read again but more 
slowly to learn the key things and practice them. then reread sections 
you need to know better as the core stuff will be somewhat familiar by 
then and you can delve deeper. and after you are done with the tutorial, 
read the full reference doc on the subject! the regex reference is very 
long and deep and you should follow the same pattern. don't be dismayed 
if you don't understand some topics. just skim and/or skip them. but get 
into the topics you already know from the tute as you will learn more 
about them. even experienced pros will go back to the docs without shame.


also i always recommend reading the entire perl FAQ as there are many 
regex tips and plenty of other useful stuff in them. and i mean the 
entire FAQ!! again, skip if you don't get something but come back later 
on to read it.


uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with another regex

2018-07-17 Thread Lauren C.

I did read them, but got no deep impression unless I met the issue. :)

Uri Guttman 写道:

On 07/17/2018 08:46 PM, Lauren C. wrote:

Thanks Gil. I think i know the difference of "\w+" and "\w*" now.



lauren, did you read the perlretut document? if not, you should. it 
covers quantifiers early on as they are one of the fundamental features 
of regexes. a key thing to learn is the {m,n} quantifier as it can do 
any number of repeats. the *, + and ? quantifiers are just shortcuts:


     ? is {0,1}    0 or 1
     * is {0,}    0 or more
     + is {1,}    1 or more

those are used so often they should become ingrained in your brain!

uri



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with another regex

2018-07-17 Thread Uri Guttman

On 07/17/2018 08:46 PM, Lauren C. wrote:

Thanks Gil. I think i know the difference of "\w+" and "\w*" now.



lauren, did you read the perlretut document? if not, you should. it 
covers quantifiers early on as they are one of the fundamental features 
of regexes. a key thing to learn is the {m,n} quantifier as it can do 
any number of repeats. the *, + and ? quantifiers are just shortcuts:


    ? is {0,1}    0 or 1
    * is {0,}    0 or more
    + is {1,}    1 or more

those are used so often they should become ingrained in your brain!

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with another regex

2018-07-17 Thread Lauren C.

yeah you explain that well. thanks.

Andy Bach 写道:

 > But it doesn't work for this case:

$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'

 > it expects 1 returned.

Well, assuming you mean it shouldn't match as $x starts with a slash and 
the RE doesn't - you're on the right path.  The reason is, the match 
goes anywhere, it is "unanchored" so Perl happily says, "walking" down 
$x saying:
"slash? nope. "p"? match! "a" match!! ... slash? yay! one or more ("+") 
word chars ("\w")? Aw, fail"


so, actually that RE fails:
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'
[crickets]
as there's not even 1 "\w"
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w}'


zero or more ("*") works
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w*}'
1

as does none
$ perl -le '$x="/path/"; print 1 if $x=~m{path/}'
1

and adding the initial "/" to the RE still works:
$ perl -le '$x="/path/"; print 1 if $x=~m{/path/}'
1

but if you'd anchored ("^" - zero-width "at the beginning of the string" 
must be at the begining of the RE) your RE would fail too:

$ perl -le '$x="/path/"; print 1 if $x=~m{^path/}'

because the RE starts w/ "p" and the $x starts with slash.


On Tue, Jul 17, 2018 at 6:56 AM, Lauren C. <mailto:lau...@miscnote.net>> wrote:


Hello,

I want to match:

/path/
/path/123
/path/abc

but /path/?xxx  should not be matched.

This works:

$ perl -le '$x="/path/abc"; print 1 if $x=~m{path/\w+}'
1


this works too:

$ perl -le '$x="/path/?abc"; print 1 if $x=~m{path/\w+}'


But it doesn't work for this case:

$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'

it expects 1 returned.

Can you help? thanks.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org

<mailto:beginners-unsubscr...@perl.org>
For additional commands, e-mail: beginners-h...@perl.org
<mailto:beginners-h...@perl.org>
http://learn.perl.org/





--

a

Andy Bach,
afb...@gmail.com <mailto:afb...@gmail.com>
608 658-1890 cell
608 261-5738 wk


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with another regex

2018-07-17 Thread Lauren C.

Thanks Gil. I think i know the difference of "\w+" and "\w*" now.


Gil Magno 写道:

2018-07-17 19:56:59 +0800 Lauren C.:

Hello,

I want to match:

/path/
/path/123
/path/abc

but /path/?xxx  should not be matched.

This works:

$ perl -le '$x="/path/abc"; print 1 if $x=~m{path/\w+}'
1


this works too:

$ perl -le '$x="/path/?abc"; print 1 if $x=~m{path/\w+}'


But it doesn't work for this case:

$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'


 From "perlintro":

 Quantifier * : zero or more of the previous thing.
 Quantifier + : one or more of the previous thing.

So "/path/" won't match m{path/\w+} because this regex wants "one or more \w" at that 
position, which the string doesn't have. If you use m{path/\w*} (note the asterisk) then you're saying 
"zero or more \w" at that position, and it'll match.


it expects 1 returned.

Can you help? thanks.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with another regex

2018-07-17 Thread Andy Bach
> But it doesn't work for this case:

$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'

> it expects 1 returned.

Well, assuming you mean it shouldn't match as $x starts with a slash and
the RE doesn't - you're on the right path.  The reason is, the match goes
anywhere, it is "unanchored" so Perl happily says, "walking" down $x saying:
"slash? nope. "p"? match! "a" match!! ... slash? yay! one or more ("+")
word chars ("\w")? Aw, fail"

so, actually that RE fails:
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'
[crickets]
as there's not even 1 "\w"
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w}'


zero or more ("*") works
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w*}'
1

as does none
$ perl -le '$x="/path/"; print 1 if $x=~m{path/}'
1

and adding the initial "/" to the RE still works:
$ perl -le '$x="/path/"; print 1 if $x=~m{/path/}'
1

but if you'd anchored ("^" - zero-width "at the beginning of the string"
must be at the begining of the RE) your RE would fail too:
$ perl -le '$x="/path/"; print 1 if $x=~m{^path/}'

because the RE starts w/ "p" and the $x starts with slash.


On Tue, Jul 17, 2018 at 6:56 AM, Lauren C.  wrote:

> Hello,
>
> I want to match:
>
> /path/
> /path/123
> /path/abc
>
> but /path/?xxx  should not be matched.
>
> This works:
>
> $ perl -le '$x="/path/abc"; print 1 if $x=~m{path/\w+}'
> 1
>
>
> this works too:
>
> $ perl -le '$x="/path/?abc"; print 1 if $x=~m{path/\w+}'
>
>
> But it doesn't work for this case:
>
> $ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'
>
> it expects 1 returned.
>
> Can you help? thanks.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: help with another regex

2018-07-17 Thread Gil Magno
2018-07-17 19:56:59 +0800 Lauren C.:
> Hello,
> 
> I want to match:
> 
> /path/
> /path/123
> /path/abc
> 
> but /path/?xxx  should not be matched.
> 
> This works:
> 
> $ perl -le '$x="/path/abc"; print 1 if $x=~m{path/\w+}'
> 1
> 
> 
> this works too:
> 
> $ perl -le '$x="/path/?abc"; print 1 if $x=~m{path/\w+}'
> 
> 
> But it doesn't work for this case:
> 
> $ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'

From "perlintro":

Quantifier * : zero or more of the previous thing.
Quantifier + : one or more of the previous thing.

So "/path/" won't match m{path/\w+} because this regex wants "one or more \w" 
at that position, which the string doesn't have. If you use m{path/\w*} (note 
the asterisk) then you're saying "zero or more \w" at that position, and it'll 
match.

> it expects 1 returned.
> 
> Can you help? thanks.
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 


signature.asc
Description: Digital signature


Re: help with another regex

2018-07-17 Thread Илья Рассадин

Hi!

I think, m{path/(\w+)?/?$} regex can solve your problem.

In general, to parse URL, you can use official regex from rfc3986 (see 
Appendix B for rfc3986)


regex is

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
$2 is protocol
$4 is host
$5 is path
$7 is query
$9 is fragment.

Another approach is to use some of cpan modules to parse URI.

For example, https://metacpan.org/pod/URI


On 7/17/18 2:56 PM, Lauren C. wrote:

Hello,

I want to match:

/path/
/path/123
/path/abc

but /path/?xxx  should not be matched.

This works:

$ perl -le '$x="/path/abc"; print 1 if $x=~m{path/\w+}'
1


this works too:

$ perl -le '$x="/path/?abc"; print 1 if $x=~m{path/\w+}'


But it doesn't work for this case:

$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'

it expects 1 returned.

Can you help? thanks.



help with another regex

2018-07-17 Thread Lauren C.

Hello,

I want to match:

/path/
/path/123
/path/abc

but /path/?xxx  should not be matched.

This works:

$ perl -le '$x="/path/abc"; print 1 if $x=~m{path/\w+}'
1


this works too:

$ perl -le '$x="/path/?abc"; print 1 if $x=~m{path/\w+}'


But it doesn't work for this case:

$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'

it expects 1 returned.

Can you help? thanks.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Uri Guttman

On 07/12/2018 11:40 PM, Lauren C. wrote:

Hi Uri,

I was reading this page:
https://www.rexegg.com/regex-lookarounds.html

the content of "Mastering Lookahead and Lookbehind" make me confused.

(?=foo)
(?<=foo)
(?!foo)
(?i suggest you don't study lookarounds until you are stronger with basic 
regex stuff. they are useful but not needed that often. you should start 
with simpler stuff like character classes and their shortcuts, grouping 
and grabbing and quantifiers (repeat counts). then move on to simple 
zero-width assertions and other stuff. after you are very comfortable 
with all that, there are plenty of deeper things to learn like 
lookaround. walk before you run! :)


the site you list above seems like it is well written but its ordering 
of lessons is way too fast and wrong IMO.


i highly recommend you read the official perl tutorial on regexes 
(mentioned by someone else earlier)


https://perldoc.perl.org/perlretut.html

it has the right pace and topic order to learn simpler and more common 
things first and builds on those. the site you found is more like a 
firehose and your asking about lookaround is why it isn't a good tutorial.


uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Lauren C.

Hi Uri,

I was reading this page:
https://www.rexegg.com/regex-lookarounds.html

the content of "Mastering Lookahead and Lookbehind" make me confused.

(?=foo)
(?<=foo)
(?!foo)
(?but seriously, regexes are a key feature in perl and most modern 
languages. it is hard to do any text or data processing without them. i 
recommend you read those tutorials mentioned earlier and possibly other 
materials. stay away from most 'perl' or 'regex' tutorials on the net as 
many are very poorly written and full of mistakes.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Uri Guttman

On 07/12/2018 08:53 PM, Lauren C. wrote:

OK I see, thanks Gil.
I think the main problem is I don't know much about regex.
I will re-learn them this day.

heh, relearning regexes will take a lifetime, not just one day! :)

but seriously, regexes are a key feature in perl and most modern 
languages. it is hard to do any text or data processing without them. i 
recommend you read those tutorials mentioned earlier and possibly other 
materials. stay away from most 'perl' or 'regex' tutorials on the net as 
many are very poorly written and full of mistakes.


and if you need more help with regexes, emailing here is a good thing!

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Lauren C.

Thanks John.

Those symbols made me crazy entirely.
As what you explained, some are metadata of regex, some are regular 
characters, it's not clear to me, due to my poor knowledge on regex.


Yes I will learn them more.

thanks.

On 2018/7/13 星期五 AM 2:23, John W. Krahn wrote:

On Thu, 2018-07-12 at 19:35 +0800, Lauren C. wrote:


My web is powered by Apache and PHP,its access log seems as blow,

xx.xx.xx.xx - - [12/Jul/2018:19:29:43 +0800] "GET
/2018/07/06/antique-internet/ HTTP/1.1" 200 5489 "https://miscnote.ne
t/"
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"

A perl script for stat purpose of this log:

tail -f /var/log/apache2/access.log|perl -nle 'next unless m{^(\S+) -
-
\[(\S+).*\] \"GET (.*?/)\s+}; printf "%-20s%-40s%-40s\n",$1,$3,$2'

I was totally confused about it.
   what does m{...} and its content stand for?




m{^

Start with the (^) beginning of line anchor, the following pattern must
match at the beginning of the line.

(\S+)

Match one or more non-whitespace characters and store the match in the
$1 variable.  This matches the "xx.xx.xx.xx" portion of your string.

' - - \['

Match the literal characters SPACE HYPHEN SPACE HYPHEN SPACE LEFT-
BRACKET.

(\S+)

Match one or more non-whitespace characters and store the match in the
$2 variable.  This matches the "12/Jul/2018:19:29:43" portion of your
string.

'.*\] \"GET'

Match zero or more non-newline characters followed by the literal
string '] "GET '.

(.*?/)

Match as few as possible non-newline characters followed by a '/'
character and store the match in the $3 variable.  This matches the
"/2018/07/06/antique-internet/" portion of your string.

\s+}

And finally, match one or more whitespace characters so that the
previous non-greedy pattern will match correctly.  The modifier is
redundant so it could simply be:

\s}



John



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Lauren C.

OK I see, thanks Gil.
I think the main problem is I don't know much about regex.
I will re-learn them this day.

On 2018/7/12 星期四 PM 10:02, Gil Magno wrote:

2018-07-12 20:50:22 +0800 Lauren C.:

thanks for the kind helps.
do you know what the expression in { } stands for?

^(\S+) - - \[(\S+).*\] \"GET (.*?/)\s+


Hi, Lauren

This is quickly explained in 
http://perldoc.perl.org/perlrequick.html#Using-character-classes

\s (lowercase) stands for a "whitespace". \S (uppercase) stands for the 
opposite of \s. So

$name = "lauren";
if ($name =~ m{\s}) { print 'it matched' }

This will not match, because there's no "whitespace" in the string. But this

$name = "lauren";
if ($name =~ m{\S}) { print 'it matched' }

will match, because in the string there is a character which is *not* 
"whitespace".

For the ^ [] and .*? in the regex, those pages I the previous email help you.

Best

gil


On 2018/7/12 星期四 PM 8:37, Илья Рассадин wrote:

"m{ pattern }" is regular expression to parse log string.

It's equal to just "/ pattern /". Using different delimiter is convenient
here because usually symbol "/" must be escaped with backslash "\", but if
we use another delimiter - we can left "/" symbol unescaped and reges is
more readable.

You can further explore regex with this site https://regex101.com/r/4CGCcB/2


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Lauren C.

Thanks Jim. that explains clearly.

On 2018/7/12 星期四 PM 10:00, Jim Gibson wrote:



On Jul 12, 2018, at 5:50 AM, Lauren C.  wrote:

thanks for the kind helps.
do you know what the expression in { } stands for?

^(\S+) - - \[(\S+).*\] \"GET (.*?/)\s+


Here is a breakdown:

^   Start looking for matches at beginning of string
(\S+)   Match a consecutive sequence of non-whitespace characters and save in 
the $1 variable
—   Match the literal string ‘ — ‘
\[  Match the character ‘[‘
(\S+)   Match a consecutive sequence of non-whitespace characters and save in 
the $2 variable
.*  Match any consecutive zero or more characters
\]  Match the character ‘]’
(space) Match a space character
\”  Match the character ‘“‘
GET Match the literal string ‘GET ‘ (with a space at the end)
(.*?/)  Match the shortest string of any consecutive characters up to but not 
including a following whitespace and save in $3
\s+ Match any consecutive sequence of whitespace characters

If all of the above entities are matched, then the regular expression 
evaluation returns true and the 41, $2, and $3 variables are assigned to their 
captured matches.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread John W. Krahn
On Thu, 2018-07-12 at 19:35 +0800, Lauren C. wrote:
> 
> My web is powered by Apache and PHP,its access log seems as blow,
> 
> xx.xx.xx.xx - - [12/Jul/2018:19:29:43 +0800] "GET 
> /2018/07/06/antique-internet/ HTTP/1.1" 200 5489 "https://miscnote.ne
> t/" 
> "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 
> (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
> 
> A perl script for stat purpose of this log:
> 
> tail -f /var/log/apache2/access.log|perl -nle 'next unless m{^(\S+) -
> - 
> \[(\S+).*\] \"GET (.*?/)\s+}; printf "%-20s%-40s%-40s\n",$1,$3,$2'
> 
> I was totally confused about it.
>   what does m{...} and its content stand for?



m{^

Start with the (^) beginning of line anchor, the following pattern must
match at the beginning of the line.

(\S+)

Match one or more non-whitespace characters and store the match in the
$1 variable.  This matches the "xx.xx.xx.xx" portion of your string.

' - - \['

Match the literal characters SPACE HYPHEN SPACE HYPHEN SPACE LEFT-
BRACKET.

(\S+)

Match one or more non-whitespace characters and store the match in the
$2 variable.  This matches the "12/Jul/2018:19:29:43" portion of your
string.

'.*\] \"GET '

Match zero or more non-newline characters followed by the literal
string '] "GET '.

(.*?/)

Match as few as possible non-newline characters followed by a '/'
character and store the match in the $3 variable.  This matches the
"/2018/07/06/antique-internet/" portion of your string.

\s+}

And finally, match one or more whitespace characters so that the
previous non-greedy pattern will match correctly.  The modifier is
redundant so it could simply be:

\s}



John

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Jim Gibson


> On Jul 12, 2018, at 5:50 AM, Lauren C.  wrote:
> 
> thanks for the kind helps.
> do you know what the expression in { } stands for?
> 
> ^(\S+) - - \[(\S+).*\] \"GET (.*?/)\s+

Here is a breakdown:

^   Start looking for matches at beginning of string
(\S+)   Match a consecutive sequence of non-whitespace characters and save in 
the $1 variable
—   Match the literal string ‘ — ‘
\[  Match the character ‘[‘
(\S+)   Match a consecutive sequence of non-whitespace characters and save in 
the $2 variable
.*  Match any consecutive zero or more characters
\]  Match the character ‘]’
(space) Match a space character
\”  Match the character ‘“‘
GET Match the literal string ‘GET ‘ (with a space at the end)
(.*?/)  Match the shortest string of any consecutive characters up to but not 
including a following whitespace and save in $3
\s+ Match any consecutive sequence of whitespace characters

If all of the above entities are matched, then the regular expression 
evaluation returns true and the 41, $2, and $3 variables are assigned to their 
captured matches.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Gil Magno
2018-07-12 20:50:22 +0800 Lauren C.:
> thanks for the kind helps.
> do you know what the expression in { } stands for?
> 
> ^(\S+) - - \[(\S+).*\] \"GET (.*?/)\s+

Hi, Lauren

This is quickly explained in 
http://perldoc.perl.org/perlrequick.html#Using-character-classes

\s (lowercase) stands for a "whitespace". \S (uppercase) stands for the 
opposite of \s. So

$name = "lauren";
if ($name =~ m{\s}) { print 'it matched' }

This will not match, because there's no "whitespace" in the string. But this

$name = "lauren";
if ($name =~ m{\S}) { print 'it matched' }

will match, because in the string there is a character which is *not* 
"whitespace".

For the ^ [] and .*? in the regex, those pages I the previous email help you.

Best

gil

> On 2018/7/12 星期四 PM 8:37, Илья Рассадин wrote:
> >"m{ pattern }" is regular expression to parse log string.
> >
> >It's equal to just "/ pattern /". Using different delimiter is convenient
> >here because usually symbol "/" must be escaped with backslash "\", but if
> >we use another delimiter - we can left "/" symbol unescaped and reges is
> >more readable.
> >
> >You can further explore regex with this site https://regex101.com/r/4CGCcB/2
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 


signature.asc
Description: Digital signature


Re: help with a stat script

2018-07-12 Thread Lauren C.

thanks for the kind helps.
do you know what the expression in { } stands for?

^(\S+) - - \[(\S+).*\] \"GET (.*?/)\s+



On 2018/7/12 星期四 PM 8:37, Илья Рассадин wrote:

"m{ pattern }" is regular expression to parse log string.

It's equal to just "/ pattern /". Using different delimiter is 
convenient here because usually symbol "/" must be escaped with 
backslash "\", but if we use another delimiter - we can left "/" symbol 
unescaped and reges is more readable.


You can further explore regex with this site https://regex101.com/r/4CGCcB/2


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Lauren C.

thanks Magno. i will check it.

On 2018/7/12 星期四 PM 8:13, Gil Magno wrote:

Hi, Lauren

The m{...} is a regular expression (regexp). If you not familiar with
regexps in Perl, I advise you to read these pages:

-http://perldoc.perl.org/perlintro.html#Regular-expressions
-http://perldoc.perl.org/perlrequick.html


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help with a stat script

2018-07-12 Thread Gil Magno
2018-07-12 19:35:14 +0800 Lauren C.:
> Hello,
> 
> My web is powered by Apache and PHP,its access log seems as blow,
> 
> xx.xx.xx.xx - - [12/Jul/2018:19:29:43 +0800] "GET
> /2018/07/06/antique-internet/ HTTP/1.1" 200 5489 "https://miscnote.net/";
> "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML,
> like Gecko) Chrome/67.0.3396.99 Safari/537.36"
> 
> A perl script for stat purpose of this log:
> 
> tail -f /var/log/apache2/access.log|perl -nle 'next unless m{^(\S+) - -
> \[(\S+).*\] \"GET (.*?/)\s+}; printf "%-20s%-40s%-40s\n",$1,$3,$2'
> 
> I was totally confused about it.
>  what does m{...} and its content stand for?
> Can you help give a explain?

Hi, Lauren

The m{...} is a regular expression (regexp). If you not familiar with
regexps in Perl, I advise you to read these pages:

- http://perldoc.perl.org/perlintro.html#Regular-expressions
- http://perldoc.perl.org/perlrequick.html

> thanks in advance.
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 


signature.asc
Description: Digital signature


Re: help with a stat script

2018-07-12 Thread Илья Рассадин

Hi!

"m{ pattern }" is regular expression to parse log string.

It's equal to just "/ pattern /". Using different delimiter is 
convenient here because usually symbol "/" must be escaped with 
backslash "\", but if we use another delimiter - we can left "/" symbol 
unescaped and reges is more readable.


You can further explore regex with this site https://regex101.com/r/4CGCcB/2


On 7/12/18 2:35 PM, Lauren C. wrote:

Hello,

My web is powered by Apache and PHP,its access log seems as blow,

xx.xx.xx.xx - - [12/Jul/2018:19:29:43 +0800] "GET 
/2018/07/06/antique-internet/ HTTP/1.1" 200 5489 
"https://miscnote.net/"; "Mozilla/5.0 (Macintosh; Intel Mac OS X 
10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 
Safari/537.36"


A perl script for stat purpose of this log:

tail -f /var/log/apache2/access.log|perl -nle 'next unless m{^(\S+) - 
- \[(\S+).*\] \"GET (.*?/)\s+}; printf "%-20s%-40s%-40s\n",$1,$3,$2'


I was totally confused about it.
 what does m{...} and its content stand for?
Can you help give a explain?

thanks in advance.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




help with a stat script

2018-07-12 Thread Lauren C.

Hello,

My web is powered by Apache and PHP,its access log seems as blow,

xx.xx.xx.xx - - [12/Jul/2018:19:29:43 +0800] "GET 
/2018/07/06/antique-internet/ HTTP/1.1" 200 5489 "https://miscnote.net/"; 
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"


A perl script for stat purpose of this log:

tail -f /var/log/apache2/access.log|perl -nle 'next unless m{^(\S+) - - 
\[(\S+).*\] \"GET (.*?/)\s+}; printf "%-20s%-40s%-40s\n",$1,$3,$2'


I was totally confused about it.
 what does m{...} and its content stand for?
Can you help give a explain?

thanks in advance.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help debugging some old Bugzilla perl scripts

2018-02-26 Thread jose cabrera

On Monday, February 26, 2018 at 4:50 AM, Shlomi Fish" wrote...

> Hi jose,
> 
> please see http://www.shlomifish.org/Files/files/code/bugzilla.patch for my
> patch against bugzilla 3.2 to get it to compile with recent perls.

Thanks.

josé 


--
What if eternity is real? Where will you spend it? Hmmm...

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help debugging some old Bugzilla perl scripts

2018-02-26 Thread Shlomi Fish
Hi jose,

please see http://www.shlomifish.org/Files/files/code/bugzilla.patch for my
patch against bugzilla 3.2 to get it to compile with recent perls.

 On Mon, 26 Feb 2018 05:43:45 +0100
"jose cabrera"  wrote:

> On Sunday, February 25, 2018 at 11:26 PM, "Uri Guttman" wrote...
> > >
> > > 
> > > syntax error at Bugzilla/Bug.pm line 1085, near "$type qw(dependson
> > > blocked)"
> > >
> > > Here is line 1085:
> > >  foreach my $type qw(dependson blocked) {
> > >  my @bug_ids = split(/[\s,]+/, $deps_in{$type});  
> > 
> > put parens around the qw(). it used to be allowed as the () in the for 
> > loop but not anymore.  
> 
> Thanks.
> 
> > >
> > > 
> > > Can't use global @_ in "my" at Bugzilla/Bug.pm line 1134, near "= @_"
> > >
> > > Here is line 1133, 1134, 1135:
> > > sub _check_dup_id {
> > >  my ($self, $dupe_of) = @_;
> > >  my $dbh = Bugzilla->dbh;
> > >  
> 
> Yeah, I could not see anything wrong with it, but what I did was to install
> perlbrew and I am downloading 5.8.9, which should be the version that was
> released with Bugzilla 3.2.  I am going to see if this new change fixes all
> of these errors. ;-)  Thanks for the support.
> 
> josé
> 
> --
> What if eternity is real? Where will you spend it? Hmmm...
> 

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help debugging some old Bugzilla perl scripts

2018-02-25 Thread Uri Guttman

On 02/25/2018 11:51 PM, jose cabrera wrote:

On Sunday, February 25, 2018 at 11:48 PM, "Uri Guttman" wrote...

Here is line 1085:
   foreach my $type qw(dependson blocked) {
   my @bug_ids = split(/[\s,]+/, $deps_in{$type});

put parens around the qw(). it used to be allowed as the () in the for
loop but not anymore.

Thanks.

i actually ran into a bug with a change in qw() that may be the cause of
this. it used to be that qw was a runtime thing and was compiled into a
split that returns scalars which were read/write. a perl version changed
qw to be a compile time thing that made a list of read only strings. the
code i was working with assumed the results of the qw were read/write
and crashed when the new version of perl came out. this may also have
been the change you see there. but maybe not. yours is a syntax change
that allowed qw() to be the parens part of for.

Don't forget this is 2007 perl code.  So...


the bug i found and fixed was from migrating from 5.005 to 5.008 iirc. 
so it was a long time ago too.


thanx,

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help debugging some old Bugzilla perl scripts

2018-02-25 Thread jose cabrera
On Sunday, February 25, 2018 at 11:48 PM, "Uri Guttman" wrote...
> >>> Here is line 1085:
> >>>   foreach my $type qw(dependson blocked) {
> >>>   my @bug_ids = split(/[\s,]+/, $deps_in{$type});
> >> put parens around the qw(). it used to be allowed as the () in the for
> >> loop but not anymore.
> > Thanks.
> i actually ran into a bug with a change in qw() that may be the cause of 
> this. it used to be that qw was a runtime thing and was compiled into a 
> split that returns scalars which were read/write. a perl version changed 
> qw to be a compile time thing that made a list of read only strings. the 
> code i was working with assumed the results of the qw were read/write 
> and crashed when the new version of perl came out. this may also have 
> been the change you see there. but maybe not. yours is a syntax change 
> that allowed qw() to be the parens part of for.

Don't forget this is 2007 perl code.  So...

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help debugging some old Bugzilla perl scripts

2018-02-25 Thread Uri Guttman

On 02/25/2018 11:43 PM, jose cabrera wrote:

On Sunday, February 25, 2018 at 11:26 PM, "Uri Guttman" wrote...


syntax error at Bugzilla/Bug.pm line 1085, near "$type qw(dependson blocked)"

Here is line 1085:
  foreach my $type qw(dependson blocked) {
  my @bug_ids = split(/[\s,]+/, $deps_in{$type});

put parens around the qw(). it used to be allowed as the () in the for
loop but not anymore.

Thanks.
i actually ran into a bug with a change in qw() that may be the cause of 
this. it used to be that qw was a runtime thing and was compiled into a 
split that returns scalars which were read/write. a perl version changed 
qw to be a compile time thing that made a list of read only strings. the 
code i was working with assumed the results of the qw were read/write 
and crashed when the new version of perl came out. this may also have 
been the change you see there. but maybe not. yours is a syntax change 
that allowed qw() to be the parens part of for.


thanx,

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help debugging some old Bugzilla perl scripts

2018-02-25 Thread jose cabrera

On Sunday, February 25, 2018 at 11:26 PM, "Uri Guttman" wrote...
> >
> > 
> > syntax error at Bugzilla/Bug.pm line 1085, near "$type qw(dependson 
> > blocked)"
> >
> > Here is line 1085:
> >  foreach my $type qw(dependson blocked) {
> >  my @bug_ids = split(/[\s,]+/, $deps_in{$type});
> 
> put parens around the qw(). it used to be allowed as the () in the for 
> loop but not anymore.

Thanks.

> >
> > 
> > Can't use global @_ in "my" at Bugzilla/Bug.pm line 1134, near "= @_"
> >
> > Here is line 1133, 1134, 1135:
> > sub _check_dup_id {
> >  my ($self, $dupe_of) = @_;
> >  my $dbh = Bugzilla->dbh;
> >

Yeah, I could not see anything wrong with it, but what I did was to install 
perlbrew and I am downloading 5.8.9, which should be the version that was 
released with Bugzilla 3.2.  I am going to see if this new change fixes all of 
these errors. ;-)  Thanks for the support.

josé

--
What if eternity is real? Where will you spend it? Hmmm...

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help debugging some old Bugzilla perl scripts

2018-02-25 Thread Uri Guttman

On 02/25/2018 10:47 PM, jose cabrera wrote:

Greetings!

Long story, I had to install Bugzilla v3.2, which was in a WinNT 4.  I have now 
installed Bugzilla v3.2, in the Ubuntu 14.04 server, but I have perl (v5.22.1) 
and I am getting lots of errors.  Once I learn to fix one of these, I can work 
with the others.


syntax error at Bugzilla/Bug.pm line 1085, near "$type qw(dependson blocked)"

Here is line 1085:
 foreach my $type qw(dependson blocked) {
 my @bug_ids = split(/[\s,]+/, $deps_in{$type});


put parens around the qw(). it used to be allowed as the () in the for 
loop but not anymore.



Can't use global @_ in "my" at Bugzilla/Bug.pm line 1134, near "= @_"

Here is line 1133, 1134, 1135:
sub _check_dup_id {
 my ($self, $dupe_of) = @_;
 my $dbh = Bugzilla->dbh;

i don't see any obvious errors there. that implies it might be something 
before this. saying it is global makes me think the sub { is not opening 
a new sub so earlier may be something wrong. post a larger snippet and 
that may help.


thanx,

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Help debugging some old Bugzilla perl scripts

2018-02-25 Thread jose cabrera

Greetings!

Long story, I had to install Bugzilla v3.2, which was in a WinNT 4.  I have now 
installed Bugzilla v3.2, in the Ubuntu 14.04 server, but I have perl (v5.22.1) 
and I am getting lots of errors.  Once I learn to fix one of these, I can work 
with the others.


syntax error at Bugzilla/Bug.pm line 1085, near "$type qw(dependson blocked)"

Here is line 1085:
foreach my $type qw(dependson blocked) {
my @bug_ids = split(/[\s,]+/, $deps_in{$type});


Can't use global @_ in "my" at Bugzilla/Bug.pm line 1134, near "= @_"

Here is line 1133, 1134, 1135:
sub _check_dup_id {
my ($self, $dupe_of) = @_;
my $dbh = Bugzilla->dbh;

How can I fix these?  Thanks.

josé

 

--
What if eternity is real? Where will you spend it? Hmmm...

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: help check a piece of code

2017-04-10 Thread Andy Bach
On Sat, Apr 1, 2017 at 4:24 AM, K. Peng  wrote:

> I don't know why the "ref" and "scalar" functions here can be used to
> validate if it's ASCII or UTF8 encoding.
>
>
> use constant ASCII => ref eval {
> require Encode;
> Encode::find_encoding('ascii');
> };
>
> use constant UTF8 => scalar eval {
> Encode::encode_utf8( chr(182) ) eq pack( 'H*', 'C2B6' );
> };
>

Not entirely clear but the constant ASCII will have a value of "Encode::XS"
if 'ascii' is found as a valid encoding and UTF8 will have the value "1" if
the utf8 encoding of "chr 182" matches the "Hex string (high nibble first)
of "C2B6" - appears to be the paragraph char.

use constant ASCII => ref eval {
require Encode;
Encode::find_encoding('ascii');
};
print "r: ", eval { Encode::find_encoding('ascii');
}, "\n";
print "r: ", ref eval { Encode::find_encoding('ascii');
}, "\n";
print "r: ", ref  Encode::find_encoding('ascii') , "\n";
print "r: ", ref  Encode::find_encoding('himom') , "\n";

use constant UTF8 => scalar eval {
Encode::encode_utf8( chr(182) ) eq pack( 'H*', 'C2B6' );
};
print "s: ", eval { Encode::encode_utf8( chr(182) ) eq pack( 'H*',
'C2B6' );
}, "\n";
print "s: ", scalar eval { Encode::encode_utf8( chr(182) ) eq pack(
'H*', 'C2B6' );
}, "\n";
print "s: ",  Encode::encode_utf8( chr(182) ) eq pack( 'H*', 'C2B6'
) , "\n";
printf("c: %c %s\n",  chr(182),  pack( 'H*', 'C2B6' ) );

$ perl /tmp/r.pl
r: Encode::XS=SCALAR(0xb39ff0)
r: Encode::XS
r: Encode::XS
r:
s: 1
s: 1
s: 1
c:  ¶



-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


help check a piece of code

2017-04-01 Thread K. Peng

can you help explain the code below?
I don't know why the "ref" and "scalar" functions here can be used to 
validate if it's ASCII or UTF8 encoding.



use constant ASCII => ref eval {
require Encode;
Encode::find_encoding('ascii'); 
};

use constant UTF8 => scalar eval {
Encode::encode_utf8( chr(182) ) eq pack( 'H*', 'C2B6' );
};


Thanks.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: wickedcoolperlscripts help

2016-09-19 Thread Uri Guttman

On 09/19/2016 09:32 PM, derr...@thecopes.me wrote:

I am working on the book Wicked Cool Perl Scripts as a learning tool to hely me 
get from beginner to intermmediate perl. The second script of the book is 
giving me lots of
trouble. Notably the part below. I realize he creating a hash and is using the 
$File::Find::name object which may be a bit over my head. Can someone explain 
to me what he is doing?

my %files;  # Files indexed by size

 # Go through the file tree and find all
 # files with a similar size
 find( sub
 {
-f &&
File::Find sets $_ to each file it finds before it calls this sub. so -f 
is acting on $_ and just checks for a regular file. if so, it then does 
the next thing after the &&.


i would code it differently. i would do:

return unless -f ;

that is less confusing about the early exit than the && op IMNSHO.
  
	push @{$files{(stat(_))[7]}}, $File::Find::name
that can be broken down easily. first, $File::Find::name is not an 
object but just a fully qualified scalar which is set to the full file 
name of the current file being looked at (the short name is in $_ and 
the current dir is also changed to the path part stored in 
$File::Find::name. so that is what is being pushed into the $files hash 
(actually an array ref in that hash). the key is the [7] (8th) element 
in the result of the stat() call which is the size of the file. the file 
arg there is _ which is a shortcut to the last used file in any file 
operator which was the aforementioned -f operator. the -s file op would 
do the same and be MUCH clearer. so we now have the file size as a key. 
the outer wrapping of @{} around that hash entry says this is an array 
reference but perl will autovivify it since the first time through the 
entry for this file size will be undef. the next time through there will 
an an array ref in there. so if you have more then one file of the same 
size, there will be an array ref holding all those file paths keyed by 
that size. works for one file of a given size too.


this is that line redone to make it clearer:

push @{ $files{ -s _ } }, $File::Find::name ;

white space is your friend!

$File::Find::name


}, @dir_list
 );

BTW, am I sending this email to the correct place?

yes.

thanx,

uri


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




wickedcoolperlscripts help

2016-09-19 Thread derrick
I am working on the book Wicked Cool Perl Scripts as a learning tool to hely me 
get from beginner to intermmediate perl. The second script of the book is 
giving me lots of
trouble. Notably the part below. I realize he creating a hash and is using the 
$File::Find::name object which may be a bit over my head. Can someone explain 
to me what he is doing?

my %files;  # Files indexed by size 

# Go through the file tree and find all 
# files with a similar size
find( sub 
{
-f &&  
push @{$files{(stat(_))[7]}}, $File::Find::name
}, @dir_list
);

BTW, am I sending this email to the correct place?
-- 
Derrick Cope

--from my mutt

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Wanted: Help performing Date-Time Arithmetic using emacs/vi and perl

2016-04-19 Thread Shlomi Fish
Hi Richard,

On Tue, 19 Apr 2016 04:58:32 + (UTC)
Richard Heintze via beginners  wrote:

>   I just love using viper mode in emacs to execute perl:
> .! perl -MPOSIX -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )*
> \) }x;$funpat = qr/$np/;} s/($funpat)=(.*)$/"$1=$3".eval($1)."$4"/ge'
> 
> This command searches for a balanced set of parens (from the camel book)
> followed by an equal sign followed by any trailing garbage and evaluates the
> expression inside the balanced parans. so if you type (4*atan(1))=gobblygook
> and then execute the above VI/Ex command you get:
> 4*atan(1))=3.14159265358979
> 
> 
> Here is my failed attempt to do date arithmetic:
> (use Date::Calc ( ":all" ); use Date::Manip; my ( $date, $yy, $dd, $mm );
> $date = scalar localtime( ( time() - ( 24 * 60 * 60 ) ) ); $date)="" Can
> someone help me figure out how to use the eval function in my perl one-liner
> to evaluate date time arithmetic? Thanks,Siegfried
> 

This is the Perl beginners mailing list and we aim to help beginners with
beginners question while encouraging best practices and code readability (see
http://perl-begin.org/tutorials/bad-elements/ for instance ). Your code is
not too readable and violates many best practices, so I'd rather not help you
with it, to avoid encouraging that.

Please write a better Perl program, in a separate file, with newlines and "use
strict;"/"use warnings;"/etc. and other best practices, (see the link) and you
can run it using :! perl ~/bin/my_filter .

I also don't understand which issue you're having with the date arithmetic.

Regards,

Shlomi Fish

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Wanted: Help performing Date-Time Arithmetic using emacs/vi and perl

2016-04-18 Thread Richard Heintze via beginners
  I just love using viper mode in emacs to execute perl:
.! perl -MPOSIX -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* \) 
}x;$funpat = qr/$np/;} s/($funpat)=(.*)$/"$1=$3".eval($1)."$4"/ge'

This command searches for a balanced set of parens (from the camel book) 
followed by an equal sign followed by any trailing garbage and evaluates the 
expression inside the balanced parans.
so if you type
(4*atan(1))=gobblygook
and then execute the above VI/Ex command you get:
4*atan(1))=3.14159265358979


Here is my failed attempt to do date arithmetic:
(use Date::Calc ( ":all" ); use Date::Manip; my ( $date, $yy, $dd, $mm ); $date 
= scalar localtime( ( time() - ( 24 * 60 * 60 ) ) ); $date)=""
Can someone help me figure out how to use the eval function in my perl 
one-liner to evaluate date time arithmetic?
Thanks,Siegfried



Re: coding help

2016-03-01 Thread Shawn H Corey
On Tue, 1 Mar 2016 12:05:14 +0530
Arghya Das  wrote:

> $num = 1234;
> $sum = 0;
> 
> while ( $num != 0)
> {
> $rem = $num % 10;
> $sum = ( $sum * 10 ) + $rem;
> $num = $num / 10;

$num = int( $num / 10 );

> }
> 
> print $sum;
> 
> 
> 
> please tell what is wrong with the reverse number code .
> It prints Infinity.

See `perldoc -f int` for details.
http://perldoc.perl.org/functions/int.html


-- 
Don't stop where the ink does.
Shawn

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: coding help

2016-02-29 Thread Kent Fredric
On 1 March 2016 at 19:35, Arghya Das  wrote:
> $num = 1234;
> $sum = 0;
>
> while ( $num != 0)
> {
> $rem = $num % 10;
> $sum = ( $sum * 10 ) + $rem;
> $num = $num / 10;
> }
>
> print $sum;

I suspect you're just exceeding precision limits.

Modified code:

use strict;
use warnings;

my $num = 1234;
my $sum = 0;

while ( $num != 0) {

  my $rem = $num % 10;

  printf "* rem: %s, sum: %s, num: %s\n", $rem, $sum, $num;
  $sum = ( $sum * 10 ) + $rem;
  $num = $num / 10;
}

print $sum;

Output:


* rem: 4, sum: 0, num: 1234
* rem: 3, sum: 4, num: 123.4
* rem: 2, sum: 43, num: 12.34
* rem: 1, sum: 432, num: 1.234
* rem: 0, sum: 4321, num: 0.1234
* rem: 0, sum: 43210, num: 0.01234
* rem: 0, sum: 432100, num: 0.001234
* rem: 0, sum: 4321000, num: 0.0001234
* rem: 0, sum: 4321, num: 1.234e-05
* rem: 0, sum: 43210, num: 1.234e-06
* rem: 0, sum: 432100, num: 1.234e-07
* rem: 0, sum: 4321000, num: 1.234e-08
* rem: 0, sum: 4321, num: 1.234e-09
* rem: 0, sum: 43210, num: 1.234e-10
* rem: 0, sum: 432100, num: 1.234e-11
* rem: 0, sum: 4321000, num: 1.234e-12
* rem: 0, sum: 4321, num: 1.234e-13
* rem: 0, sum: 43210, num: 1.234e-14
* rem: 0, sum: 432100, num: 1.234e-15
* rem: 0, sum: 4321000, num: 1.234e-16

...


* rem: 0, sum: 4.321e+306, num: 1.234e-304
* rem: 0, sum: 4.321e+307, num: 1.234e-305
* rem: 0, sum: Inf, num: 1.234e-306
* rem: 0, sum: Inf, num: 1.234e-307
* rem: 0, sum: Inf, num: 1.234e-308
* rem: 0, sum: Inf, num: 1.234e-309
* rem: 0, sum: Inf, num: 1.233999e-310
* rem: 0, sum: Inf, num: 1.233989e-311
* rem: 0, sum: Inf, num: 1.233891e-312
* rem: 0, sum: Inf, num: 1.233397e-313
* rem: 0, sum: Inf, num: 1.2340009278e-314
* rem: 0, sum: Inf, num: 1.2340206904e-315
* rem: 0, sum: Inf, num: 1.2339712839e-316
* rem: 0, sum: Inf, num: 1.2349594151e-317
* rem: 0, sum: Inf, num: 1.23399811967893e-318
* rem: 0, sum: Inf, num: 1.2339783570531e-319
* rem: 0, sum: Inf, num: 1.23417598331143e-320
* rem: 0, sum: Inf, num: 1.23516411460312e-321
* rem: 0, sum: Inf, num: 1.23516411460312e-322
* rem: 0, sum: Inf, num: 9.88131291682493e-324


If you modify the code to use arbitrary precision floating point, it
just runs forever.


So you possibly have a logic error. Though it depends entirely on what
you're trying to achieve.

If I had to guess, I'd say you expected " $num / 10 " to return an
integer, not a floating point value.

changing the last line of the "while" to read:

 $num = int ( $num / 10 );


Results in the following:
* rem: 4, sum: 0, num: 1234
* rem: 3, sum: 4, num: 123
* rem: 2, sum: 43, num: 12
* rem: 1, sum: 432, num: 1
4321



-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




coding help

2016-02-29 Thread Arghya Das
$num = 1234;
$sum = 0;

while ( $num != 0)
{
$rem = $num % 10;
$sum = ( $sum * 10 ) + $rem;
$num = $num / 10;
}

print $sum;



please tell what is wrong with the reverse number code .
It prints Infinity.


Re: Counter Help

2016-02-12 Thread Kent Fredric
On 13 February 2016 at 10:08, Kent Fredric  wrote:
>
> All you're doing is sorting the *view* of it. Not the data itself.


If you want a demonstration of this fact, on a Linux filesystem, poke
around with 'find'. Or if you've got Path::Iterator::Rule installed:

 perl -MPIR -E' $it = PIR->new->iter_fast(q(.)); while( my $entry =
$it->() ) { say $entry }'

You'll notice that the output frequently /appears/ partly sorted.

But often doesn't seem sorted at all.

tools like 'ls' make this simple for you because they automatically
sort things to display it, but the folder itself is typically not
"sorted", all you've sorted is "a list of files that you retrieved
from a folder"

Similarly, you can "sort" the list of "keys" that come out of a hash,
but you can't sort the hash itself.

my @keys = sort keys %hash;

( Hashes and Filesystems themselves, however, are stored unsorted,
because it is typically faster for the operating system to do it that
way, and then only sort it for presentation purposes when strictly
necessary , because sorting is expensive, so it delays doing it at all
until the very last possible oppotunity )

-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-12 Thread Kent Fredric
On 13 February 2016 at 08:38, timothy adigun <2teezp...@gmail.com> wrote:
> In your analogy, if hashes are like folder,  keys and values are like what?
> Name of folders. If yes,  can those be sorted?  If yes,  they you have just
> made my point..  :)


Keys are files.

Values are file contents.

But no, the folders themselves cannot be "Sorted", sorting modifies
the *property* of the thing.

What you are doing, is sorting how it is *displayed*

If you used low-level mechanics to look at how the files are laid out
on the filesystem, the files in those folders wouldn't necessarily
even be stored near each other. "Soriting" them as such would be an
expensive mechanism moving stuff all over your hard drive.

All you're doing is sorting the *view* of it. Not the data itself.

-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Counter Help

2016-02-12 Thread uri
On Fri, February 12, 2016 12:37 pm, Christin Deville wrote:
> I have been lurking for a while but I want to chime in and say thanks for
> that piece of advice. I've been trying to sort out in my head when to use
> a hash or an array and this all helps!
>
>

in real world code you should be using hashes way more often than arrays.
arrays map to stacks, lists, fifo, etc. which is just a few concepts.
hashes map to many more concepts: data structures, sets, tables,
dispatching, lookup by name, mapping from one domain to another, inclusion
tests, etc. it is almost never a choice of which to use. it should be
plain to see when a hash or array is needed. study some good cpan code and
you will see that hashes are much more common than arrays in perl.

some notes to keep around:

arrays are indexed by integers
hashes are indexed by strings (keys)

arrays are ordered (by their integer indexes)
hashes are unordered (keys are not presorted)

arrays can be added to at their beginning (unshift) or end (push) or
inside (splice). those are all functions. you can delete from the
beginning with shift or the end with pop. deleting from the middle with
splice().
hashes can be added to by assignment, and keys can be deleted by delete().

you loop over arrays with foreach and get their values
you loop over hash keys with foreach/keys() (only get keys), while/each()
(can get keys and values).

arrays and hashes have very similar syntax. the sigils are @ vs % and the
indexing delimiters are [] vs {} (same for anon refs to them).

uri





-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-12 Thread timothy adigun
On Feb 12, 2016 8:28 PM, "Kent Fredric"  wrote:
>
> On 13 February 2016 at 07:39, timothy adigun <2teezp...@gmail.com> wrote:
> > And hashes keys/values can't be sorted? Just saying.. :)
>
>
> In my other message where I give an analogy to a "Folder" or
> "Directory" in a file system.
In your analogy, if hashes are like folder,  keys and values are like
what?  Name of folders. If yes,  can those be sorted?  If yes,  they you
have just made my point..  :)
>
> Can you sort a folder? ... not really. They don't really have an
> "order", you can traverse them in an order, but there's lots of
> different ways of doing that.
>
> The folder itself is, under the hood, sorted somewhat randomly most of
> the time, and your user interface might just sort them before you get
> what comes out of it ;).
>
> You can demonstrate this by asking somebody to change the order of 2
> specific files in a folder without changing the name, and then watch
> as they fail :P
>
>
>
> --
> Kent
>
> KENTNL - https://metacpan.org/author/KENTNL


Re: Counter Help

2016-02-12 Thread Kent Fredric
On 13 February 2016 at 07:39, timothy adigun <2teezp...@gmail.com> wrote:
> And hashes keys/values can't be sorted? Just saying.. :)


In my other message where I give an analogy to a "Folder" or
"Directory" in a file system.

Can you sort a folder? ... not really. They don't really have an
"order", you can traverse them in an order, but there's lots of
different ways of doing that.

The folder itself is, under the hood, sorted somewhat randomly most of
the time, and your user interface might just sort them before you get
what comes out of it ;).

You can demonstrate this by asking somebody to change the order of 2
specific files in a folder without changing the name, and then watch
as they fail :P



-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-12 Thread Kent Fredric
On 10 February 2016 at 03:46, James Kerwin  wrote:
> (I'm a bit wary of hashes because they're weird).


If you want a nice way to reason about hashes when you're really new,
there's something that you probably already understand you can borrow
understanding from:

Folders.

A hash is like a folder.

And every key is like a file.

And you can trivially demonstrate this with this code that treats a
folder as a storage mechanism.

For every item you see:

1. Look in the folder for a file with the same name.
2. Does it exist? No, then its zero
yes? Then read what number it currently has in it
3. Add one to that number
4. write that file out again.
5. Append that number to the item.

https://gist.github.com/kentfredric/8b90c88e20c7af9b1252


Now, this has the obvious limitation that this data structure persists
its data between runs, so every time you run the script, the numbers
will get bigger, and you'll have to nuke that script between runs.

But that's basically what the hash is doing for you.

Its a collection ( Folder / Hash ), ordered by keys ( like filenames
), that contain data ( like file contents ).

The only distinction you need to immediately worry about is that
Hashes happen entirely in memory, and they evaporate
as soon as your program stops.

I wouldn't recommend using the file technique for any real purpose,
but its interesting from an education perspective.


-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-12 Thread timothy adigun
On Feb 12, 2016 6:22 PM, "Shawn H Corey"  wrote:
>
> On Fri, 12 Feb 2016 12:08:07 -0500
> Uri Guttman  wrote:
>
> > hashes are very easy to learn. and once you get the hang of them you
> > will wonder why you waited so long.
>
> If keeping the data ordering is important,
And hashes keys/values can't be sorted? Just saying.. :)
>use an array. Otherwise, use
> a hash. :)
>
>
> --
> Don't stop where the ink does.
> Shawn
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>


RE: Counter Help

2016-02-12 Thread Christin Deville
I have been lurking for a while but I want to chime in and say thanks for that 
piece of advice. I've been trying to sort out in my head when to use a hash or 
an array and this all helps!

-Christin

-Original Message-
From: Shawn H Corey [mailto:shawnhco...@gmail.com] 
Sent: Friday, February 12, 2016 10:21 AM
To: beginners@perl.org
Subject: Re: Counter Help

On Fri, 12 Feb 2016 12:08:07 -0500
Uri Guttman  wrote:

> hashes are very easy to learn. and once you get the hang of them you 
> will wonder why you waited so long.

If keeping the data ordering is important, use an array. Otherwise, use a hash. 
:)


--
Don't stop where the ink does.
Shawn

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, 
e-mail: beginners-h...@perl.org http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-12 Thread Uri Guttman

On 02/12/2016 04:33 AM, James Kerwin wrote:

Thank you all for your help; all suggestions were welcome and helpful.

I didn't give the full details but Jim's solution did what I wanted 
the best and after reading around I think I get it. I've sat here 
trying to "break" it for the past half an hour and so far so good.


I solemnly swear to properly learn hashes and use them (and to stop 
thinking of them as "weird").




if you are not using hashes and regexes, you are not coding in perl. :)

hashes are very easy to learn. and once you get the hang of them you 
will wonder why you waited so long.


uri





--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-12 Thread Shawn H Corey
On Fri, 12 Feb 2016 12:08:07 -0500
Uri Guttman  wrote:

> hashes are very easy to learn. and once you get the hang of them you 
> will wonder why you waited so long.

If keeping the data ordering is important, use an array. Otherwise, use
a hash. :)


-- 
Don't stop where the ink does.
Shawn

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-12 Thread James Kerwin
Thank you all for your help; all suggestions were welcome and helpful.

I didn't give the full details but Jim's solution did what I wanted the
best and after reading around I think I get it. I've sat here trying to
"break" it for the past half an hour and so far so good.

I solemnly swear to properly learn hashes and use them (and to stop
thinking of them as "weird").

Again, thank you for the help! It's very appreciated.

James.

On Tue, Feb 9, 2016 at 6:37 PM, Jim Gibson  wrote:

>
> > On Feb 9, 2016, at 6:46 AM, James Kerwin  wrote:
> >
> > Thank you both very much for your help. I'll investigate this way when I
> get home later (I'm a bit wary of hashes because they're weird).
>
> Here is a solution using a hash:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @array = qw{
>11_
>22_
>22_
>33_
>33_
>33_
>44_
>44_
>55_
> };
>
> my %count;
> for my $element ( @array ) {
>   $element .= ++$count{$element};
> }
>
> print "Array:\n\n  ". join("\n  ",@array) . "\n”;
>
> __END__
>
> Note that the array needn’t be sorted for this method to work.
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Counter Help

2016-02-09 Thread Jim Gibson

> On Feb 9, 2016, at 6:46 AM, James Kerwin  wrote:
> 
> Thank you both very much for your help. I'll investigate this way when I get 
> home later (I'm a bit wary of hashes because they're weird).

Here is a solution using a hash:

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

my @array = qw{
   11_
   22_
   22_
   33_
   33_
   33_
   44_
   44_
   55_
};

my %count;
for my $element ( @array ) {
  $element .= ++$count{$element};
}

print "Array:\n\n  ". join("\n  ",@array) . "\n”;

__END__

Note that the array needn’t be sorted for this method to work.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-09 Thread Shlomi Fish
Hi Duncan and James,

On Tue, 9 Feb 2016 14:58:05 +
Duncan Ferguson  wrote:

> I disagree – hashes are not weird – they are incredibly useful.  It is just
> an array indexed by a word instead of a number  ☺
> 

I agree with Duncan here. Hashes are an integral part of idiomatic Perl and one
should learn how to use them properly and use them often - when appropriate.
For some resources for learning about hashes see:

* http://perl-begin.org/topics/hashes/

Otherwise, James’s code suffers from many bad idioms. For how to improve it,
see:

* http://perl-begin.org/tutorials/bad-elements/

(Note: perl-begin.org is a web site that I maintain.)

Below is one comment on Duncan's code:

> 
> 
> Here is some working code that may help
> 
> ===
> 
> #!/usr/bin/perl
> 
> use strict;
> 
> use warnings;
> 
> 
> 
> my @array = ( qw/ 11_ 22_ 33_ 33_ 33_ 44_ 44_ 55_ / );
> 
> 
> 
> my %results;
> 
> 
> 
> # create a hash of arrays
> 
> for my $item (@array) {
> 
> push(@{ $results{$item} }, $item);
> 
> }
> 
> 
> 
> # For each entry in the hash
> 
> for my $key (sort(keys(%results))) {
> 
># make a local array back out of what is stored in the hash
> 
> my @arr = @{ $results{$key}};
> 
> print "key=$key:", $/;
> 
> # print out the indexes of items in the local array
> 
> print "  $key$_",$/ foreach (0 .. $#arr)
> 

Since you're only using key and the length of the $results{$key} array
reference, you might as well just store a count which you can increment using
either of the ++ operators. Moreover, do you wish to preserve the order of the
occurrences?

Regards,

Shlomi Fish

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-09 Thread Nathan Hilterbrand



On 02/09/2016 09:08 AM, James Kerwin wrote:

Afternoon all,

I have the following problem:

I have an array containing a list of non-unique strings. eg:

@array Contains:

11_
22_
33_
33_
33_
44_
44_
55_

What I would like to do is number each element of the array to look as 
follows:


11_1
22_1
33_1
33_2
33_3
44_1
44_2
55_1

I have so far tried to use "exists", while loops and all the usual 
stuff as it seemed very simple at first. My attempts have mostly 
focused on equating the current element of the array with the previous 
one and asking if they are equal and then taking action. I just can't 
get the logic right though. I keep getting them all numbered 
sequentially from 1 to 10 or they all end in "_2" or alternating "_1" 
and "_2" If anybody could shed even the smallest glimmer of light on 
how to solve this I'd be really grateful.


Thanks,
James.



Personally, I would normally use a hash.  However, one hashless solution 
might be:


#!/usr/bin/perl

  use strict;
  use warnings;


  my @array = qw{
11_
22_
33_
33_
33_
44_
44_
55_
};


  my $lastelem = '';
  my $count;
  for my $element (@array) {
$count = 1 unless $element eq $lastelem;
$lastelem = $element;
$element .= $count++;
  }

  print map {"$_\n"} @array;


Best of luck

Nathan


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Counter Help

2016-02-09 Thread Duncan Ferguson
I disagree – hashes are not weird – they are incredibly useful.  It is just an 
array indexed by a word instead of a number  ☺



Here is some working code that may help

===

#!/usr/bin/perl

use strict;

use warnings;



my @array = ( qw/ 11_ 22_ 33_ 33_ 33_ 44_ 44_ 55_ / );



my %results;



# create a hash of arrays

for my $item (@array) {

push(@{ $results{$item} }, $item);

}



# For each entry in the hash

for my $key (sort(keys(%results))) {

   # make a local array back out of what is stored in the hash

my @arr = @{ $results{$key}};

print "key=$key:", $/;

# print out the indexes of items in the local array

print "  $key$_",$/ foreach (0 .. $#arr)

}



  Duncs





From: James Kerwin [mailto:jkerwin2...@gmail.com]

Sent: 09 February 2016 14:47

Cc: beginners@perl.org

Subject: Re: Counter Help



Thank you both very much for your help. I'll investigate this way when I get 
home later (I'm a bit wary of hashes because they're weird).



As my files are sorted numerically I managed to do the following (it's ugly, 
please don't shout at me):



my $length = (scalar @New)-1;

#print $length;

push (my @other, @New[0]);

foreach (@New){

chop ($_);

#print "$_\n";

}



my $i = 0; my $ii = 1;

foreach (@New[1 .. $length]) {

#foreach (@New){

if ($_ ne $New[$i]){

push (@other, (join "", $_,"1"));

$i++;

}

elsif ($_ eq $New[$i]){

$i++;

$ii++;

push (@other, (join "", $_,$ii));

}}



foreach (@other){



print "$_\n";

}



This gave me the desired output. Like I said, I'll investigate the other way 
later...



Thanks,

James.



On Tue, Feb 9, 2016 at 2:22 PM, Jim Gibson  wrote:



> On Feb 9, 2016, at 6:08 AM, James Kerwin  wrote:

>

> Afternoon all,

>

> I have the following problem:

>

> I have an array containing a list of non-unique strings. eg:

>

> @array Contains:

>

> 11_

> 22_

> 33_

> 33_

> 33_

> 44_

> 44_

> 55_

>

> What I would like to do is number each element of the array to look as 
> follows:

>

> 11_1

> 22_1

> 33_1

> 33_2

> 33_3

> 44_1

> 44_2

> 55_1

>

> I have so far tried to use "exists", while loops and all the usual stuff as 
> it seemed very simple at first. My attempts have mostly focused on equating 
> the current element of the array with the previous one and asking if they are 
> equal and then taking action. I just can't get the logic right though. I keep 
> getting them all numbered sequentially from 1 to 10 or they all end in "_2" 
> or alternating "_1" and "_2" If anybody could shed even the smallest glimmer 
> of light on how to solve this I'd be really grateful.



You should define a hash with the strings as keys and the number of strings 
encountered in the array so far as the hash value. You can then add the number 
to each string as you iterate over the array and increment the hash value count.





Jim Gibson

j...@gibson.org









--

To unsubscribe, e-mail: beginners-unsubscr...@perl.org

For additional commands, e-mail: beginners-h...@perl.org

http://learn.perl.org/






Re: Counter Help

2016-02-09 Thread James Kerwin
Thank you both very much for your help. I'll investigate this way when I
get home later (I'm a bit wary of hashes because they're weird).

As my files are sorted numerically I managed to do the following (it's
ugly, please don't shout at me):

my $length = (scalar @New)-1;
#print $length;
push (my @other, @New[0]);
foreach (@New){
chop ($_);
#print "$_\n";
}

my $i = 0; my $ii = 1;
foreach (@New[1 .. $length]) {
#foreach (@New){
if ($_ ne $New[$i]){
push (@other, (join "", $_,"1"));
$i++;
}
elsif ($_ eq $New[$i]){
$i++;
$ii++;
push (@other, (join "", $_,$ii));
}}
foreach (@other){
print "$_\n";
}

This gave me the desired output. Like I said, I'll investigate the other
way later...

Thanks,
James.

On Tue, Feb 9, 2016 at 2:22 PM, Jim Gibson  wrote:

>
> > On Feb 9, 2016, at 6:08 AM, James Kerwin  wrote:
> >
> > Afternoon all,
> >
> > I have the following problem:
> >
> > I have an array containing a list of non-unique strings. eg:
> >
> > @array Contains:
> >
> > 11_
> > 22_
> > 33_
> > 33_
> > 33_
> > 44_
> > 44_
> > 55_
> >
> > What I would like to do is number each element of the array to look as
> follows:
> >
> > 11_1
> > 22_1
> > 33_1
> > 33_2
> > 33_3
> > 44_1
> > 44_2
> > 55_1
> >
> > I have so far tried to use "exists", while loops and all the usual stuff
> as it seemed very simple at first. My attempts have mostly focused on
> equating the current element of the array with the previous one and asking
> if they are equal and then taking action. I just can't get the logic right
> though. I keep getting them all numbered sequentially from 1 to 10 or they
> all end in "_2" or alternating "_1" and "_2" If anybody could shed even the
> smallest glimmer of light on how to solve this I'd be really grateful.
>
> You should define a hash with the strings as keys and the number of
> strings encountered in the array so far as the hash value. You can then add
> the number to each string as you iterate over the array and increment the
> hash value count.
>
>
> Jim Gibson
> j...@gibson.org
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Counter Help

2016-02-09 Thread Jing Yu via beginners
Hello,

I don’t know whether it is possible to count the occurrence in hash? 

print $var, $myhash{$var}++.”\n”;

Jing
> On 9 Feb 2016, at 14:08, James Kerwin  wrote:
> 
> Afternoon all,
> 
> I have the following problem:
> 
> I have an array containing a list of non-unique strings. eg:
> 
> @array Contains:
> 
> 11_
> 22_
> 33_
> 33_
> 33_
> 44_
> 44_
> 55_
> 
> What I would like to do is number each element of the array to look as 
> follows:
> 
> 11_1
> 22_1
> 33_1
> 33_2
> 33_3
> 44_1
> 44_2
> 55_1
> 
> I have so far tried to use "exists", while loops and all the usual stuff as 
> it seemed very simple at first. My attempts have mostly focused on equating 
> the current element of the array with the previous one and asking if they are 
> equal and then taking action. I just can't get the logic right though. I keep 
> getting them all numbered sequentially from 1 to 10 or they all end in "_2" 
> or alternating "_1" and "_2" If anybody could shed even the smallest glimmer 
> of light on how to solve this I'd be really grateful.
> 
> Thanks,
> James.
> 
>  


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Counter Help

2016-02-09 Thread Jim Gibson

> On Feb 9, 2016, at 6:08 AM, James Kerwin  wrote:
> 
> Afternoon all,
> 
> I have the following problem:
> 
> I have an array containing a list of non-unique strings. eg:
> 
> @array Contains:
> 
> 11_
> 22_
> 33_
> 33_
> 33_
> 44_
> 44_
> 55_
> 
> What I would like to do is number each element of the array to look as 
> follows:
> 
> 11_1
> 22_1
> 33_1
> 33_2
> 33_3
> 44_1
> 44_2
> 55_1
> 
> I have so far tried to use "exists", while loops and all the usual stuff as 
> it seemed very simple at first. My attempts have mostly focused on equating 
> the current element of the array with the previous one and asking if they are 
> equal and then taking action. I just can't get the logic right though. I keep 
> getting them all numbered sequentially from 1 to 10 or they all end in "_2" 
> or alternating "_1" and "_2" If anybody could shed even the smallest glimmer 
> of light on how to solve this I'd be really grateful.

You should define a hash with the strings as keys and the number of strings 
encountered in the array so far as the hash value. You can then add the number 
to each string as you iterate over the array and increment the hash value count.


Jim Gibson
j...@gibson.org




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Counter Help

2016-02-09 Thread James Kerwin
Afternoon all,

I have the following problem:

I have an array containing a list of non-unique strings. eg:

@array Contains:

11_
22_
33_
33_
33_
44_
44_
55_

What I would like to do is number each element of the array to look as
follows:

11_1
22_1
33_1
33_2
33_3
44_1
44_2
55_1

I have so far tried to use "exists", while loops and all the usual stuff as
it seemed very simple at first. My attempts have mostly focused on equating
the current element of the array with the previous one and asking if they
are equal and then taking action. I just can't get the logic right though.
I keep getting them all numbered sequentially from 1 to 10 or they all end
in "_2" or alternating "_1" and "_2" If anybody could shed even the
smallest glimmer of light on how to solve this I'd be really grateful.

Thanks,
James.


Re: XML Simple + parsing inner loop elements + help

2015-12-08 Thread perl kamal
Hi,
Thanks you for your valuable comments,let me try the Twig module.


On 12/8/15, Kent Fredric  wrote:
> On 8 December 2015 at 19:25, perl kamal  wrote:
>> I am trying to parse the inner loop elements of the attached input xml
>> elements.
>> The below code doesn't retrieve the inner loop() elements if
>> the properties tag contains more than one item. Will you please  point
>> the error and correct me.
>> Please find the attached input xml file. Thanks.
>
> A quick glance suggests you're getting bitten by one of the known
> problems of XML::Simple: That its completely inconsistent.
>
> 2 seemingly identally strucutred XML files can be decoded completely
> different to each other, so you need to have special cases everywhere
> in your code *just in case* that happens.
>
> Take for instance this simple code and its simple XML
>
> use strict;
> use warnings;
> use utf8;
>
> my $sample_a = <<"EOF";
> 
>  
>   
>  
> 
> EOF
>
> my $sample_b = <<"EOF";
> 
>  
>   
>   
>  
> 
> EOF
>
> use XML::Simple;
> use Data::Dump qw(pp);
> my $sample_a_dec = XMLin($sample_a);
> my $sample_b_dec = XMLin($sample_b);
>
> pp {
>   a => $sample_a_dec,
>   b => $sample_b_dec,
> };
>
> It looks simple, it looks like a and be have similar enough data
> structures, and you expect the pretty printed output to also be
> similar, right? Right?
>
> Nope!
>
> Here, XML::Simple went a bit special snowflake.
>
> {
>   a => { subgroup => { item => { name => "bruce" } } },
>   b => { subgroup => { item => { mary => {}, sue => {} } } },
> }
>
> At first glance you might overlook how these 2 entries are completely
> different.
>
> One is a hash mapping:
>   "somevalue" => hash
> The other is a has mapping:
>"name" => some value
>
> either it should be:
>
>   a => { subgroup => { item => { bruce  => {} } } },
>   b => { subgroup => { item => { mary => {}, sue => {} } } },
>
> or it should be
>
>   a => { subgroup => { item => [{ name => "bruce" }] }},
>   b => { subgroup => { item => [{ name => "mary" }, { name => "sue" }] } }
>
>
> But XML::Simple gave you the worst of both worlds.
>
> For this reason, XML::Simple is not recommended for real world work.
> XML::Twig may be more what you're looking for.
>
> Even its maintainer and author for 15 says "Hey, please don't use this" :)
>
> --
> Kent
>
> KENTNL - https://metacpan.org/author/KENTNL
>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: XML Simple + parsing inner loop elements + help

2015-12-08 Thread shawn wilson
tl;dr I'm not answering your specific question here.

On Dec 8, 2015 1:26 AM, "perl kamal"  wrote:
>
> Hi,
>
> I am trying to parse the inner loop elements of the attached input xml
elements.

Just fyi, I've found it easier to use xslt as an etl preprocessor to perl.
I'm not sure how you intend to use the output but you might look into basex
if you intend to store and parse lots of xml.

> use strict;
> use XML::Simple;
> use Data::Dumper;
>

You'll find that for lots of uses, ::Simple is too simple. Also note that
no perl modules support newer features (specifically, I've found xpath to
be lacking) - another reason to prefer using xslt (though python's has
richer modules for xml - but I can't stand python so...).


  1   2   3   4   5   6   7   8   9   10   >