Re: array within array
On Oct 29, 11:09 am, [EMAIL PROTECTED] (John W . Krahn) wrote: On Monday 29 October 2007 06:42, Mike Tran wrote: Hey all, Hello, I'm new with Perl and need help with this simple script. I'm still playing around with the script below to get a feel for Perl. My script below is incomplete and I'm doing an array within an array which is incorrect. Please help. You are not using arrays you are using hashes. Here's what I want to do; I have to flat files (pipe delimited, export from a database) that I want to parse through and assign variables for each column. Basically, I want to parse through exclude_bases.txt and do: if base_no in exclude_bases.txt equals to base_no in base.txt then search in the description field of base.txt for the string listed in the keyword field in exclude_bases.tx and replace with new_keyword in exclude_bases.txt and write the out put into a new file called new_bases.txt. Any suggestions on how I could accomplish the above task is greatly appreciated. Thanks all. Flat Files: base.txt: base_no|name|description 1|test|test desc 10001|test2|test desc 2 10002|test3|test desc 3 exclude_bases.txt: base_no|keyword|new_keyword| 1|test desc|testdesc|0 10001|test desc 2|testdesc2|0 10002|test desc 3|testdesc3|1 [snip] It looks like you may want something like this: #!/usr/bin/perl use strict; use warnings; my $exclude_bases = 'exclude_bases.txt'; my $current_base = 'base.txt'; my $output= 'new_bases.txt'; open EXCLUDE, '', $exclude_bases or die Could not open '$exclude_bases' $!; my %exclude_bases; while ( EXCLUDE ) { next if $. == 1; # exclude header chomp; my ( $exbase_no, $keyword, $new_keyword ) = split /\|/; $exclude_bases{ $exbase_no } = { from = qr/\Q$keyword/, to = $new_keyword }; } Since you're not taking into account the blank lines, you'll be generating these warnings: Use of uninitialized value in quotemeta at C:\test\JohnKrahn.pl line 17, EXCLUDE line 2. Use of uninitialized value in hash element at C:\test\JohnKrahn.pl line 17, EXCLUDE line 2. Use of uninitialized value in quotemeta at C:\test\JohnKrahn.pl line 17, EXCLUDE line 4. Use of uninitialized value in hash element at C:\test\JohnKrahn.pl line 17, EXCLUDE line 4. close EXCLUDE; open BASE, '', $current_base or die Could not open '$current_base' $!; open OUT, '', $outputor die Could not open '$output' $!; while ( BASE ) { my ( $base_no, $name, $description ) = split /\|/; if ( exists $exclude_bases{ $base_no } ) { $description =~ s/$exclude_bases{$base_no}{from}/$exclude_bases{$base_no}{to}/g; $_ = join '|', $base_no, $name, $description; } print OUT; } Again, blank lines are not taken into account, but no warnings since the printing is being handled within the conditional block. Since a complete solution, to what appears to be a homework question, has already been provided, I guess I'll show mine. #!/usr/bin/perl use warnings; use strict; my %base; open (my $new, '', 'new_bases.txt') || die new_bases.txt $!; open (my $exclude, '', 'exclude_bases.txt') || die exclude_bases.txt $!; open (my $base, '', 'base.txt') || die base.txt $!; while ($base) { next if /^\s*$/; # skip over blank lines print and next if $. == 1; # print header chomp; my @fields = split /\|/; $base{$fields[0]} = [EMAIL PROTECTED]; } close $base; while ($exclude) { next if /^\s*$/; # skip over blank lines chomp; my @fields = split /\|/; if ( exists $base{$fields[0]} and $fields[1] eq $base{$fields[0]} [2]) { $base{$fields[0]}[2] = $fields[2]; # if you want to retain the empty lines between records, # you can use \n\n in the print statement, # or reassign the output record separator print $new join('|', @{$base{$fields[0]}}), \n; } } close $exclude; close $new; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: array within array
On 29 Oct 2007 at 8:42, Mike Tran wrote: Hey all, I'm new with Perl and need help with this simple script. I'm still playing around with the script below to get a feel for Perl. My script below is incomplete and I'm doing an array within an array which is incorrect. Please help. Here's what I want to do; I have to flat files (pipe delimited, export from a database) that I want to parse through and assign variables for each column. Basically, I want to parse through exclude_bases.txt and do: if base_no in exclude_bases.txt equals to base_no in base.txt then search in the description field of base.txt for the string listed in the keyword field in exclude_bases.tx and replace with new_keyword in exclude_bases.txt and write the out put into a new file called new_bases.txt. Any suggestions on how I could accomplish the above task is greatly appreciated. Thanks all. Flat Files: base.txt: base_no|name|description 1|test|test desc 10001|test2|test desc 2 10002|test3|test desc 3 exclude_bases.txt: base_no|keyword|new_keyword| 1|test desc|testdesc|0 10001|test desc 2|testdesc2|0 10002|test desc 3|testdesc3|1 #!/usr/bin/perl use strict; use warnings; my $exclude_bases = exclude_bases.txt; my $current_base = base.txt; my $output = new_bases.txt; my %exclude_bases; my $exclude_text; my $exbase_no; my $keyword; my $new_keyword; open(EXCLUDE,exclude_bases.txt )|| die(Could not open file!); %exclude_bases=EXCLUDE; close(EXCLUDE); my $base_no =; my $name=; my $description=; my $current_base=; my $base_text=; my %bases; open(BASE,base.txt)|| die(Could not open file!); %bases=BASE; close(BASE); #choping lines and assign variables to base.txt foreach $base_text (%bases) { chop($base_text); ($base_no,$name,$description)=split(/\|/,$base_text); #choping lines and assign variables to exclude_bases.txt foreach $exclude_text (%exclude_bases) { chop($exclude_text); ($exbase_no,$keyword,$new_keyword)=split(/\|/,$base_text); if ($exbase_no=$base_no) { $keyword =~ s/$keyword/$new_keyword/g;} Well here my effort. It should help you get closer to what your after but it's not complete. I would use hashes not arrays, you'll find them extremely useful for de-duping data I haven't run the script below, please do a perl -c first. Check out exists perldoc -f exists #!/usr/bin/perl use strict; # Well done use warnings; # ditto. my $exclude_bases = exclude_bases.txt; open(EXCLUDE,excludes.txt )|| die(Could not open file!); while (EXCLUDE) { chomp; my @fields = split(/|/,$_); $exclude_bases{$F[0]} = 0; # $f[0] contains base_no } close(EXCLUDE); open(BASE,base.txt)|| die(Could not open file!); while (BASE) { chomp; my @fields = split(/|/,$_); if (! exists($exlude_bases{$_}) ) { print $f[2]\n; } } close(BASE); HTH, Dp. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: array within array
be very careful with exists, it auto creates the structure i.e. use Data::Dumper; my %hash; if (exists $hash{a}{b}) { } print Dumper(\%hash) then the next time you use exists it is there. defined is much safer as it doesnt do this. -Original Message- From: Beginner [mailto:[EMAIL PROTECTED] Sent: 29 October 2007 15:30 To: beginners @ perl. org Subject: Re: array within array On 29 Oct 2007 at 8:42, Mike Tran wrote: Hey all, I'm new with Perl and need help with this simple script. I'm still playing around with the script below to get a feel for Perl. My script below is incomplete and I'm doing an array within an array which is incorrect. Please help. Here's what I want to do; I have to flat files (pipe delimited, export from a database) that I want to parse through and assign variables for each column. Basically, I want to parse through exclude_bases.txt and do: if base_no in exclude_bases.txt equals to base_no in base.txt then search in the description field of base.txt for the string listed in the keyword field in exclude_bases.tx and replace with new_keyword in exclude_bases.txt and write the out put into a new file called new_bases.txt. Any suggestions on how I could accomplish the above task is greatly appreciated. Thanks all. Flat Files: base.txt: base_no|name|description 1|test|test desc 10001|test2|test desc 2 10002|test3|test desc 3 exclude_bases.txt: base_no|keyword|new_keyword| 1|test desc|testdesc|0 10001|test desc 2|testdesc2|0 10002|test desc 3|testdesc3|1 #!/usr/bin/perl use strict; use warnings; my $exclude_bases = exclude_bases.txt; my $current_base = base.txt; my $output = new_bases.txt; my %exclude_bases; my $exclude_text; my $exbase_no; my $keyword; my $new_keyword; open(EXCLUDE,exclude_bases.txt )|| die(Could not open file!); %exclude_bases=EXCLUDE; close(EXCLUDE); my $base_no =; my $name=; my $description=; my $current_base=; my $base_text=; my %bases; open(BASE,base.txt)|| die(Could not open file!); %bases=BASE; close(BASE); #choping lines and assign variables to base.txt foreach $base_text (%bases) { chop($base_text); ($base_no,$name,$description)=split(/\|/,$base_text); #choping lines and assign variables to exclude_bases.txt foreach $exclude_text (%exclude_bases) { chop($exclude_text); ($exbase_no,$keyword,$new_keyword)=split(/\|/,$base_text); if ($exbase_no=$base_no) { $keyword =~ s/$keyword/$new_keyword/g;} Well here my effort. It should help you get closer to what your after but it's not complete. I would use hashes not arrays, you'll find them extremely useful for de-duping data I haven't run the script below, please do a perl -c first. Check out exists perldoc -f exists #!/usr/bin/perl use strict; # Well done use warnings; # ditto. my $exclude_bases = exclude_bases.txt; open(EXCLUDE,excludes.txt )|| die(Could not open file!); while (EXCLUDE) { chomp; my @fields = split(/|/,$_); $exclude_bases{$F[0]} = 0; # $f[0] contains base_no } close(EXCLUDE); open(BASE,base.txt)|| die(Could not open file!); while (BASE) { chomp; my @fields = split(/|/,$_); if (! exists($exlude_bases{$_}) ) { print $f[2]\n; } } close(BASE); HTH, Dp. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ This e-mail is from the PA Group. For more information, see www.thepagroup.com. This e-mail may contain confidential information. Only the addressee is permitted to read, copy, distribute or otherwise use this email or any attachments. If you have received it in error, please contact the sender immediately. Any opinion expressed in this e-mail is personal to the sender and may not reflect the opinion of the PA Group. Any e-mail reply to this address may be subject to interception or monitoring for operational reasons or for lawful business practices. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: array within array
On Monday 29 October 2007 07:29, Beginner wrote: while (EXCLUDE) { chomp; my @fields = split(/|/,$_); ^^^ $exclude_bases{$F[0]} = 0; # $f[0] contains base_no } close(EXCLUDE); open(BASE,base.txt)|| die(Could not open file!); while (BASE) { chomp; my @fields = split(/|/,$_); ^^^ The '|' character has a special meaning in a regular expression so you have to escape it if you want to match a literal '|' character. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: array within array
Andrew Curry schreef: be very careful with exists, it auto creates the structure use Data::Dumper; my %hash; if (exists $hash{a}{b}) {} print Dumper(\%hash) then the next time you use exists it is there. defined is much safer as it doesnt do this. No, the difference here is not between exists() and defined(). To be able to check the definedness (or the existence) of $h{foo}{bar}, $h{foo} is created first (auto-vivification). Neither exists() nor defined() short-cuts hash-levels. perl -MData::Dumper -wle' my %hash; if ( defined $hash{foo}{bar} ) {}; print Dumper \%hash; ' $VAR1 = { 'foo' = {} }; (same with exists()) Solution: test $h{foo} first: if ( exists( $hash{foo} ) and exists( $hash{foo}{bar} ) ) { ... } -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: array within array
On Monday 29 October 2007 06:42, Mike Tran wrote: Hey all, Hello, I'm new with Perl and need help with this simple script. I'm still playing around with the script below to get a feel for Perl. My script below is incomplete and I'm doing an array within an array which is incorrect. Please help. You are not using arrays you are using hashes. Here's what I want to do; I have to flat files (pipe delimited, export from a database) that I want to parse through and assign variables for each column. Basically, I want to parse through exclude_bases.txt and do: if base_no in exclude_bases.txt equals to base_no in base.txt then search in the description field of base.txt for the string listed in the keyword field in exclude_bases.tx and replace with new_keyword in exclude_bases.txt and write the out put into a new file called new_bases.txt. Any suggestions on how I could accomplish the above task is greatly appreciated. Thanks all. Flat Files: base.txt: base_no|name|description 1|test|test desc 10001|test2|test desc 2 10002|test3|test desc 3 exclude_bases.txt: base_no|keyword|new_keyword| 1|test desc|testdesc|0 10001|test desc 2|testdesc2|0 10002|test desc 3|testdesc3|1 #!/usr/bin/perl use strict; use warnings; my $exclude_bases = exclude_bases.txt; my $current_base = base.txt; my $output = new_bases.txt; my %exclude_bases; my $exclude_text; my $exbase_no; my $keyword; my $new_keyword; You should define variables in the smallest possible scope. open(EXCLUDE,exclude_bases.txt )|| die(Could not open file!); You have stored the file name in the $exclude_bases variable above so why do you not use the variable instead? You should include the $! variable in the error message so you know *why* it failed. %exclude_bases=EXCLUDE; You are assigning the list from the file to a hash. The list elements are defined by the value of the $/ variable so here each element of the list is a line from the file. Because you are assigning to a hash the first element is a key and the second element is the value for that key. if any of the keys (odd elements) are the same as a previous key then the old value will be overwritten by the new value and you will lose lines from the file. close(EXCLUDE); my $base_no =; my $name=; my $description=; my $current_base=; my $base_text=; my %bases; open(BASE,base.txt)|| die(Could not open file!); Same as above. %bases=BASE; Same as above. close(BASE); #choping lines and assign variables to base.txt foreach $base_text (%bases) { chop($base_text); You should use chomp() instead of chop(). ($base_no,$name,$description)=split(/\|/,$base_text); #choping lines and assign variables to exclude_bases.txt foreach $exclude_text (%exclude_bases) { chop($exclude_text); Same as above. ($exbase_no,$keyword,$new_keyword)=split(/\|/,$base_text); if ($exbase_no=$base_no) { You are assigning the value of $base_no to the variable $exbase_no. If the value of $base_no is true then the expression is true. You want to use a comparison operator like '==' or 'eq'. $keyword =~ s/$keyword/$new_keyword/g;} Your description said that you wanted to modify 'the description field of base.txt' so that sould be: $description =~ s/$keyword/$new_keyword/g;} } } It looks like you may want something like this: #!/usr/bin/perl use strict; use warnings; my $exclude_bases = 'exclude_bases.txt'; my $current_base = 'base.txt'; my $output= 'new_bases.txt'; open EXCLUDE, '', $exclude_bases or die Could not open '$exclude_bases' $!; my %exclude_bases; while ( EXCLUDE ) { next if $. == 1; # exclude header chomp; my ( $exbase_no, $keyword, $new_keyword ) = split /\|/; $exclude_bases{ $exbase_no } = { from = qr/\Q$keyword/, to = $new_keyword }; } close EXCLUDE; open BASE, '', $current_base or die Could not open '$current_base' $!; open OUT, '', $outputor die Could not open '$output' $!; while ( BASE ) { my ( $base_no, $name, $description ) = split /\|/; if ( exists $exclude_bases{ $base_no } ) { $description =~ s/$exclude_bases{$base_no}{from}/$exclude_bases{$base_no}{to}/g; $_ = join '|', $base_no, $name, $description; } print OUT; } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: array within array
On Oct 29, 6:42 am, [EMAIL PROTECTED] (Mike Tran) wrote: Hey all, I'm new with Perl and need help with this simple script. I'm still playing around with the script below to get a feel for Perl. My script below is incomplete and I'm doing an array within an array which is incorrect. Please help. Here's what I want to do; I have to flat files (pipe delimited, export from a database) that I want to parse through and assign variables for each column. Basically, I want to parse through exclude_bases.txt and do: if base_no in exclude_bases.txt equals to base_no in base.txt then search in the description field of base.txt for the string listed in the keyword field in exclude_bases.tx and replace with new_keyword in exclude_bases.txt and write the out put into a new file called new_bases.txt. Any suggestions on how I could accomplish the above task is greatly appreciated. Thanks all. This sounds like a homework assignment, so I won't provide a complete solution, but I will give a few pointers and maybe a little code. Flat Files: base.txt: base_no|name|description 1|test|test desc 10001|test2|test desc 2 10002|test3|test desc 3 exclude_bases.txt: base_no|keyword|new_keyword| 1|test desc|testdesc|0 10001|test desc 2|testdesc2|0 10002|test desc 3|testdesc3|1 Your description of your required output doesn't say what you want to do with the 4th field in the exclude_bases.txt. #!/usr/bin/perl use strict; use warnings; my $exclude_bases = exclude_bases.txt; my $current_base = base.txt; my $output = new_bases.txt; my %exclude_bases; my $exclude_text; my $exbase_no; my $keyword; my $new_keyword; You don't really need most of those and the following global vars. You should define the vars in the smallest scope that they require. open(EXCLUDE,exclude_bases.txt )|| die(Could not open file!); Why didn't you use the $exclude_bases var that you previously defined? You should use the 3 arg form of open and include $! in the die statement which will tell you why it failed. %exclude_bases=EXCLUDE; Why are you slurping the data into a hash? If you use the Data::Dumper module to output that hash, I think you'll find that it's not in the format that you wanted. close(EXCLUDE); my $base_no =; my $name=; my $description=; my $current_base=; my $base_text=; my %bases; open(BASE,base.txt)|| die(Could not open file!); %bases=BASE; close(BASE); See my previous comments. #choping lines and assign variables to base.txt foreach $base_text (%bases) { chop($base_text); use chomp instead of chop ($base_no,$name,$description)=split(/\|/,$base_text); #choping lines and assign variables to exclude_bases.txt foreach $exclude_text (%exclude_bases) { chop($exclude_text); ($exbase_no,$keyword,$new_keyword)=split(/\|/,$base_text); if ($exbase_no=$base_no) { $keyword =~ s/$keyword/$new_keyword/g;} } } ~ Here's how I'd load the hash. my %base; open (my $base, '', 'base.txt') || die base.txt $!; while ($base) { next if /^\s*$/; chomp; my @fields = split /\|/; $base{$fields[0]} = [EMAIL PROTECTED]; } close $base; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: array within array
John, That is exactly what I wanted. Thanks for pointing out all those errors for me and your help with the script. Appreciate the prompt respond. Cheers, Mike -Original Message- From: John W.Krahn [mailto:[EMAIL PROTECTED] Sent: Monday, October 29, 2007 1:10 PM To: Perl beginners Subject: Re: array within array On Monday 29 October 2007 06:42, Mike Tran wrote: Hey all, Hello, I'm new with Perl and need help with this simple script. I'm still playing around with the script below to get a feel for Perl. My script below is incomplete and I'm doing an array within an array which is incorrect. Please help. You are not using arrays you are using hashes. Here's what I want to do; I have to flat files (pipe delimited, export from a database) that I want to parse through and assign variables for each column. Basically, I want to parse through exclude_bases.txt and do: if base_no in exclude_bases.txt equals to base_no in base.txt then search in the description field of base.txt for the string listed in the keyword field in exclude_bases.tx and replace with new_keyword in exclude_bases.txt and write the out put into a new file called new_bases.txt. Any suggestions on how I could accomplish the above task is greatly appreciated. Thanks all. Flat Files: base.txt: base_no|name|description 1|test|test desc 10001|test2|test desc 2 10002|test3|test desc 3 exclude_bases.txt: base_no|keyword|new_keyword| 1|test desc|testdesc|0 10001|test desc 2|testdesc2|0 10002|test desc 3|testdesc3|1 #!/usr/bin/perl use strict; use warnings; my $exclude_bases = exclude_bases.txt; my $current_base = base.txt; my $output = new_bases.txt; my %exclude_bases; my $exclude_text; my $exbase_no; my $keyword; my $new_keyword; You should define variables in the smallest possible scope. open(EXCLUDE,exclude_bases.txt )|| die(Could not open file!); You have stored the file name in the $exclude_bases variable above so why do you not use the variable instead? You should include the $! variable in the error message so you know *why* it failed. %exclude_bases=EXCLUDE; You are assigning the list from the file to a hash. The list elements are defined by the value of the $/ variable so here each element of the list is a line from the file. Because you are assigning to a hash the first element is a key and the second element is the value for that key. if any of the keys (odd elements) are the same as a previous key then the old value will be overwritten by the new value and you will lose lines from the file. close(EXCLUDE); my $base_no =; my $name=; my $description=; my $current_base=; my $base_text=; my %bases; open(BASE,base.txt)|| die(Could not open file!); Same as above. %bases=BASE; Same as above. close(BASE); #choping lines and assign variables to base.txt foreach $base_text (%bases) { chop($base_text); You should use chomp() instead of chop(). ($base_no,$name,$description)=split(/\|/,$base_text); #choping lines and assign variables to exclude_bases.txt foreach $exclude_text (%exclude_bases) { chop($exclude_text); Same as above. ($exbase_no,$keyword,$new_keyword)=split(/\|/,$base_text); if ($exbase_no=$base_no) { You are assigning the value of $base_no to the variable $exbase_no. If the value of $base_no is true then the expression is true. You want to use a comparison operator like '==' or 'eq'. $keyword =~ s/$keyword/$new_keyword/g;} Your description said that you wanted to modify 'the description field of base.txt' so that sould be: $description =~ s/$keyword/$new_keyword/g;} } } It looks like you may want something like this: #!/usr/bin/perl use strict; use warnings; my $exclude_bases = 'exclude_bases.txt'; my $current_base = 'base.txt'; my $output= 'new_bases.txt'; open EXCLUDE, '', $exclude_bases or die Could not open '$exclude_bases' $!; my %exclude_bases; while ( EXCLUDE ) { next if $. == 1; # exclude header chomp; my ( $exbase_no, $keyword, $new_keyword ) = split /\|/; $exclude_bases{ $exbase_no } = { from = qr/\Q$keyword/, to = $new_keyword }; } close EXCLUDE; open BASE, '', $current_base or die Could not open '$current_base' $!; open OUT, '', $outputor die Could not open '$output' $!; while ( BASE ) { my ( $base_no, $name, $description ) = split /\|/; if ( exists $exclude_bases{ $base_no } ) { $description =~ s/$exclude_bases{$base_no}{from}/$exclude_bases{$base_no}{to}/g; $_ = join '|', $base_no, $name, $description; } print OUT; } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
RE: Array of Array refs
Enough Is enough, can we leave this thread be now. This just puts people off posting questions looking for help in fear of joining some flame war. -Original Message- From: Brian [mailto:[EMAIL PROTECTED] Sent: 29 May 2007 19:30 To: beginners@perl.org; [EMAIL PROTECTED] Subject: Re: Array of Array refs On May 29, 6:06 am, [EMAIL PROTECTED] (Paul Lalli) wrote: On May 29, 4:58 am, [EMAIL PROTECTED] (Brian) wrote: On May 28, 6:14 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: oh yes, more important than all that minutiae... the push did not work for me in the working code. The push worked absolutely fine. It just didn't do what you wanted it to. Learning how to parse your problem should be your first step toward becoming a better programmer. hmmm, misunderstanding there. The push worked fine in the sample I posted, but not in the more complex working program I had simplified as an example. nope, sorry, you're wrong. push() works perfectly well. It adds elements to an array. If your program produced incorrect results, it is because you did something wrong, not because push didn't work. ugh. pedantic semantics... Of course, as you haven't shown any code that produces these undesired results, we can only guess as to what your actual problem was. yes, the real code is beyond what I would think of as beginner I truly meant to just post a small, working piece pf code that worked with some basic data structures... The DBI client is a bit more complicated, yes? The array was being rewritten. Then you didn't delcare your variables in the correct scope. understandable misperception on your part, as above Nope. If your array is being used in a loop, the contents of that array are changing when you don't want them to be changing, and instead want to be creating new arrays, you declared your array in the wrong scope. The sample I put out and the code I was working on are not identical. I declared my array in a different scope in the sample. Neither is wrong, they are different. I had to use an array copy push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array, push array ref Do you understand *why* that was necessary? Do you understand the difference between these two pieces of code? actually, I do indeed. In C++, the concept of a deep copy, vs shallow copy vs ref comes up all the time. I am just learning the syntax here, not programming itself perhaps you should be. . . ugh. insults. I am not trying to insulting you Paul, why resort to that? You have no idea how many programs I've written, well. I'd say its bad form to assume the worst or lowest in people. aha, a force to be reckoned with. Your point above about the docs is quite true. I dont have time to rewrite docs right now.. They do need work though You don't even have time to point out what you find to be wrong with them? But instead you do have time to create examples that you claim to be in the service of newbies, all the while saying that the docs are bad? I'd suggest you could do with attending a few more time- management seminars. please examine logic - rewriting core Perl docs vs a 10 line sample program This is sometimes appearing to be contentious No sometimes about it. Every post you've made thus far is contentious, and so I have answered in kind. no, I'd suggest you read it that way.. Its notoriously difficult to convey context and nuance in a few lines of ascii text. Please refer to your RTFM and then essentially yelling at me for a modest sample program. I am not backing down from your brow-beating. I posted a small working sample program then found out more about the context I was dealing with and attempted to discuss it in an unassuming manner. For this I get these responses. phooey! I'll be posting again from time to time, and probably going to OSCon. Happy to talk with you anytime. May not respond every time though. I think I am sensing a pattern here... best regards -Brian -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ This e-mail is from the PA Group. For more information, see www.thepagroup.com. This e-mail may contain confidential information. Only the addressee is permitted to read, copy, distribute or otherwise use this email or any attachments. If you have received it in error, please contact the sender immediately. Any opinion expressed in this e-mail is personal to the sender and may not reflect the opinion of the PA Group. Any e-mail reply to this address may be subject to interception or monitoring for operational reasons or for lawful business practices. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 28, 6:14 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: oh yes, more important than all that minutiae... the push did not work for me in the working code. The push worked absolutely fine. It just didn't do what you wanted it to. Learning how to parse your problem should be your first step toward becoming a better programmer. hmmm, misunderstanding there. The push worked fine in the sample I posted, but not in the more complex working program I had simplified as an example. The array was being rewritten. Then you didn't delcare your variables in the correct scope. understandable misperception on your part, as above ...As a general rule of thumb, declare your variables in the smallest scope possible. yes, sounds good. Please realize that after many long commercial projects in C/C++, we did this to an enormous degree I had to use an array copy push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array, push array ref Do you understand *why* that was necessary? Do you understand the difference between these two pieces of code? actually, I do indeed. In C++, the concept of a deep copy, vs shallow copy vs ref comes up all the time. I am just learning the syntax here, not programming itself #Sample 1 ... hey, thats alot of sample! you're good at this, no kidding driven too Do you see now? ... yep, never did miss that. thanks though... ...By declaring only one instance of @array1... this is helpful to others to talk this out I'd say this can never be explained too much. Its slippery and a constant source of error. Paul Lalli aha, a force to be reckoned with. Your point above about the docs is quite true. I dont have time to rewrite docs right now.. They do need work though This is sometimes appearing to be contentious, but I mean it to produce clarity. I appreciate your responses -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 28, 6:04 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: On May 28, 3:22 pm, [EMAIL PROTECTED] (Brian) wrote: my variable names end with A for array and H for hash, Pointless. Variable names in Perl identify what kind of variable they are. @ for arrays, % for hashes. [ ] for accessing an element of a hash, { } for accessing element of a hash. no, I disagree. Changing @ to $ is confusing... besides that, my naming is consistent, and helps me understand what I'm writing. I think you missed it on that one... its a disservice to beginners to say 'pointless' like that.. ( I may not respond in such detail in the future.. Its just that I'm new here, - I'm surprised by all of this intensity over something pretty mundane. ) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 29, 5:30 am, [EMAIL PROTECTED] (Brian) wrote: On May 28, 6:04 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: On May 28, 3:22 pm, [EMAIL PROTECTED] (Brian) wrote: my variable names end with A for array and H for hash, Pointless. Variable names in Perl identify what kind of variable they are. @ for arrays, % for hashes. [ ] for accessing an element of a hash, { } for accessing element of a hash. no, I disagree. Changing @ to $ is confusing @ is for entire arrays. $ is for single elements. How is that confusing? ... besides that, my naming is consistent, and helps me understand what I'm writing. I think you missed it on that one Your method relies on the programmer being consistent. Perl's built- in method is inforced via the compiler. Your method relies on all programmers who will ever read or modify your code following the same convention. ... its a disservice to beginners to say 'pointless' like that.. You're right, it's actually worse than pointless, since it's not enforceable nor guaranteed, and therefore creates a false sense of security. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 29, 4:58 am, [EMAIL PROTECTED] (Brian) wrote: On May 28, 6:14 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: oh yes, more important than all that minutiae... the push did not work for me in the working code. The push worked absolutely fine. It just didn't do what you wanted it to. Learning how to parse your problem should be your first step toward becoming a better programmer. hmmm, misunderstanding there. The push worked fine in the sample I posted, but not in the more complex working program I had simplified as an example. nope, sorry, you're wrong. push() works perfectly well. It adds elements to an array. If your program produced incorrect results, it is because you did something wrong, not because push didn't work. Of course, as you haven't shown any code that produces these undesired results, we can only guess as to what your actual problem was. The array was being rewritten. Then you didn't delcare your variables in the correct scope. understandable misperception on your part, as above Nope. If your array is being used in a loop, the contents of that array are changing when you don't want them to be changing, and instead want to be creating new arrays, you declared your array in the wrong scope. I had to use an array copy push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array, push array ref Do you understand *why* that was necessary? Do you understand the difference between these two pieces of code? actually, I do indeed. In C++, the concept of a deep copy, vs shallow copy vs ref comes up all the time. I am just learning the syntax here, not programming itself perhaps you should be. . . aha, a force to be reckoned with. Your point above about the docs is quite true. I dont have time to rewrite docs right now.. They do need work though You don't even have time to point out what you find to be wrong with them? But instead you do have time to create examples that you claim to be in the service of newbies, all the while saying that the docs are bad? I'd suggest you could do with attending a few more time- management seminars. This is sometimes appearing to be contentious No sometimes about it. Every post you've made thus far is contentious, and so I have answered in kind. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
Brian wrote: Paul Lalli wrote: Brian wrote: my variable names end with A for array and H for hash, Pointless. Variable names in Perl identify what kind of variable they are. @ for arrays, % for hashes. [ ] for accessing an element of a hash, { } for accessing element of a hash. no, I disagree. Changing @ to $ is confusing... besides that, my naming is consistent, and helps me understand what I'm writing. I think you missed it on that one... its a disservice to beginners to say 'pointless' like that.. ( I may not respond in such detail in the future.. Its just that I'm new here, - I'm surprised by all of this intensity over something pretty mundane.) I agree with you Brian. I have been posting on this list for around four years, and rarely have I seen 'help' that is expressed with such vitriol as Paul's has been. When Casey West envisaged perl.beginners back in 2001 his intent was to avoid this very attitude of muscle-flexing. He writes: ~ It seems that the very thing we want to have happen, adding number to our ~ ranks, is the first thing we fight against when beginners show their ~ faces. Wielding our swords of RTFM and shields of killfile we smite ~ the very programmers that will carry this language into the future. I ~ have a co-worker who is known for saying, It's a good thing Perl is so ~ powerful and cool, it barely makes up for the collective, childish ~ 'elitism' displayed by its community. Collectively, this is a sad truth. I hope you will persist in posting requests for, and perhaps offers of help. All group members are valuable and I look forward to your input. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 29, 10:16 am, [EMAIL PROTECTED] (Rob Dixon) wrote: Brian wrote: I agree with you Brian. I have been posting on this list for around four years, and rarely have I seen 'help' that is expressed with such vitriol as Paul's has been. I'm truly sorry you feel that way, Brian. My responses are intended to match the attitude of the poster. When the poster starts off a thread by implying that the documentation is worthless, and that his superior programming and debugging skills are going to be of far more use to a new programmer, I tend to lose respect for him. When he continues the thread by refusing to point out *what* is wrong with the docs, only asserting that they are provably unhelpful, and then stating that he has not the time to rewrite them, I lose all respect. When he continuously asserts that a built-in function doesn't work, but refuses to show any code to support this assertion, we sink into the negative, and it becomes blatant dis- respect. Yes, my attitude in response is more blatant and obvious. I don't hide how I feel behind polite-sounding words. That doesn't make the attitude itself any different than the OP's. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 29, 11:10 am, [EMAIL PROTECTED] (Paul Lalli) wrote: On May 29, 10:16 am, [EMAIL PROTECTED] (Rob Dixon) wrote: Brian wrote: I agree with you Brian. I have been posting on this list for around four years, and rarely have I seen 'help' that is expressed with such vitriol as Paul's has been. I'm truly sorry you feel that way, Brian. I'm meant Rob here, of course. My bad. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
Brian schreef: Changing @ to $ is confusing... Huh? @ means array, $ means scalar; there is nothing to change. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 29, 3:21 pm, [EMAIL PROTECTED] (Dr.Ruud) wrote: Brian schreef: Changing @ to $ is confusing... Huh? @ means array, $ means scalar; there is nothing to change. Presumably, he meant that @array identifies the entire array, while $array[0] identifies the first element of the array, thus changing the @ to a $ to access a single element. The mnemonic device is not @ for array, $ for scalar, but rather: $ is for single elements, be they single elments of arrays or hashes, or actual scalar variables. @ is for lists of data, whether entire arrays, slices of arrays, or slices of hashes % is for an entire hash. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Re: Array of Array refs @ 1180470274
Intrah onat Diria .. , ** wrote Revera y: Changing @ to $ is confusing... this is different in perl6 , http://dev.perl.org/perl6/doc/design/syn/S09.html the following could be unreadable @ 1180470350 ::: hbeingspiel , 0::- , 1::',| , 0::| , 1::vert{ , 1::gruen , 1::{wasser , 0::.recof.REF. , 0::.. , 1::ein.oyellow , 0::^[icx] , 1::PROPOSINGoimmane , -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On 29 May 2007 13:04:42 -0700, Paul Lalli [EMAIL PROTECTED] wrote: On May 29, 3:21 pm, [EMAIL PROTECTED] (Dr.Ruud) wrote: Brian schreef: Changing @ to $ is confusing... Huh? @ means array, $ means scalar; there is nothing to change. Presumably, he meant that @array identifies the entire array, while $array[0] identifies the first element of the array, thus changing the @ to a $ to access a single element. The mnemonic device is not @ for array, $ for scalar, but rather: $ is for single elements, be they single elments of arrays or hashes, or actual scalar variables. @ is for lists of data, whether entire arrays, slices of arrays, or slices of hashes % is for an entire hash. Paul Lalli Of course, that is changing in Perl 6. Scalars (including references) will always be prefixed with $, arrays will always be prefixed with @, and hashes will always be prefixed with %. Array slices and indexing will be preformed thusly: pugs my @a = 1 .. 5 #the same in Perl 5 (1, 2, 3, 4, 5) pugs @a[2..4] #the same in Perl 5 (3, 4, 5) pugs @a[1] #$a[1] in Perl 5 2 pugs my $aref = @a; #my $aref = [EMAIL PROTECTED] in Perl 5 pugs $aref[1] #$aref-[1] in Perl 5 2 pugs $aref[1 .. 3] [EMAIL PROTECTED] in Perl 5 (2, 3, 4) And hash slices and indexing will be done like this pugs my %h = zip one two three four, [1 .. 4] #much more complicated in Perl 5 ((four, 4), (one, 1), (three, 3), (two, 2)) pugs %hone two #%h{qwone two} in Perl 5 (1, 2) pugs %hthree #$h{three} in Perl 5 3 pugs %h{four} #oops, this is an error in Perl 6, {} does not stringify barewords *** No such subroutine: four at interactive line 1, column 4-8 pugs %h{'four'} #$h{four} in Perl 5 (note the necessary '' in Perl 6, use instead) 4 pugs my $href = %h #my $href = \%h; in Perl 5 ((four, 4), (one, 1), (three, 3), (two, 2)) pugs $hrefone two #%{$href}{qwone two} in Perl 5 (1, 2) pugs $hrefthree #$href-{three} in Perl 5 3 pugs $href{'four'} #$href-{four} in Perl 5, again Perl 6 needs the quotes 4 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 29, 6:06 am, [EMAIL PROTECTED] (Paul Lalli) wrote: On May 29, 4:58 am, [EMAIL PROTECTED] (Brian) wrote: On May 28, 6:14 pm, [EMAIL PROTECTED] (Paul Lalli) wrote: oh yes, more important than all that minutiae... the push did not work for me in the working code. The push worked absolutely fine. It just didn't do what you wanted it to. Learning how to parse your problem should be your first step toward becoming a better programmer. hmmm, misunderstanding there. The push worked fine in the sample I posted, but not in the more complex working program I had simplified as an example. nope, sorry, you're wrong. push() works perfectly well. It adds elements to an array. If your program produced incorrect results, it is because you did something wrong, not because push didn't work. ugh. pedantic semantics... Of course, as you haven't shown any code that produces these undesired results, we can only guess as to what your actual problem was. yes, the real code is beyond what I would think of as beginner I truly meant to just post a small, working piece pf code that worked with some basic data structures... The DBI client is a bit more complicated, yes? The array was being rewritten. Then you didn't delcare your variables in the correct scope. understandable misperception on your part, as above Nope. If your array is being used in a loop, the contents of that array are changing when you don't want them to be changing, and instead want to be creating new arrays, you declared your array in the wrong scope. The sample I put out and the code I was working on are not identical. I declared my array in a different scope in the sample. Neither is wrong, they are different. I had to use an array copy push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array, push array ref Do you understand *why* that was necessary? Do you understand the difference between these two pieces of code? actually, I do indeed. In C++, the concept of a deep copy, vs shallow copy vs ref comes up all the time. I am just learning the syntax here, not programming itself perhaps you should be. . . ugh. insults. I am not trying to insulting you Paul, why resort to that? You have no idea how many programs I've written, well. I'd say its bad form to assume the worst or lowest in people. aha, a force to be reckoned with. Your point above about the docs is quite true. I dont have time to rewrite docs right now.. They do need work though You don't even have time to point out what you find to be wrong with them? But instead you do have time to create examples that you claim to be in the service of newbies, all the while saying that the docs are bad? I'd suggest you could do with attending a few more time- management seminars. please examine logic - rewriting core Perl docs vs a 10 line sample program This is sometimes appearing to be contentious No sometimes about it. Every post you've made thus far is contentious, and so I have answered in kind. no, I'd suggest you read it that way.. Its notoriously difficult to convey context and nuance in a few lines of ascii text. Please refer to your RTFM and then essentially yelling at me for a modest sample program. I am not backing down from your brow-beating. I posted a small working sample program then found out more about the context I was dealing with and attempted to discuss it in an unassuming manner. For this I get these responses. phooey! I'll be posting again from time to time, and probably going to OSCon. Happy to talk with you anytime. May not respond every time though. I think I am sensing a pattern here... best regards -Brian -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
Brian wrote: Hi All- I am trudging through some DBI, XML, etc.. I had a problem and was baffled by how to get at array elements out of a series of pushed array refs. But, by simplifying the problem, I found that the syntax I used was in error. here is the small sample, already debugged. Hope this helps someone... #!/usr/bin/perl my @tRespsA; my @fieldList = ( one, two, three, four ); my @r1 = ( 1, 2, 3, 4 ); my @r2 = ( 13, 14, 15, 16 ); my @r3 = ( 23, 24, 25, 26 ); push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; foreach my $tRowRef ( @tRespsA ) { my $tCnt=0; foreach my $tFld (@fieldList) { #if ( $tRowRef-[ $tCnt] eq ) { next; } print $tFld . =' . $tRowRef-[ $tCnt++ ] . ' \r; } } First of all, /always/ use strict; use warnings; I think you're still a little confused and thinking in another language - something like C? Your program works, sure, but iterating over a list of header names in the inner loop confuses things and won't provide a general solution. Also your cryptic variable names don't help. Take a look at this program and see what you think. The variable $i wouldn't be necessary at all if we weren't displaying header names as well as the array data. HTH, Rob use strict; use warnings; my @r1 = ( 1, 2, 3, 4 ); my @r2 = ( 13, 14, 15, 16 ); my @r3 = ( 23, 24, 25, 26 ); my @array2d; push @array2d, [EMAIL PROTECTED]; push @array2d, [EMAIL PROTECTED]; push @array2d, [EMAIL PROTECTED]; my @heads = qw/ one two three four /; foreach my $row (@array2d) { my $i = 0; foreach my $col (@$row) { printf %s = %s\n, $heads[$i++], $col; } print \n; } **OUTPUT** one = 1 two = 2 three = 3 four = 4 one = 13 two = 14 three = 15 four = 16 one = 23 two = 24 three = 25 four = 26 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 28, 12:00 am, [EMAIL PROTECTED] (Brian) wrote: I am trudging through some DBI, XML, etc.. I had a problem and was baffled by how to get at array elements out of a series of pushed array refs. What's to be baffled by? Have you read: perldoc perlreftut perldoc perllol perldoc perldsc ? Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 27, 9:00 pm, [EMAIL PROTECTED] (Brian) wrote: Hi All- I am trudging through some DBI, XML, etc.. I had a problem and was baffled by how to get at array elements out of a series of pushed array refs. But, by simplifying the problem, I found that the syntax I used was in error. here is the small sample, already debugged. Hope this helps someone... #!/usr/bin/perl my @tRespsA; my @fieldList = ( one, two, three, four ); my @r1 = ( 1, 2, 3, 4 ); my @r2 = ( 13, 14, 15, 16 ); my @r3 = ( 23, 24, 25, 26 ); push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; foreach my $tRowRef ( @tRespsA ) { my $tCnt=0; foreach my $tFld (@fieldList) { #if ( $tRowRef-[ $tCnt] eq ) { next; } print $tFld . =' . $tRowRef-[ $tCnt++ ] . ' \r; } } ok... a few things.. use strict and warnings is fine.. this was a small sample, perhaps allthe more important to include, actually, so you are right there... my variable names end with A for array and H for hash, start with a lower case t if they are used locally as temp variables, as in being written over each time through a loop. But this isn't a real program, so there isn't that much detail. In fact, I'd say that starting variable name s with array again and again is very hard to read. Resp is short for responses. These aren't actually static arrays, they come from iterating over the arrays returned by a DBI execute(). this is simplified for purposes of explanation. The reason for the construct is that those are names of fields used in formulating the Query string. var names rows and cols is fine... yes, I come from C yes, I have read all of those perl doc pages. You perl folks like to refer to whose pages a lot Paul, but in many cases they are not very well written as manuals [this is provably true] However, to give credit where credit is due, they are often full of interesting tidbits, and they do exist, so a lot of people have obviously tried to do a lot of explaining. ..and yes, I can make mistakes in the syntax, even though I have read through those. Practice makes perfect. I do appreciate the responses, as I don't really have anyone else to ask in my environment. thank you for that -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 27, 9:00 pm, [EMAIL PROTECTED] (Brian) wrote: Hi All- I am trudging through some DBI, XML, etc.. I had a problem and was baffled by how to get at array elements out of a series of pushed array refs. But, by simplifying the problem, I found that the syntax I used was in error. here is the small sample, already debugged. Hope this helps someone... #!/usr/bin/perl my @tRespsA; my @fieldList = ( one, two, three, four ); my @r1 = ( 1, 2, 3, 4 ); my @r2 = ( 13, 14, 15, 16 ); my @r3 = ( 23, 24, 25, 26 ); push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; foreach my $tRowRef ( @tRespsA ) { my $tCnt=0; foreach my $tFld (@fieldList) { #if ( $tRowRef-[ $tCnt] eq ) { next; } print $tFld . =' . $tRowRef-[ $tCnt++ ] . ' \r; } } oh yes, more important than all that minutiae... the push did not work for me in the working code. The array was being rewritten. I had to use an array copy push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array, push array ref -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 28, 3:22 pm, [EMAIL PROTECTED] (Brian) wrote: my variable names end with A for array and H for hash, Pointless. Variable names in Perl identify what kind of variable they are. @ for arrays, % for hashes. [ ] for accessing an element of a hash, { } for accessing element of a hash. yes, I have read all of those perl doc pages. You perl folks like to refer to whose pages a lot Paul, but in many cases they are not very well written as manuals [this is provably true] I heartily disagree, but if you think there's something that's not clear, either use the built-in `perlbug` utility, mention it to the perl5-porters list, or start a discussion about it on comp.lang.perl.misc. That's the point of the language being open- source, after all. Far more people are going to see those documents than will ever see your post to beginners@perl.org, so if you're aim is really to help out new Perl programmers, improving the documentation is the best approach. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Array of Array refs
On May 28, 3:26 pm, [EMAIL PROTECTED] (Brian) wrote: On May 27, 9:00 pm, [EMAIL PROTECTED] (Brian) wrote: Hi All- I am trudging through some DBI, XML, etc.. I had a problem and was baffled by how to get at array elements out of a series of pushed array refs. But, by simplifying the problem, I found that the syntax I used was in error. here is the small sample, already debugged. Hope this helps someone... #!/usr/bin/perl my @tRespsA; my @fieldList = ( one, two, three, four ); my @r1 = ( 1, 2, 3, 4 ); my @r2 = ( 13, 14, 15, 16 ); my @r3 = ( 23, 24, 25, 26 ); push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; push @tRespsA, [EMAIL PROTECTED]; foreach my $tRowRef ( @tRespsA ) { my $tCnt=0; foreach my $tFld (@fieldList) { #if ( $tRowRef-[ $tCnt] eq ) { next; } print $tFld . =' . $tRowRef-[ $tCnt++ ] . ' \r; } } oh yes, more important than all that minutiae... the push did not work for me in the working code. The push worked absolutely fine. It just didn't do what you wanted it to. Learning how to parse your problem should be your first step toward becoming a better programmer. The array was being rewritten. Then you didn't delcare your variables in the correct scope. As a general rule of thumb, declare your variables in the smallest scope possible. I had to use an array copy push @tRespsA, [ @r1 ]; ## copy contents to an anonymous array, push array ref Do you understand *why* that was necessary? Do you understand the difference between these two pieces of code? #Sample 1 my @array; my @BigArray; for (1..5) { @array = get_contents(); push @BigArray([EMAIL PROTECTED]); } #Sample 2; my @BigArray; for (1..5) { my @array = get_contents(); push @BigArray([EMAIL PROTECTED]); } In the first, you're reusing the same array over and over again. Each time, you're overwriting the contents of the array with new values, but you keep pushing references to THE SAME ARRAY onto @BigArray. In the second, you constantly create and destroy a brand new array each time through the for loop. The array gets created, it is filled with contents, a reference to that array is created, that reference is pushed onto @BigArray, and then that array goes out of scope. The contents are still accessable via the reference you pushed onto @BigArray, but via no other means. Next iteration, a brand new array is created. This might make it more clear: my @BigArray; my @array1; for (1..3) { my @array2; my $ref1 = [EMAIL PROTECTED]; my $ref2 = [EMAIL PROTECTED]; push @BigArray, $ref1, $ref2; print $ref1 - $ref2\n; } __END__ Output: ARRAY(0x37c38) - ARRAY(0x37c80) ARRAY(0x37c38) - ARRAY(0x2537c) ARRAY(0x37c38) - ARRAY(0x25094) Do you see now? By declaring only one instance of @array1, you're getting the same array every time you take a reference to it, so within the for loop, you keep changing the contents of the same array. @array2, on the other hand, is a completely new and unrelated array each time through the for loop. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: @array =to= $array
my $localtime; @$localtime{qw / second minute hour mday month year weekday yearday isdst /} = localtime(time); [1] A style nit: Speaking of nitpicks: my %localtime; If you DO need to iterate across all the indices for an array ( rarely necessary if you have designed your data correctly ) do it right: my @person = ( qw / Freddy Mary Georgetta Loretta Fred Lawrence / ); ## ## UNBEARABLY LAME C ## for ( my $index = 0 ; $index @person ; $index ++ ) { print unbearably bad $person[$index]\n; } ## ## MERELY BAD PERL ## for my $index ( 0 .. $#person ) { print merely bad $person[$index]\n; } This is not Bad Perl. This is a solution to a problem that the following code will not solve. Sometimes you need $index, sometimes you don't. ## ## GETTING INTO THE PERL STATE OF MIND ## for my $person (@person) { print Hello $person\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
my $localtime; @$localtime{qw / second minute hour mday month year weekday yearday isdst / } = localtime(time); [1] A style nit: Speaking of nitpicks: my %localtime; Assuming you are suggesting making that change, I'd reccomend against it, unless you wish to elicit the following error: Global symbol $localtime requires explicit package name at /tmp/try line 12. Global symbol $localtime requires explicit package name at /tmp/try line 14. Execution of /tmp/try aborted due to compilation errors. However, leaving it as: my $localtime; @$localtime{qw / second minute hour mday month year weekday yearday isdst /} = localtime(time); print Dumper $localtime; Will result in the following output: $VAR1 = { 'weekday' = 5, 'hour' = 11, 'month' = 5, 'second' = 25, 'isdst' = 1, 'minute' = 27, 'yearday' = 160, 'mday' = 10, 'year' = 105 }; ## ## MERELY BAD PERL ## for my $index ( 0 .. $#person ) { print merely bad $person[$index]\n; } This is not Bad Perl. This is a solution to a problem that the following code will not solve. Sometimes you need $index, sometimes you don't. I concede the point -- it is not Bad Perl. It is Bad Programming, using Perl as the platform for badness :) The subtle point I was trying to make was: Needing $index is frequently a sign of bad data design. Not always, mind you -- but it should be a flashing yellow light meaning am I doing something lame here? ## ## GETTING INTO THE PERL STATE OF MIND ## for my $person (@person) { print Hello $person\n; } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
On 6/10/05, Lawrence Statton [EMAIL PROTECTED] wrote: my $localtime; @$localtime{qw / second minute hour mday month year weekday yearday isdst / } = localtime(time); [1] A style nit: Speaking of nitpicks: my %localtime; Assuming you are suggesting making that change, I'd reccomend against it, unless you wish to elicit the following error: Global symbol $localtime requires explicit package name at /tmp/try line 12. Global symbol $localtime requires explicit package name at /tmp/try line 14. Execution of /tmp/try aborted due to compilation errors. Huh. So it does... that doesn't look like it's making a reference, though. I think I would write that as @{$localtime}{ ... } for clarity. Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. Heh. I like that. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
Huh. So it does... that doesn't look like it's making a reference, though. I think I would write that as @{$localtime}{ ... } for clarity. I only got out my snippy voice because it will be a snowy day here when I post example code to the list that hasn't actually been tested. It does not boost ones reputation as a source of Good Advice to give non-functioning examples, or suggest breaking known-working code. As to the clarity question. To my eyes, I find spurious {} tend to diminish rather than enhance, especially in common idioms, but I am wiling to accept it as a question of taste. Last night, my associate Will remarked, You are replying to someone who is confused by simple arrays with an example of a hashref-slice? And I responded: Hash slices (and their reference form) are such a wonderfully useful tool that they should be taught early and often. To that end: a humble example... - begin perl code -- #!/usr/bin/perl use strict; use warnings; use Data::Dumper qw(Dumper); # # Suppose we want to collect a little information # about a person # my %person; ## brute force technique # Gets the job done. Not very pretty. $person{first_name} = 'Lawrence'; $person{last_name} = 'Statton'; $person{ocupation} = 'Perl Hacker'; $person{city} = 'Guadalajara'; $person{country} = 'Mexico'; ## Slightly easier on the fingers version: %person = ( first_name = 'Lawrence', last_name = 'Statton', occupation = 'Perl Hacker', city = 'Guadalajara', country = 'Mexico' ); ## Here is the hash slice notation @person{ qw / first_name last_name occupation city country / } = ( 'Lawrence', 'Statton', 'Perl Hacker', 'Guadalajara', 'Mexico' ); ## what can I do with that? ## suppose you have a line from a text file of the form my $line = 'Lawrence:Statton:Perl Hacker:Guadalajara:Mexico'; @person{ qw / first_name last_name occupation city country/ } = split(':', $line ); ## ## NOW - let's do that whole thing, instead of using a hash (%person) we'll ## use a hashref $person ## my $person; $person-{first_name} = 'Lawrence'; $person-{last_name} = 'Statton'; $person-{ocupation} = 'Perl Hacker'; $person-{city} = 'Guadalajara'; $person-{country} = 'Mexico'; # or ... %$person = ( first_name = 'Lawrence', last_name = 'Statton', occupation = 'Perl Hacker', city = 'Guadalajara', country = 'Mexico' ); # even better ... $person = { first_name = 'Lawrence', last_name = 'Statton', occupation = 'Perl Hacker', city = 'Guadalajara', country = 'Mexico' }; ## now, combining the hashref with the hash-slice notation, we get: @$person{ qw / first_name last_name occupation city country/ } = split(':', $line ); # creates $person = { ... } ## compare it to a duplicate of the hash-slice -- hash notation @person{ qw / first_name last_name occupation city country/ } = split(':', $line ); # creates %person = ( ... ) -- end perl code --- Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. Heh. I like that. Thanks. A friend and I came up with that in a late-night debugging session in 1985. I think it was the same sleep-deprivation-induced stupor that produced the spoonerism 'iso-optilator'. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
On 6/10/05, Lawrence Statton [EMAIL PROTECTED] wrote: Huh. So it does... that doesn't look like it's making a reference, though. I think I would write that as @{$localtime}{ ... } for clarity. As to the clarity question. To my eyes, I find spurious {} tend to diminish rather than enhance, especially in common idioms, but I am wiling to accept it as a question of taste. When I mentioned clarity, I was referring to forcing precedence, even if it does not change the effect. I am, however, willing to accept the use of the word spurious as a question of opinion ;) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
[EMAIL PROTECTED] wrote: Hi All Hello, Prob: I want to convert all the elememts of an array to ascalor variable that I can use latter on in my code Each element of an array *is* a scalar so why do you think you need to do this? e.g. my @array=qw/balli johney bobby/; now here i need 3 different variable with the above values i.e. my $name1=balli ; my $name2=johney ; my $name3=bobby ; Soln so far: my ($i) ; for ($i=0; $i@array;$i++) { $name=$array[$i] ; # lost here is to how to have different $name each time also how can make these $names available out side the block as iam using strict. I am sure there is acleaver way of doing this. need help! } my ( $name1, $name2, $name3 ) = @array; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
Hi All Prob: I want to convert all the elememts of an array to ascalor variable that I can use latter on in my code e.g. my @array=qw/balli johney bobby/; now here i need 3 different variable with the above values i.e. my $name1=balli ; my $name2=johney ; my $name3=bobby ; No. You need very much *NOT* to do that, since you already have it built-in to Perl: $array[0] = 'balli'; $array[1] = 'johney'; $array[2] = 'bobby'; Problem solved. ANY time you see in your code something of the form $foo1, $foo2, $foo3 you should be using an array. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
[EMAIL PROTECTED] wrote: Each element of an array *is* a scalar so why do you think you need to do this? my ( $name1, $name2, $name3 ) = @array; True, But how would I know how many ($name) to use in the line above In my case for a perticular user I have some bad volumes which are @arrays and I want to do the comparison of these bad volumes e.g.size check mount point etc. and get the one that i think is appropriate. so for a user this array could be 1 or may be 5 or may be 2 Can you provide an example of the type of data you are working with and what you intend to do with it? John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
[EMAIL PROTECTED] wrote: Hi All Prob: I want to convert all the elememts of an array to ascalor variable that I can use latter on in my code e.g. my @array=qw/balli johney bobby/; now here i need 3 different variable with the above values i.e. my $name1=balli ; my $name2=johney ; my $name3=bobby ; Soln so far: my ($i) ; for ($i=0; $i@array;$i++) { $name=$array[$i] ; # lost here is to how to have different $name each time also how can make these my $i; for ($i=0; $i@arrayl $i++) { $name$i=$array[$i]; } That way you don't need to know in advance how many $name variables you need. $names available out side the block as iam using strict. I am sure there is acleaver way of doing this. need help! } -Bobby -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: @array =to= $array
[EMAIL PROTECTED] wrote: Hi All Prob: I want to convert all the elememts of an array to ascalor variable tha t I can use latter on in my code e.g. my @array=qw/balli johney bobby/; now here i need 3 different variable with the above values i.e. my $name1=balli ; my $name2=johney ; my $name3=bobby ; Soln so far: my ($i) ; for ($i=0; $i@array;$i++) { $name=$array[$i] ; # lost here is to how to have different $name each time a lso how can make these my $i; for ($i=0; $i@arrayl $i++) { $name$i=$array[$i]; } That way you don't need to know in advance how many $name variables you need. NO, NO, NO, NO, NO! First: Your code doesn't even work. Second: It is not actually SOLVING anything. variables of the form $name0, $name1, $name2 ... do not provide **ANY** functional difference from $array[0], $array[1], and $array[2] Write that on the blackboard fifty times. That is to say: $array[0] is a first-class citizen, just as good as $name0 -- iterating in the worst possible way[1] across an array to create copies of the data provides NO additional functionality to the code. I suspect if the original poster could read and write English he (she?) would be able to explain what was really needed. There *are* times when exploding an array into a series of separately named scalars *does* make sense. Think how stupid the following is: my @array; ($array[0], $array[1], $array[2], $array[3], $array[4], $array[5], $array[6], $array[7], $array[8] ) = localtime(time); But, if we rewrite it thusly... my ($second, $minute, $hour, $month_day, $month, $year, $weekday, $yearday, $isdst ) = localtime(time); ...the phrase makes sense. You are expanding the list of nine data returned by localtime into convenient mnemonic variables. (Getting further afield: I would probably never do THAT either -- if I have a series of nine things that are tightly coupled, I would want some better data structure to hold them. A hash or hashref, would be perfect.) my $localtime; @$localtime{qw / second minute hour mday month year weekday yearday isdst /} = localtime(time); [1] A style nit: Write Perl, not C. If you DO need to iterate across all the indices for an array ( rarely necessary if you have designed your data correctly ) do it right: my @person = ( qw / Freddy Mary Georgetta Loretta Fred Lawrence / ); ## ## UNBEARABLY LAME C ## for ( my $index = 0 ; $index @person ; $index ++ ) { print unbearably bad $person[$index]\n; } ## ## MERELY BAD PERL ## for my $index ( 0 .. $#person ) { print merely bad $person[$index]\n; } ## ## GETTING INTO THE PERL STATE OF MIND ## for my $person (@person) { print Hello $person\n; } Note that of the three techniques, the last is the only one that will work for all values of $[ (of course, anyone who sets $[ to any nonzero value deserves hot pokers in the eyes.) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: array against array regexp
Randal L. Schwartz [RLS], on , , 2005 at 05:47 (-0700) wrote about: RLS $wanted ||= $input =~ /$_/ for @filter; RLS $wanted; RLS } @input; RLS Note the use of ||= for short-circuiting the tests once a good filter RLS is found. You can't use return 1, because a grep/map block is not a RLS subroutine. very nice example, thanks a lot. -- How do you protect mail on web? I use http://www.2pu.net [Just give me the original history, please! -- Sam Beckett] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response