Re: Help on saving and retrieving data structures
Dear David, Thanks for throwing light on the topic by citing the security implications of executing eval and also suggesting the industry standard 'JSON' for interoperability. It is greatly appreciated. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- From: David Precious dav...@preshweb.co.uk To: Perl Beginners beginners@perl.org Sent: Friday, 13 September 2013 6:01 PM Subject: Re: Help on saving and retrieving data structures On Fri, 13 Sep 2013 19:10:58 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: I am saving the data structure to a file and retrieving it back again, but, when I 'use strict' it is giving the following error message Global symbol %game requires explicit package name Others have pointed out that the problem will be solved by adding a my %game before you eval the contents of the file. However, I personally would suggest serialising the data to JSON and storing that in the file, rather than using Data::Dumper - that way you've got your data in a more standard format in case you want to work with it with other tools, and also you're not eval'ing code, so if someone had decided to edit that file and add, say, system('rm -rf /') in it, you won't suddenly execute things you didn't expect. It can be as simple as saying use JSON to load JSON.pm, then using JSON::to_json(\%game) to generate JSON to write to the file, then saying e.g. my $game = JSON::from_json($json) (where $json is the JSON you just read in from the file). Alternatively, you could even look at DBM::Deep, which would abstract away the saving/loading for you (although you'd lose the bonus of having stored the data in a standard widely-supported format). It is working fine without 'strict'. Please help [code] #Code for saving the data structure use strict; use warnings; use Data::Dumper; my %game = ( worldcup = { series = WC 2011, play = [monday, wednesday, friday], players = [ {name = dhoni, role = captain, country = india}, {name = tendulkar, role = batsman, country = india}, {name = yuvraj, role = batsman, country = india} ], }, ipl = { series = Edition 5, play = [saturday, sunday], players = [ {name = sehwag, role = captain, country = india}, {name = muralidharan, role = batsman, country = srilanka}, {name = gayle, role = batsman, country = westindies} ], }, ); $Data::Dumper::Purity = 1; open my $fout, '', 'gameinfo.perldata' or die Cannot open file ($!); print $fout Data::Dumper-Dump([\%game], ['*game']); close $fout or die Cannot open file ($!); [/code] [code] #Code for reading the data structure. Please note that, I have disabled 'strict' and 'warnings' #use strict; #use warnings; use Data::Dumper; open my $fin, '', 'gameinfo.perldata' or die Cannot open file ($!); undef $/; #read in file all at once eval $fin; if($@) { die Can't recreate game data from gameinfo.perldata $@; } close $fin or die Cannot open file ($!); print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] I have also tried using 'do' using strict but in vain [code] #use strict; #use warnings; use Data::Dumper; do gameinfo.perldata or die Can't recreate gameinfo: $! $@; print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] [output] Name : muralidharan Role : batsman Country : srilanka [/output] My question is, how to use the above program with strict enabled? Thank you. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- -- David Precious (bigpresh) dav...@preshweb.co.uk http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
Hi Shaji, On Sat, 14 Sep 2013 16:19:26 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: Dear David, Thanks for throwing light on the topic by citing the security implications of executing eval and also suggesting the industry standard 'JSON' for interoperability. One should note that JSON is not the only sane option for serialisation and deserialisation in Perl. There are also: * https://metacpan.org/release/Storable - a binary format that is specific for Perl, which works very well. Using it avoids some JSON-specific limitations such as no references to scalars. Make sure you use nstore and friends instead of store. * http://blog.booking.com/the-next-sereal-is-coming.html - there's also this, but I have no experience with it. * There are other formats such as YAML, but YAML should be avoided because it's too complex and quirky (at least for serialisation). Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Optimising Code for Speed - http://shlom.in/optimise Whitespace in Python is not a problem: just lay out all the whitespace first, then add the code around it. — sizz on Freenode’s #perl Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
++Storable On 14 September 2013 09:39, Shlomi Fish shlo...@shlomifish.org wrote: Hi Shaji, On Sat, 14 Sep 2013 16:19:26 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: Dear David, Thanks for throwing light on the topic by citing the security implications of executing eval and also suggesting the industry standard 'JSON' for interoperability. One should note that JSON is not the only sane option for serialisation and deserialisation in Perl. There are also: * https://metacpan.org/release/Storable - a binary format that is specific for Perl, which works very well. Using it avoids some JSON-specific limitations such as no references to scalars. Make sure you use nstore and friends instead of store. * http://blog.booking.com/the-next-sereal-is-coming.html - there's also this, but I have no experience with it. * There are other formats such as YAML, but YAML should be avoided because it's too complex and quirky (at least for serialisation). Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Optimising Code for Speed - http://shlom.in/optimise Whitespace in Python is not a problem: just lay out all the whitespace first, then add the code around it. — sizz on Freenode’s #perl Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
Yes, I agree. Top answer! Can also consider YAML if you wanted to do a lot of manual editing of data, but probably JSON is best. Dr Jimi C Wills David Precious wrote in message news:20130913133147.0b88fbeb@columbia... On Fri, 13 Sep 2013 19:10:58 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: I am saving the data structure to a file and retrieving it back again, but, when I 'use strict' it is giving the following error message Global symbol %game requires explicit package name Others have pointed out that the problem will be solved by adding a my %game before you eval the contents of the file. However, I personally would suggest serialising the data to JSON and storing that in the file, rather than using Data::Dumper - that way you've got your data in a more standard format in case you want to work with it with other tools, and also you're not eval'ing code, so if someone had decided to edit that file and add, say, system('rm -rf /') in it, you won't suddenly execute things you didn't expect. It can be as simple as saying use JSON to load JSON.pm, then using JSON::to_json(\%game) to generate JSON to write to the file, then saying e.g. my $game = JSON::from_json($json) (where $json is the JSON you just read in from the file). Alternatively, you could even look at DBM::Deep, which would abstract away the saving/loading for you (although you'd lose the bonus of having stored the data in a standard widely-supported format). It is working fine without 'strict'. Please help [code] #Code for saving the data structure use strict; use warnings; use Data::Dumper; my %game = ( worldcup = { series = WC 2011, play = [monday, wednesday, friday], players = [ {name = dhoni, role = captain, country = india}, {name = tendulkar, role = batsman, country = india}, {name = yuvraj, role = batsman, country = india} ], }, ipl = { series = Edition 5, play = [saturday, sunday], players = [ {name = sehwag, role = captain, country = india}, {name = muralidharan, role = batsman, country = srilanka}, {name = gayle, role = batsman, country = westindies} ], }, ); $Data::Dumper::Purity = 1; open my $fout, '', 'gameinfo.perldata' or die Cannot open file ($!); print $fout Data::Dumper-Dump([\%game], ['*game']); close $fout or die Cannot open file ($!); [/code] [code] #Code for reading the data structure. Please note that, I have disabled 'strict' and 'warnings' #use strict; #use warnings; use Data::Dumper; open my $fin, '', 'gameinfo.perldata' or die Cannot open file ($!); undef $/; #read in file all at once eval $fin; if($@) { die Can't recreate game data from gameinfo.perldata $@; } close $fin or die Cannot open file ($!); print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] I have also tried using 'do' using strict but in vain [code] #use strict; #use warnings; use Data::Dumper; do gameinfo.perldata or die Can't recreate gameinfo: $! $@; print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] [output] Name : muralidharan Role : batsman Country : srilanka [/output] My question is, how to use the above program with strict enabled? Thank you. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- -- David Precious (bigpresh) dav...@preshweb.co.uk http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook www.preshweb.co.uk/cpanwww.preshweb.co.uk/github -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Help on saving and retrieving data structures
Dear Perlers, I am saving the data structure to a file and retrieving it back again, but, when I 'use strict' it is giving the following error message Global symbol %game requires explicit package name It is working fine without 'strict'. Please help [code] #Code for saving the data structure use strict; use warnings; use Data::Dumper; my %game = ( worldcup = { series = WC 2011, play = [monday, wednesday, friday], players = [ {name = dhoni, role = captain, country = india}, {name = tendulkar, role = batsman, country = india}, {name = yuvraj, role = batsman, country = india} ], }, ipl = { series = Edition 5, play = [saturday, sunday], players = [ {name = sehwag, role = captain, country = india}, {name = muralidharan, role = batsman, country = srilanka}, {name = gayle, role = batsman, country = westindies} ], }, ); $Data::Dumper::Purity = 1; open my $fout, '', 'gameinfo.perldata' or die Cannot open file ($!); print $fout Data::Dumper-Dump([\%game], ['*game']); close $fout or die Cannot open file ($!); [/code] [code] #Code for reading the data structure. Please note that, I have disabled 'strict' and 'warnings' #use strict; #use warnings; use Data::Dumper; open my $fin, '', 'gameinfo.perldata' or die Cannot open file ($!); undef $/; #read in file all at once eval $fin; if($@) { die Can't recreate game data from gameinfo.perldata $@; } close $fin or die Cannot open file ($!); print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] I have also tried using 'do' using strict but in vain [code] #use strict; #use warnings; use Data::Dumper; do gameinfo.perldata or die Can't recreate gameinfo: $! $@; print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] [output] Name : muralidharan Role : batsman Country : srilanka [/output] My question is, how to use the above program with strict enabled? Thank you. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. ---
Re: Help on saving and retrieving data structures
On Fri, Sep 13, 2013 at 07:10:58PM +0800, *Shaji Kalidasan* wrote: Dear Perlers, I am saving the data structure to a file and retrieving it back again, but, when I 'use strict' it is giving the following error message Global symbol %game requires explicit package name This message means you haven't declared the game hash. In the first section of code you declare the game hash and populate it. In the second section of code you haven't decared the game hash at all. You can declare it using my %game; Then you actually have to populate it with the data you have read from the file before you can get any useful output. You might want to read up on autovivication - particularly with reference to hashes. ( http://en.wikipedia.org/wiki/Autovivification ) regards L. It is working fine without 'strict'. Please help [code] #Code for saving the data structure use strict; use warnings; use Data::Dumper; my %game = ( worldcup = { series = WC 2011, play = [monday, wednesday, friday], players = [ {name = dhoni, role = captain, country = india}, {name = tendulkar, role = batsman, country = india}, {name = yuvraj, role = batsman, country = india} ], }, ipl = { series = Edition 5, play = [saturday, sunday], players = [ {name = sehwag, role = captain, country = india}, {name = muralidharan, role = batsman, country = srilanka}, {name = gayle, role = batsman, country = westindies} ], }, ); $Data::Dumper::Purity = 1; open my $fout, '', 'gameinfo.perldata' or die Cannot open file ($!); print $fout Data::Dumper-Dump([\%game], ['*game']); close $fout or die Cannot open file ($!); [/code] [code] #Code for reading the data structure. Please note that, I have disabled 'strict' and 'warnings' #use strict; #use warnings; use Data::Dumper; open my $fin, '', 'gameinfo.perldata' or die Cannot open file ($!); undef $/; #read in file all at once eval $fin; if($@) { die Can't recreate game data from gameinfo.perldata $@; } close $fin or die Cannot open file ($!); print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] I have also tried using 'do' using strict but in vain [code] #use strict; #use warnings; use Data::Dumper; do gameinfo.perldata or die Can't recreate gameinfo: $! $@; print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] [output] Name : muralidharan Role : batsman Country : srilanka [/output] My question is, how to use the above program with strict enabled? Thank you. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
Hi Lesley, Thanks for the explanation and pointing me to further resources (Wikipedia). best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- From: 'lesleyb' lesl...@herlug.org.uk To: beginners@perl.org Sent: Friday, 13 September 2013 5:26 PM Subject: Re: Help on saving and retrieving data structures On Fri, Sep 13, 2013 at 07:10:58PM +0800, *Shaji Kalidasan* wrote: Dear Perlers, I am saving the data structure to a file and retrieving it back again, but, when I 'use strict' it is giving the following error message Global symbol %game requires explicit package name This message means you haven't declared the game hash. In the first section of code you declare the game hash and populate it. In the second section of code you haven't decared the game hash at all. You can declare it using my %game; Then you actually have to populate it with the data you have read from the file before you can get any useful output. You might want to read up on autovivication - particularly with reference to hashes. ( http://en.wikipedia.org/wiki/Autovivification ) regards L. It is working fine without 'strict'. Please help [code] #Code for saving the data structure use strict; use warnings; use Data::Dumper; my %game = ( worldcup = { series = WC 2011, play = [monday, wednesday, friday], players = [ {name = dhoni, role = captain, country = india}, {name = tendulkar, role = batsman, country = india}, {name = yuvraj, role = batsman, country = india} ], }, ipl = { series = Edition 5, play = [saturday, sunday], players = [ {name = sehwag, role = captain, country = india}, {name = muralidharan, role = batsman, country = srilanka}, {name = gayle, role = batsman, country = westindies} ], }, ); $Data::Dumper::Purity = 1; open my $fout, '', 'gameinfo.perldata' or die Cannot open file ($!); print $fout Data::Dumper-Dump([\%game], ['*game']); close $fout or die Cannot open file ($!); [/code] [code] #Code for reading the data structure. Please note that, I have disabled 'strict' and 'warnings' #use strict; #use warnings; use Data::Dumper; open my $fin, '', 'gameinfo.perldata' or die Cannot open file ($!); undef $/; #read in file all at once eval $fin; if($@) { die Can't recreate game data from gameinfo.perldata $@; } close $fin or die Cannot open file ($!); print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] I have also tried using 'do' using strict but in vain [code] #use strict; #use warnings; use Data::Dumper; do gameinfo.perldata or die Can't recreate gameinfo: $! $@; print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] [output] Name : muralidharan Role : batsman Country : srilanka [/output] My question is, how to use the above program with strict enabled? Thank you. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
Hi Jimi, Thanks for your explanation and pointing me exactly where I am going wrong. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- From: Dr Jimi-Carlo Bukowski-Wills jbwi...@staffmail.ed.ac.uk To: *Shaji Kalidasan* shajiin...@yahoo.com Sent: Friday, 13 September 2013 5:12 PM Subject: Re: Help on saving and retrieving data structures Hi It’s because the output file has no (my) in it and you haven’t declared the variable scope in the code that reads the data. Try adding my %game; before the line eval $fin; The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
Re: Help on saving and retrieving data structures
On Fri, 13 Sep 2013 19:10:58 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: I am saving the data structure to a file and retrieving it back again, but, when I 'use strict' it is giving the following error message Global symbol %game requires explicit package name Others have pointed out that the problem will be solved by adding a my %game before you eval the contents of the file. However, I personally would suggest serialising the data to JSON and storing that in the file, rather than using Data::Dumper - that way you've got your data in a more standard format in case you want to work with it with other tools, and also you're not eval'ing code, so if someone had decided to edit that file and add, say, system('rm -rf /') in it, you won't suddenly execute things you didn't expect. It can be as simple as saying use JSON to load JSON.pm, then using JSON::to_json(\%game) to generate JSON to write to the file, then saying e.g. my $game = JSON::from_json($json) (where $json is the JSON you just read in from the file). Alternatively, you could even look at DBM::Deep, which would abstract away the saving/loading for you (although you'd lose the bonus of having stored the data in a standard widely-supported format). It is working fine without 'strict'. Please help [code] #Code for saving the data structure use strict; use warnings; use Data::Dumper; my %game = ( worldcup = { series = WC 2011, play = [monday, wednesday, friday], players = [ {name = dhoni, role = captain, country = india}, {name = tendulkar, role = batsman, country = india}, {name = yuvraj, role = batsman, country = india} ], }, ipl = { series = Edition 5, play = [saturday, sunday], players = [ {name = sehwag, role = captain, country = india}, {name = muralidharan, role = batsman, country = srilanka}, {name = gayle, role = batsman, country = westindies} ], }, ); $Data::Dumper::Purity = 1; open my $fout, '', 'gameinfo.perldata' or die Cannot open file ($!); print $fout Data::Dumper-Dump([\%game], ['*game']); close $fout or die Cannot open file ($!); [/code] [code] #Code for reading the data structure. Please note that, I have disabled 'strict' and 'warnings' #use strict; #use warnings; use Data::Dumper; open my $fin, '', 'gameinfo.perldata' or die Cannot open file ($!); undef $/; #read in file all at once eval $fin; if($@) { die Can't recreate game data from gameinfo.perldata $@; } close $fin or die Cannot open file ($!); print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] I have also tried using 'do' using strict but in vain [code] #use strict; #use warnings; use Data::Dumper; do gameinfo.perldata or die Can't recreate gameinfo: $! $@; print Name : , $game{ipl}-{players}-[1]-{name}, \n; print Role : , $game{ipl}-{players}-[1]-{role}, \n; print Country : , $game{ipl}-{players}-[1]-{country}, \n; [/code] [output] Name : muralidharan Role : batsman Country : srilanka [/output] My question is, how to use the above program with strict enabled? Thank you. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- -- David Precious (bigpresh) dav...@preshweb.co.uk http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook www.preshweb.co.uk/cpanwww.preshweb.co.uk/github -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
On Fri, Sep 13, 2013 at 01:31:47PM +0100, David Precious wrote: On Fri, 13 Sep 2013 19:10:58 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: I am saving the data structure to a file and retrieving it back again, but, when I 'use strict' it is giving the following error message Global symbol %game requires explicit package name Others have pointed out that the problem will be solved by adding a my %game before you eval the contents of the file. However, I personally would suggest serialising the data to JSON and storing that in the file, rather than using Data::Dumper - that way you've got your data in a more standard format in case you want to work with it with other tools, and also you're not eval'ing code, so if someone had decided to edit that file and add, say, system('rm -rf /') in it, you won't suddenly execute things you didn't expect. It can be as simple as saying use JSON to load JSON.pm, then using JSON::to_json(\%game) to generate JSON to write to the file, then saying e.g. my $game = JSON::from_json($json) (where $json is the JSON you just read in from the file). Alternatively, you could even look at DBM::Deep, which would abstract away the saving/loading for you (although you'd lose the bonus of having stored the data in a standard widely-supported format). A shame we can't rate answers on beginners but that is an excellent answer and really informative. Thanks David. snip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl Module to parse any other Perl script/module to fetch stats about data-structures
Parag == Parag Kalra paragka...@gmail.com writes: Parag Do we have any Perl module which can parse any other perl script Parag (or module) and fetch information like total number of arrays Parag being used, total number of hashes, total number of scalar Parag variables etc and size information (like total elements, total Parag keys etc) for each data structure in that perl script or may be Parag another perl module It's not possible to completely parse Perl statically [1], although PPI [2] is pretty close and usable for most programs. [1] http://www.perlmonks.org/index.pl?node_id=44722 [2] http://search.cpan.org/~adamk/PPI-1.215/lib/PPI.pm -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 mer...@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.posterous.com/ for Smalltalk discussion -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl Module to parse any other Perl script/module to fetch stats about data-structures
On 12-02-08 05:20 PM, Parag Kalra wrote: Do we have any Perl module which can parse any other perl script (or module) and fetch information like total number of arrays being used, total number of hashes, total number of scalar variables etc and size information (like total elements, total keys etc) for each data structure in that perl script or may be another perl module Thanks, Parag I haven't found a module to do exactly what you want, but B::Xref may do for a start: perl -MO=Xref myscript See `perldoc B::Xref` for details. -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Perl Module to parse any other Perl script/module to fetch stats about data-structures
Do we have any Perl module which can parse any other perl script (or module) and fetch information like total number of arrays being used, total number of hashes, total number of scalar variables etc and size information (like total elements, total keys etc) for each data structure in that perl script or may be another perl module Thanks, Parag
Re: Perl Module to parse any other Perl script/module to fetch stats about data-structures
On 2012.02.08 17:20, Parag Kalra wrote: Do we have any Perl module which can parse any other perl script (or module) and fetch information like total number of arrays being used, total number of hashes, total number of scalar variables etc and size information (like total elements, total keys etc) for each data structure in that perl script or may be another perl module I've written such to identify subs within my own apps for which I've written numerous interconnected modules, but never had the need to see individual elements. Even my own internal traces didn't follow single elements. I would suspect that this would be something that would have to run against the file itself, even prior to compile. Curious task. Might I ask what the purpose of your desire is? Steve -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl Module to parse any other Perl script/module to fetch stats about data-structures
于 2012-2-9 10:15, Steve Bertrand 写道: I would suspect that this would be something that would have to run against the file itself, even prior to compile. Curious task. Might I ask what the purpose of your desire is? I am also not sure what's the special purpuse of the OP. Some modules on CPAN under the namespace of Devel::* may help him. Jeff. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl Module to parse any other Perl script/module to fetch stats about data-structures
By data-structures being used, I mean the data structures that are actually *declared* in the script. And is it possible to find at least the data-structures stats of the current script that is getting executed if no parser is available. For example if I have a big script or module and if I want to find total arrays, hashes and variables declared in that script. Is there any special variable where this info is stored. Thanks, Parag On Wed, Feb 8, 2012 at 6:31 PM, Jeff Peng p...@staff.dnsbed.com wrote: 于 2012-2-9 10:15, Steve Bertrand 写道: I would suspect that this would be something that would have to run against the file itself, even prior to compile. Curious task. Might I ask what the purpose of your desire is? I am also not sure what's the special purpuse of the OP. Some modules on CPAN under the namespace of Devel::* may help him. Jeff. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl Module to parse any other Perl script/module to fetch stats about data-structures
于 2012-2-9 15:21, Parag Kalra 写道: By data-structures being used, I mean the data structures that are actually *declared* in the script. And is it possible to find at least the data-structures stats of the current script that is getting executed if no parser is available. For example if I have a big script or module and if I want to find total arrays, hashes and variables declared in that script. Is there any special variable where this info is stored. I'm not sure, but you may take a look at this module: http://search.cpan.org/~flora/perl-5.14.2/ext/Devel-Peek/Peek.pm http://search.cpan.org/%7Eflora/perl-5.14.2/ext/Devel-Peek/Peek.pm HTH.
Efficient way to compare 2 data structures particularly hash
Hi, I am evaluating different approaches to compare data structures in Perl - i.e whether they are same or not. I am planning to start wit: 1. Data::Compare 2. And then found this - http://stackoverflow.com/questions/1273616/how-do-i-compare-two-hashes-in-perl-without-using-datacompare Any other options or suggestions? I would like to use the most efficient. ~Parag
Re: Efficient way to compare 2 data structures particularly hash
On 30/04/2011 19:22, Parag Kalra wrote: Hi, I am evaluating different approaches to compare data structures in Perl - i.e whether they are same or not. I am planning to start wit: 1. Data::Compare 2. And then found this - http://stackoverflow.com/questions/1273616/how-do-i-compare-two-hashes-in-perl-without-using-datacompare Any other options or suggestions? I would like to use the most efficient. I suggest you take a look at perldoc -q How do I test whether two arrays or hashes are equal? But have you established that Data::Compare is too inefficient? I imagine a lot has gone into making it work as well as possible, and if it is not causing a bottleneck problem then you should go ahead and just use it. Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Efficient way to compare 2 data structures particularly hash
On Sat, Apr 30, 2011 at 11:22:57AM -0700, Parag Kalra wrote: Hi, I am evaluating different approaches to compare data structures in Perl - i.e whether they are same or not. I am planning to start wit: 1. Data::Compare 2. And then found this - http://stackoverflow.com/questions/1273616/how-do-i-compare-two-hashes-in-perl-without-using-datacompare Any other options or suggestions? I would like to use the most efficient. Most efficient in terms of what? Data::Compare seems to do what you want. It is established, tested, maintained, and has no outstanding reported bugs. Since you know about it, is there any reason to look any further? Why evaluate different approaches? I suggest that the most efficient use of your time would be to use Data::Compare, until you have a reason not to. Then you can wonder what to use instead. -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Rendering data structures using CGI::Application
I've been stuck for the last three hours trying to render the following data structure into my browser. I'm using CGI::Application, and CGI::Application::Plugin::PageBuilder. No matter what I do or try, I can not loop the structure into a template variable. I'm literally at wits end, and am considering just figuring out a way to 'print' it out. Any strong kick to the teeth as a boost would be most appreciated. This is my data structure, and below shows that the particular module that produces this data can be queried to produce more human readable output: $error-dump_all(); $VAR1 = \[ 'quantity is undefined, zero or illegal', 'amount is undefined or illegal', 'payment is undefined or illegal' ]; $VAR2 = \[ { 'sub' = undef, 'filename' = 'tests/purchase.pl', 'line' = 43, 'package' = 'main' }, { 'sub' = 'ISP::Transac::create_transaction', 'filename' = '../ISP/Transac.pm', 'line' = 32, 'package' = 'ISP::Transac' }, { 'sub' = 'ISP::Sanity::transaction_data', 'filename' = '../ISP/Sanity.pm', 'line' = 55, 'package' = 'ISP::Sanity' } ]; # I can print it out nicely on the command line, but not in the browser: my @messages= $error-get_messages(); my @stack = $error-get_stack(); print join (\n, @messages); for (@stack) { while ( my ($key, $value) = each %{$_}) { print $key = $value\n if defined $key and defined $value; } } } Is there an easy way to render this to the browser, while keeping HTML out of my code? Steve smime.p7s Description: S/MIME Cryptographic Signature
Re: Rendering data structures using CGI::Application
How do you want the output to look? If you write in a sample output, I could help with the template to generate that output Regards On Mon, 2009-06-29 at 22:56 -0400, Steve Bertrand wrote: I've been stuck for the last three hours trying to render the following data structure into my browser. I'm using CGI::Application, and CGI::Application::Plugin::PageBuilder. No matter what I do or try, I can not loop the structure into a template variable. I'm literally at wits end, and am considering just figuring out a way to 'print' it out. Any strong kick to the teeth as a boost would be most appreciated. This is my data structure, and below shows that the particular module that produces this data can be queried to produce more human readable output: $error-dump_all(); $VAR1 = \[ 'quantity is undefined, zero or illegal', 'amount is undefined or illegal', 'payment is undefined or illegal' ]; $VAR2 = \[ { 'sub' = undef, 'filename' = 'tests/purchase.pl', 'line' = 43, 'package' = 'main' }, { 'sub' = 'ISP::Transac::create_transaction', 'filename' = '../ISP/Transac.pm', 'line' = 32, 'package' = 'ISP::Transac' }, { 'sub' = 'ISP::Sanity::transaction_data', 'filename' = '../ISP/Sanity.pm', 'line' = 55, 'package' = 'ISP::Sanity' } ]; # I can print it out nicely on the command line, but not in the browser: my @messages= $error-get_messages(); my @stack = $error-get_stack(); print join (\n, @messages); for (@stack) { while ( my ($key, $value) = each %{$_}) { print $key = $value\n if defined $key and defined $value; } } } Is there an easy way to render this to the browser, while keeping HTML out of my code? Steve
Re: Rendering data structures using CGI::Application
Gurunandan R. Bhat wrote: How do you want the output to look? If you write in a sample output, I could help with the template to generate that output I would be extremely delighted to even get the following rendered for now, given the Data::Dumper output below. If I had a decent example, then I'd be able to learn from it, and go from there: --- output displayed as I'd see it in the browser--- Error messages: quantity is undefined, zero or illegal amount is undefined or illegal payment is undefined or illegal Stack trace: sub = undef; filename = tests/purchase.pl line = 43 package = main sub = ISP::Transac::create_transaction filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm line = 32 package = ISP::Transac sub = ISP::Sanity::transaction_data filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm line = 55 package = ISP::Sanity --- end browser output --- Here again is the actual data. $VAR1 is an array of the error messages, and $VAR2 is an array, where each element is a hash which represents a level in a stack trace. $VAR1 = \[ 'quantity is undefined, zero or illegal', 'amount is undefined or illegal', 'payment is undefined or illegal' ]; $VAR2 = \[ { 'sub' = undef, 'filename' = 'tests/purchase.pl', 'line' = 43, 'package' = 'main' }, { 'sub' = 'ISP::Transac::create_transaction', 'filename' = '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm', 'line' = 32, 'package' = 'ISP::Transac' }, { 'sub' = 'ISP::Sanity::transaction_data', 'filename' = '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm', 'line' = 55, 'package' = 'ISP::Sanity' } ]; Thank you! Steve smime.p7s Description: S/MIME Cryptographic Signature
Re: Rendering data structures using CGI::Application
Sure. I think you would like to have the error messages next to the sub that threw them, but here is what you want: (NOT TESTED!!) In your module: my $messages = [ {text = 'quantity is undefined, zero or illegal'}, {text = 'amount is undefined or illegal'}, {text = 'payment is undefined or illegal'}, ]; my $stack = [ { sub = undef, filename = 'tests/purchase.pl', line = 43, package = 'main', }, { sub = 'ISP::Transac::create_transaction', filename = '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm', line = 32, package = 'ISP::Transac', }, { sub = 'ISP::Sanity::transaction_data', filename = '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm', line = 55, package = 'ISP::Sanity', }, ]; my $tpl = $app-load_tmpl('templatefile.tpl', die_on_bad_params = 0); $tpl-param( MESSAGES = $messages, STACK = $stack, ); return $tpl-output; In your template file ul !-- TMPL_LOOP NAME=MESSAGES -- li!-- TMPL_VAR NAME=TEXT --/li !-- /TMPL_LOOP -- /ul ul !-- TMPL_LOOP NAME=STACK -- li ul !-- TMPL_VAR NAME=sub --br / !-- TMPL_VAR NAME=filename --br / !-- TMPL_VAR NAME=line --br / !-- TMPL_VAR NAME=package -- /ul /li !-- /TMPL_LOOP -- HTH On Mon, 2009-06-29 at 23:51 -0400, Steve Bertrand wrote: Gurunandan R. Bhat wrote: How do you want the output to look? If you write in a sample output, I could help with the template to generate that output I would be extremely delighted to even get the following rendered for now, given the Data::Dumper output below. If I had a decent example, then I'd be able to learn from it, and go from there: --- output displayed as I'd see it in the browser--- Error messages: quantity is undefined, zero or illegal amount is undefined or illegal payment is undefined or illegal Stack trace: sub = undef; filename = tests/purchase.pl line = 43 package = main sub = ISP::Transac::create_transaction filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm line = 32 package = ISP::Transac sub = ISP::Sanity::transaction_data filename = /usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm line = 55 package = ISP::Sanity --- end browser output --- Here again is the actual data. $VAR1 is an array of the error messages, and $VAR2 is an array, where each element is a hash which represents a level in a stack trace. $VAR1 = \[ 'quantity is undefined, zero or illegal', 'amount is undefined or illegal', 'payment is undefined or illegal' ]; $VAR2 = \[ { 'sub' = undef, 'filename' = 'tests/purchase.pl', 'line' = 43, 'package' = 'main' }, { 'sub' = 'ISP::Transac::create_transaction', 'filename' = '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Transac.pm', 'line' = 32, 'package' = 'ISP::Transac' }, { 'sub' = 'ISP::Sanity::transaction_data', 'filename' = '/usr/local/lib/perl5/site_perl/5.8.9/ISP/Sanity.pm', 'line' = 55, 'package' = 'ISP::Sanity' } ]; Thank you! Steve -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Rendering data structures using CGI::Application
Gurunandan R. Bhat wrote: Sure. I think you would like to have the error messages next to the sub that threw them, but here is what you want: (NOT TESTED!!) ..woot!!! Thank you ever so much. Although I inserted the data statically into my _render_error() method, the result is positive! Other than site-specific changes, your code was completely copy and paste. Kudos. Now that I have a baseline to work with, it will be trivial for me to follow this example to insert my data dynamically, and format it as I please. Cheers, Steve ps. I've stuck with my 'passing the $error around' for now. I've been finding that going over older code (a couple of years old) and being able to rewrite it at 20% its original size is a good thing. Even if my error checking is the hard way, it's still 'a' way, and one I'll eventually learn a lesson from ;) smime.p7s Description: S/MIME Cryptographic Signature
RE: references to complex data structures
-Original Message- From: Rob Dixon [mailto:[EMAIL PROTECTED] Sent: Monday, January 14, 2008 8:59 PM To: beginners@perl.org Cc: Kevin Viel Subject: Re: references to complex data structures Kevin Viel wrote: Consider the following anonymous array of anonymous arrays: my $a = [ [ 00 , 01 ] , [ 10 , 11 ] ] ; I can print the first element of the first anonymous array: print $$a[ 0 ][ 0 ]\n ; or, equivalently, I can explicit use bracers to dereference it: print ${$a}[ 0 ][ 0 ]\n ; OK But they're 'braces' :) Should I not need two pairs of bracers? The 'missing' pair of braces is because you're using both of the two different syntaxes for indexing a referenced array. The first element of the outer array is either ${$a}[0] (which can be written $$a[0] without ambiguity) or $a-[0] These are references to the first inner array, which can also be indexed in either way. So we have four ways of reaching the first element of the first inner array: ${${$a}[0]}[0] ${$a}[0]-[0] ${$a-[0]}[0] $a-[0]-[0] In addition, Perl allows us to remove the arrow operator between pairs of closing and opening brackets or braces - ][ or }{, so the second and last options can be written ${$a}[0][0] $a-[0][0] and so your code turns out to be the same as the first of these two. If you use the same syntax for indexing in both cases then the second pair of braces reappears. It is also the same as your final example below. $a is a reference to the anonymous array. The elements of the anonymous arrays are references to anonymous arrays, correct? They are references to arrays, yes. They may be named arrays or anonymous ones. The following seems to achieve this result: print ${${$a}[ 0 ]}[ 0 ]\n ; Is the outmost pair of bracers with the appropriate symbol ($, @, %) the default? If so, how does perl select the correct symbol? I'm not sure what you mean here: you have explicit dollar signs in this code for both dereferences. Perl has no default way of handling references, but it will complain if you try to use a reference to one type of data as something different. I realize that it is seemingly moot, but it may help my understanding of more complex structures, like hash of arrays or hash of hases. Don't get to like them to the extent that you use them when your data isn't shaped that way. They are a way of expressing hierarchical data only. Thanks for another informative post. My data are hierarchical. I have 22 files, one for each autosome (non-sex chromosome, sex chromosomes being the X and Y-apologies is this is unnecessary to state). In each file I have markers and their locations. The first thing I want to do is to be sure that that a marker is unique to a chromosome: if ( exists $marker_hash{ $marker } ) { print Collision: Chromosome: $Chr Marker: $marker\n ; } else{ $marker_hash{ $marker } = $chr ; } However, I would also like to do two things: collect the chromosomes on which a markers exists and create a ordered database table that reports the chromosome , marker , and location. The outer most object would be a hash, the keys of which are the markers. The values of this hash could be a hash with keys being chromosome and the values being an array of locations. Coding aside, through which I still must suffer, does a complex data structure seem appropriate to this problem or should I be considerng another approach? Kind regards, Kevin Kevin Viel, PhD Post-doctoral fellow Department of Genetics Southwest Foundation for Biomedical Research San Antonio, TX 78227 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
references to complex data structures
Consider the following anonymous array of anonymous arrays: my $a = [ [ 00 , 01 ] , [ 10 , 11 ] ] ; I can print the first element of the first anonymous array: print $$a[ 0 ][ 0 ]\n ; or, equivalently, I can explicit use bracers to dereference it: print ${$a}[ 0 ][ 0 ]\n ; Should I not need two pairs of bracers? $a is a reference to the anonymous array. The elements of the anonymous arrays are references to anonymous arrays, correct? The following seems to achieve this result: print ${${$a}[ 0 ]}[ 0 ]\n ; Is the outmost pair of bracers with the appropriate symbol ($, @, %) the default? If so, how does perl select the correct symbol? I realize that it is seemingly moot, but it may help my understanding of more complex structures, like hash of arrays or hash of hases. Thanks, Kevin Kevin Viel, PhD Post-doctoral fellow Department of Genetics Southwest Foundation for Biomedical Research San Antonio, TX 78227 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: references to complex data structures
Kevin Viel wrote: Consider the following anonymous array of anonymous arrays: my $a = [ [ 00 , 01 ] , [ 10 , 11 ] ] ; I can print the first element of the first anonymous array: print $$a[ 0 ][ 0 ]\n ; or, equivalently, I can explicit use bracers to dereference it: print ${$a}[ 0 ][ 0 ]\n ; Should I not need two pairs of bracers? $a is a reference to the anonymous array. The elements of the anonymous arrays are references to anonymous arrays, correct? The following seems to achieve this result: print ${${$a}[ 0 ]}[ 0 ]\n ; Well, TIMTOWDI. :) I would have said: print $a-[0][0]\n; Have you studied the applicable Perl docs? perldoc perllol perldoc perldsc perldoc perlref perldoc perlreftut They explain it better than I ever would be able to... If you seek help here to better understand references, you'd better do so via specific questions on one or more of those documents. -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: references to complex data structures
Kevin Viel wrote: Consider the following anonymous array of anonymous arrays: my $a = [ [ 00 , 01 ] , [ 10 , 11 ] ] ; I can print the first element of the first anonymous array: print $$a[ 0 ][ 0 ]\n ; or, equivalently, I can explicit use bracers to dereference it: print ${$a}[ 0 ][ 0 ]\n ; OK But they're 'braces' :) Should I not need two pairs of bracers? The 'missing' pair of braces is because you're using both of the two different syntaxes for indexing a referenced array. The first element of the outer array is either ${$a}[0] (which can be written $$a[0] without ambiguity) or $a-[0] These are references to the first inner array, which can also be indexed in either way. So we have four ways of reaching the first element of the first inner array: ${${$a}[0]}[0] ${$a}[0]-[0] ${$a-[0]}[0] $a-[0]-[0] In addition, Perl allows us to remove the arrow operator between pairs of closing and opening brackets or braces - ][ or }{, so the second and last options can be written ${$a}[0][0] $a-[0][0] and so your code turns out to be the same as the first of these two. If you use the same syntax for indexing in both cases then the second pair of braces reappears. It is also the same as your final example below. $a is a reference to the anonymous array. The elements of the anonymous arrays are references to anonymous arrays, correct? They are references to arrays, yes. They may be named arrays or anonymous ones. The following seems to achieve this result: print ${${$a}[ 0 ]}[ 0 ]\n ; Is the outmost pair of bracers with the appropriate symbol ($, @, %) the default? If so, how does perl select the correct symbol? I'm not sure what you mean here: you have explicit dollar signs in this code for both dereferences. Perl has no default way of handling references, but it will complain if you try to use a reference to one type of data as something different. I realize that it is seemingly moot, but it may help my understanding of more complex structures, like hash of arrays or hash of hases. Don't get to like them to the extent that you use them when your data isn't shaped that way. They are a way of expressing hierarchical data only. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Accessing packed data structures
From: Dharshana Eswaran [EMAIL PROTECTED] I was going thro the topic Accessing packed data structures in the Perl Complete Reference Book. I came across this example: struct utmp { char ut_user[8]; /* User login name */ char ut_id[4]; /* /etc/inittab id */ char ut_line[12]; /* device name */ short ut_pid; /* process ID */ short ut_type; /* type of entry */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS. */ time_t ut_time; /* time entry was made */ }; For the above structure: The pack template given was a8a4a12l. ... You might also want to have a look at Inline::Struct http://search.cpan.org/~neilw/Inline-Struct-0.06/Struct.pod Jenda = [EMAIL PROTECTED] === http://Jenda.Krynicky.cz = When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Accessing packed data structures
On 3/14/07, Dharshana Eswaran [EMAIL PROTECTED] wrote: snip The data which i need to pack and unpack to these elements are in hex format(0x0B 0x1C 0x34 etc). It is a string of hex bytes. Can i know about the format in which i need to supply the input? Coz i am unable to accept the hex string. Thanks and Regards, Dharshana Is 0x0B 0x1C 0x34 the actual form of your data? If so, then you do not probably want to use pack, in that case split, map, and hex are your friends: $input = however_you_get_your_input(); my @bytes = map { hex } split ' ', $input; die $_ is not a byte unless $_ = 0 and $_ = 255 for @bytes; If you have a total of six bytes then they are outputing the enum with two bytes, so you need to determine which order it is in (big endian or little endian). my $callId = $bytes[0]; my $type = $bytes[1] 8 | $bytes[2]; #or $bytes[2] 8 | $bytes[1]; my $phonenumber = $bytes[3]; my $lineno = $bytes[4] = 127 ? $bytes[4] : -256 + $bytes[4]; #handle sign my $alpha = $bytes[5] If you only have five bytes then they are treating the enum as a one byte field due to the fact that they know (or hope) it will never be larger than 255: my $callId = $bytes[0]; my $type = $bytes[1]; my $phonenumber = $bytes[2]; my $lineno = $bytes[3] = 127 ? $bytes[3] : -256 + $bytes[3]; #handle sign my $alpha = $bytes[4] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: [SPAM] Re: Accessing packed data structures
On Wed, 14 Mar 2007, Dharshana Eswaran wrote: On 3/13/07, Tom Phoenix [EMAIL PROTECTED] wrote: On 3/13/07, Dharshana Eswaran [EMAIL PROTECTED] wrote: I was going thro the topic Accessing packed data structures in the Perl Complete Reference Book. I came across this example: struct utmp { char ut_user[8]; /* User login name */ char ut_id[4]; /* /etc/inittab id */ char ut_line[12]; /* device name */ short ut_pid; /* process ID */ short ut_type; /* type of entry */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS. */ time_t ut_time; /* time entry was made */ }; For the above structure: The pack template given was a8a4a12l. I am, somehow, not able to understand how to generate the pack template from the structure's data types. The pieces match up this way: char ut_user[8]; /* User login name: a8 */ char ut_id[4]; /* /etc/inittab id: a4 */ char ut_line[12]; /* device name: a12 */ short ut_pid; /* process ID: s */ short ut_type; /* type of entry: s */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS: s s */ time_t ut_time; /* time entry was made: l */ A char array becomes an a42, with the number being the length of the array. A short becomes s, and a long becomes l. (You do need to know something about how C stores data in memory. Note that exit_status and ut_exit are two variables, even though they are declared on the same line; that's why there are two s's in the comment.) Here, how does the pack and unpack play its role? What format should the input be in? What are the possible formats it can accept here? Have you seen the documentation for pack and unpack? There is a large table of format letters. Are you asking for something else? Do you have a C struct that you can't translate to a pack/unpack template? If so, feel free to post it here. Someone will know how to deal with it. Yes, you are right... I have a structure and it is somethiung like this typedef struct _TAPI_VOICE_NOTIFY_INCOMING_CALL_MSG_S TAPI_CALL_ID_T callId; TAPI_VOICE_INCOMING_CALL_TYPE_E type; TAPI_PHONE_NUMBER_A phoneNumber; TAPI_CALL_LINE_ElineNo; TAPI_PHONEBOOK_NAME_A alpha; } TAPI_VOICE_NOTIFY_INCOMING_CALL_MSG_S; Here the typedefs are: TAPI_CALL_ID_T, TAPI_PHONE_NUMBER_A, TAPI_PHONEBOOK_NAME_A = Unsigned int 8 TAPI_VOICE_INCOMING_CALL_TYPE_E = enum type TAPI_CALL_LINE_E = signed int 8 Now how do i come up with the pack template? I dont know how a enum should be represented in the pack template. :-( /I8(enum)I8i8I8/ = pack template The above mentioned enum just contains two elements. The data which i need to pack and unpack to these elements are in hex format(0x0B 0x1C 0x34 etc). It is a string of hex bytes. I kindly request anyone to guide me in this. Thanks and Regards, Dharshana What about this one: http://search.cpan.org/~mhx/Convert-Binary-C-0.67/lib/Convert/Binary/C.pm I once used it to solve the complex pack/unpack temlate and machine endianess problem Vincent Li http://bl0g.blogdns.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Accessing packed data structures
Hi all, I was going thro the topic Accessing packed data structures in the Perl Complete Reference Book. I came across this example: struct utmp { char ut_user[8]; /* User login name */ char ut_id[4]; /* /etc/inittab id */ char ut_line[12]; /* device name */ short ut_pid; /* process ID */ short ut_type; /* type of entry */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS. */ time_t ut_time; /* time entry was made */ }; For the above structure: The pack template given was a8a4a12l. my $packstring = a8a4a12l; my $reclength = length(pack($packstring)); my @ut_types = qw(EMPTY RUN_LVL BOOT_TIME OLD_TIME NEW_TIME INIT_PROCESS LOGIN_PROCESS USER_PROCESS DEAD_PROCESS ACCOUNTING); open(D,/var/adm/wtmp) or die Couldn't open wtmp, $!; while(sysread(D,my $rec,$reclength)) { my ($user,$userid,$line,$pid,$type,$eterm,$eexit,$time) = unpack($packstring,$rec); print($user, $userid, $line, $pid, $ut_types[$type], , $eterm, $eexit, , scalar localtime($time),\n); } close(D) or die Couldn't close wtmp, $!; I am, somehow, not able to understand how to generate the pack template from the structure's data types. I understand that we need to refer to the table in which each character in described (for eg: A - An ASCII string, will be space padded) Here, how does the pack and unpack play its role? What format should the input be in? What are the possible formats it can accept here? I kindly request anyone to explain me this concept. Thanks and Regards, Dharshana
Re: Accessing packed data structures
On 3/13/07, Dharshana Eswaran [EMAIL PROTECTED] wrote: I was going thro the topic Accessing packed data structures in the Perl Complete Reference Book. I came across this example: struct utmp { char ut_user[8]; /* User login name */ char ut_id[4]; /* /etc/inittab id */ char ut_line[12]; /* device name */ short ut_pid; /* process ID */ short ut_type; /* type of entry */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS. */ time_t ut_time; /* time entry was made */ }; For the above structure: The pack template given was a8a4a12l. I am, somehow, not able to understand how to generate the pack template from the structure's data types. The pieces match up this way: char ut_user[8]; /* User login name: a8 */ char ut_id[4]; /* /etc/inittab id: a4 */ char ut_line[12]; /* device name: a12 */ short ut_pid; /* process ID: s */ short ut_type; /* type of entry: s */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS: s s */ time_t ut_time; /* time entry was made: l */ A char array becomes an a42, with the number being the length of the array. A short becomes s, and a long becomes l. (You do need to know something about how C stores data in memory. Note that exit_status and ut_exit are two variables, even though they are declared on the same line; that's why there are two s's in the comment.) Here, how does the pack and unpack play its role? What format should the input be in? What are the possible formats it can accept here? Have you seen the documentation for pack and unpack? There is a large table of format letters. Are you asking for something else? Do you have a C struct that you can't translate to a pack/unpack template? If so, feel free to post it here. Someone will know how to deal with it. Cheers! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Accessing packed data structures
Tom Phoenix wrote: On 3/13/07, Dharshana Eswaran [EMAIL PROTECTED] wrote: I was going thro the topic Accessing packed data structures in the Perl Complete Reference Book. I came across this example: struct utmp { char ut_user[8]; /* User login name */ char ut_id[4]; /* /etc/inittab id */ char ut_line[12]; /* device name */ short ut_pid; /* process ID */ short ut_type; /* type of entry */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS. */ time_t ut_time; /* time entry was made */ }; For the above structure: The pack template given was a8a4a12l. I am, somehow, not able to understand how to generate the pack template from the structure's data types. The pieces match up this way: char ut_user[8]; /* User login name: a8 */ char ut_id[4]; /* /etc/inittab id: a4 */ char ut_line[12]; /* device name: a12 */ short ut_pid; /* process ID: s */ short ut_type; /* type of entry: s */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS: s s */ time_t ut_time; /* time entry was made: l */ A char array becomes an a42, with the number being the length of the array. A short becomes s, and a long becomes l. (You do need to know something about how C stores data in memory. Note that exit_status and ut_exit are two variables, even though they are declared on the same line; that's why there are two s's in the comment.) No, exit_status is an identifier and ut_exit is the variable. In C it is declared something like: struct exit_status { shortone; shorttwo; }; And is used something like: struct exit_status variable_1, variable_2; See the C Reference Manual at: http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Accessing packed data structures
On 3/13/07, Tom Phoenix [EMAIL PROTECTED] wrote: On 3/13/07, Dharshana Eswaran [EMAIL PROTECTED] wrote: I was going thro the topic Accessing packed data structures in the Perl Complete Reference Book. I came across this example: struct utmp { char ut_user[8]; /* User login name */ char ut_id[4]; /* /etc/inittab id */ char ut_line[12]; /* device name */ short ut_pid; /* process ID */ short ut_type; /* type of entry */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS. */ time_t ut_time; /* time entry was made */ }; For the above structure: The pack template given was a8a4a12l. I am, somehow, not able to understand how to generate the pack template from the structure's data types. The pieces match up this way: char ut_user[8]; /* User login name: a8 */ char ut_id[4]; /* /etc/inittab id: a4 */ char ut_line[12]; /* device name: a12 */ short ut_pid; /* process ID: s */ short ut_type; /* type of entry: s */ struct exit_status ut_exit; /* The exit status of a process */ /* marked as DEAD_PROCESS: s s */ time_t ut_time; /* time entry was made: l */ A char array becomes an a42, with the number being the length of the array. A short becomes s, and a long becomes l. (You do need to know something about how C stores data in memory. Note that exit_status and ut_exit are two variables, even though they are declared on the same line; that's why there are two s's in the comment.) Here, how does the pack and unpack play its role? What format should the input be in? What are the possible formats it can accept here? Have you seen the documentation for pack and unpack? There is a large table of format letters. Are you asking for something else? Do you have a C struct that you can't translate to a pack/unpack template? If so, feel free to post it here. Someone will know how to deal with it. Yes, you are right... I have a structure and it is somethiung like this typedef struct _TAPI_VOICE_NOTIFY_INCOMING_CALL_MSG_S TAPI_CALL_ID_T callId; TAPI_VOICE_INCOMING_CALL_TYPE_E type; TAPI_PHONE_NUMBER_A phoneNumber; TAPI_CALL_LINE_ElineNo; TAPI_PHONEBOOK_NAME_A alpha; } TAPI_VOICE_NOTIFY_INCOMING_CALL_MSG_S; Here the typedefs are: TAPI_CALL_ID_T, TAPI_PHONE_NUMBER_A, TAPI_PHONEBOOK_NAME_A = Unsigned int 8 TAPI_VOICE_INCOMING_CALL_TYPE_E = enum type TAPI_CALL_LINE_E = signed int 8 Now how do i come up with the pack template? I dont know how a enum should be represented in the pack template. :-( /I8(enum)I8i8I8/ = pack template The above mentioned enum just contains two elements. The data which i need to pack and unpack to these elements are in hex format(0x0B 0x1C 0x34 etc). It is a string of hex bytes. I kindly request anyone to guide me in this. Thanks and Regards, Dharshana
Re: Accessing packed data structures
On 3/14/07, Dharshana Eswaran [EMAIL PROTECTED] wrote: snip Now how do i come up with the pack template? I dont know how a enum should be represented in the pack template. :-( /I8(enum)I8i8I8/ = pack template The above mentioned enum just contains two elements. The data which i need to pack and unpack to these elements are in hex format(0x0B 0x1C 0x34 etc). It is a string of hex bytes. I kindly request anyone to guide me in this. Thanks and Regards, Dharshana In ANSI C enums are of type int. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Accessing packed data structures
On 3/14/07, Chas Owens [EMAIL PROTECTED] wrote: On 3/14/07, Dharshana Eswaran [EMAIL PROTECTED] wrote: snip Now how do i come up with the pack template? I dont know how a enum should be represented in the pack template. :-( /I8(enum)I8i8I8/ = pack template The above mentioned enum just contains two elements. The data which i need to pack and unpack to these elements are in hex format(0x0B 0x1C 0x34 etc). It is a string of hex bytes. I kindly request anyone to guide me in this. Thanks and Regards, Dharshana In ANSI C enums are of type int. The data which i need to pack and unpack to these elements are in hex format(0x0B 0x1C 0x34 etc). It is a string of hex bytes. Can i know about the format in which i need to supply the input? Coz i am unable to accept the hex string. Thanks and Regards, Dharshana
Can anyone guide me for : Data Structures in Perl
Hi, can anybody help me with a link or a tutorial for understanding the Datastructures in perl. Example, Array of Array, Hash of Array, Hash of Hash, Array of Hash. Along with few scripts to understand their application. thanks in advance, Atul Ashpalia __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Can anyone guide me for : Data Structures in Perl
You can find everything you need in: perldoc perldsc -- Regards, Edward WIJAYA SINGAPORE can anybody help me with a link or a tutorial for understanding the Datastructures in perl. Example, Array of Array, Hash of Array, Hash of Hash, Array of Hash. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Can anyone guide me for : Data Structures in Perl
Wijaya Edward wrote: can anybody help me with a link or a tutorial for understanding the Datastructures in perl. Example, Array of Array, Hash of Array, Hash of Hash, Array of Hash. You can find everything you need in: perldoc perldsc -- Regards, Edward WIJAYA SINGAPORE There are also: perldoc perllol perldoc perlreftut perldoc perlref And Learning Perl Objects, References, and Modules from O'Reilly. http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Module to merge arbitrary multi-level data structures?
Randy W. Sims wrote: Does anyone know of a module that can take a reference to two arbitrarily organized data structures, and return a merged structure? Scratch that. Hash::Merge seems to do what I want. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Module to merge arbitrary multi-level data structures?
Does anyone know of a module that can take a reference to two arbitrarily organized data structures, and return a merged structure? Thanks, Randy. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: [PBML] Help with Data Structures
From: William Martell mailto:[EMAIL PROTECTED] wrote: : Hello All, : : I was wondering if someone could tell me how to manipulate : this data structure. I have attached the datafile. My code : and code results are printed below. : : If you take a look at the results, you will see that there is : a hash created for each interaction in the loop and the : results are printed using data::dumper. The problem, is that : some hash values are blank, because they are printed during : the following interaction of the loop. : : In other words. I am getting this... [snip] : and I want to get this... : : $VAR1 = { : 'waste' = '0.00', : 'promo' = '0.00', : 'count' = '5.00', : 'plu' = '002800', : 'total' = '10.00', : 'description' = '6 NUGGET', : 'line' = 'PLU' : }; : : Could anyone help explain why this is occuring and how to : resolve it. Thank you all very much for reading my message. It occurs because you are resetting the %record on each pass instead of only as you see a new record begin. Try: my %record; # skip first 3 lines FILE; FILE; FILE; while ( FILE ) { if ( /^PLU/ ) { #reset record %record = (); @record{ qw| line plu description | } = get_plu_and_description($_); } elsif (/^\t/) { @record{ qw| count total promo waste | } = get_count_total_promo_waste($_); print Dumper(\%record); } } HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Help with Data Structures
Hello All, I was wondering if someone could tell me how to manipulate this data structure. I have attached the datafile. My code and code results are printed below. If you take a look at the results, you will see that there is a hash created for each interaction in the loop and the results are printed using data::dumper. The problem, is that some hash values are blank, because they are printed during the following interaction of the loop. In other words. I am getting this... $VAR1 = { 'waste' = '', 'promo' = '', 'count' = '', 'plu' = '002800', 'total' = '', 'description' = '6 NUGGET', 'line' = 'PLU' }; $VAR1 = { 'waste' = '0.00', 'promo' = '0.00', 'count' = '5.00', 'plu' = '', 'total' = '10.00', 'description' = '', 'line' = '' }; and I want to get this... $VAR1 = { 'waste' = '0.00', 'promo' = '0.00', 'count' = '5.00', 'plu' = '002800', 'total' = '10.00', 'description' = '6 NUGGET', 'line' = 'PLU' }; Could anyone help explain why this is occuring and how to resolve it. Thank you all very much for reading my message. PS. Does anyone know where I can get a tutorial or book chapter that talks about how to use Data::Dumper. I checked the documentation on CPAN and it is difficult for me to understand. Any ideas?? THANKS!! CODE #!/perl use strict; use warnings; use Diagnostics; use Data::Dumper 'Dumper'; #find the date report you need #open the report open(FILE,SAM65 plu report.txt) or die(Can't open report); my %record; #print csv( { #line = 'line', #plu= 'plu', #description = 'description', #count = 'count', #total = 'total', #promo = 'promo', #waste = 'waste' #} ), \n; while(FILE){ # create record my %record = ( line= '', plu = '', description = '', count = '', total = '', promo = '', waste = '' ); if($.3){ if(/^PLU/){ #print $.starts with plu\n; @record{ qw| line plu description |} = get_plu_and_description($_); # delete($record{'count'}); # delete($record{'total'}); # delete($record{'promo'}); # delete($record{'waste'}); } else{ @record{ qw| count total promo waste | } = get_count_total_promo_waste($_); } #print Data::Dumper-Dump([\%record],[*record]); print Dumper(\%record); #print csv( \%record ), \n if is_valid_record( \%record ); } } sub get_info{ my $field = shift; chomp($field); $field =~ s/\t+/ /g; my @fields = split(/' '/); foreach my $each (@fields){ $each =~ s/^\s+//; $each =~ s/\s+$//; } return $fields['line']; } sub get_count_total_promo_waste{ my $field = shift; chomp($field); $field =~ s/\t+/ /g;#replace all tabs with spaces my($count, $total, $promo,$waste) = split( ,$field); return $count, $total, $promo, $waste; } sub get_plu_and_description{ my $field = shift; chomp($field); my($line, $plu, $description) = split(/\t/,$field); return $line, $plu, $description; } sub is_valid_record { my $field = shift; foreach my $value ( values %$field ) { # return true if one field is valid return 1 if defined $value $value ne ''; } # return false if we get here return; } sub csv { my $field = shift; # if you need to change field order # do it here my @field_order = qw| line plu description count total promo waste |; # add double quotes () foreach my $value ( values %$field ) { $value = NULL if(!defined($value)); $value =~ s/^\s+//; $value =~ s/\s+$//; $value = qq($value); } # use a hash slice to order the fields return join ',', @$field{ @field_order }; } END CODE RESULTS $VAR1 = { 'waste' = '', 'promo' = '', 'count' = '', 'plu' = '002800', 'total' = '',
Help with Data Structures
Hello All, I hope you all are doing well. I am trying to get this code to work correctly and I have used another Perl Guru's code [Thanks Charles Clarkson] to get this far, but I am still unsure about a few things... I am trying to read a file (attached). and I am trying to make a csv file of it. Here is my code. #!/perl -w use strict; use warnings; use Diagnostics; use Data::Dumper 'Dumper'; #find the date report you need #open the report open(FILE,SAM65 financial report.txt) or die(Can't open report); my %report; my %rpt_title; my %record; print csv( { name = 'name', count = 'count', total = 'total', ext_total = 'ext_total' } ), \n; while(FILE){ if($.3){ print csv( \%record ), \n if is_valid_ad( \%record ); # Set field defaults %record = ( name = '', count = '', total = '', ext_total = '' ); # update %record for this line @record{ qw| name count total ext_total | } = split(/\t/, $_); }#end print csv }#end if.. to skip first three lines. print csv( \%record ), \n if is_valid_ad( \%record ); sub is_valid_ad { my $field = shift; foreach my $value ( values %$field ) { # return true if one field is valid return 1 unless $value eq ''; } # return false if we get here return; } sub csv { my $field = shift; # if you need to change field order # do it here my @field_order = qw|name count total ext_total|; # add double quotes () foreach my $value ( values %$field ) { $value = NULL if(!defined($value)); #if there is no match, then make that field null $value =~ s/^\s+//;#remove leading whitespace $value =~ s/\s+$//;#remove trailing whitespace $value = qq($value); } # use a hash slice to order the fields return join ',', @$field{ @field_order }; } __END__ What I am curious about is, when I run the code without the line to make value equal null if there it is undefined, I get a lot of errors... Use of Uninitialized Values.. Blah Blah. When I add the line in.. the number of errors is reduced, but I still get them. Why do I get them. And how do I get rid of them. The source files are ascii files and I plan on entering each record into a database, then creating a worksheet in excel out of it. THanks in Advance for your time. Will CRS DEMO/12-12-1997/14:00:29 X1/FINANCIAL/12-12-1997/21:02 COUNT TOTAL +PLU TTL32.00 52.28 ADJST TTL 32.00 52.28 NONTAX 1 1.00 TAX1 SALES 0 44.99 TAX2 SALES 0 1.09 TAX3 SALES 0 1.29 TAX10 2.25 XMPTl SALES 0 0.25 EATIN TTL 1 5.24 TAKEOUT TTL 1 0.25 % 1 3 -1.57 % 2 2 -1.11 % 3 1 -4.94 NET SALE6 48.49 RETURN 4 -6.98 GROSS SALES 0 63.09 CASH SALES 6 48.49 CASH- IN-D 6 48.49 DRWR TTL0 48.49 REPORT END-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Help with Data Structures
Hello Group, I am trying to get this code to run. I got it from the book Data Munging with Perl. I cut and pasted the code, then changed the input from stdin to FILE. I supplied a text file for the passwords (I am on a Windows 2k box). But I can't get it correct. I think the line @[EMAIL PROTECTED]) = split(/:/); is incorrect syntactically but I could be wrong. Could anyone help me understand this problem. Thanks in Advance! #!/perl -w use strict; use warnings; open(FILE, passwords.txt) or die(cant open file); #read_passwd; sub read_passwd { my %users; my @fields = qw/name pword uid gid fullname home shell/; while (FILE) { chomp; my %rec; @[EMAIL PROTECTED]) = split(/:/); $users{$rec-{name}} = \%rec; } return \%users; } my $users = read_passwd(); my @names; foreach (keys %{$users}) { next unless $users-{$_}{fullname}; my ($forename, $surname) = split(/\s+/, $users-{$_}{fullname}, 2); push @names, $surname, $forename; } print map { $_\n } sort @names; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Help with Data Structures
From: William Martell mailto:[EMAIL PROTECTED] wrote: : Hello Group, : : : : I am trying to get this code to run. I got it from the book : Data Munging with Perl. I cut and pasted the code, then : changed the input from stdin to FILE. I supplied a text file : for the passwords (I am on a Windows 2k box). But I can't get : it correct. I think the line @[EMAIL PROTECTED]) = split(/:/); is : incorrect syntactically but I could be wrong. Could anyone : help me understand this problem. Thanks in Advance! [snipped code] After removing blank lines and illegal characters I get the following errors. syntax error at bb.pl line 15, near @fields) Global symbol $rec requires explicit package name at bb.pl line 16. Missing right curly or square bracket at bb.pl line 30, at end of line Show us the example from the book. Or give us a link to it on line. (When you cut and paste code please prune excess spacing.) A sample file would be nice to. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Help with Data Structures
On Fri, 4 Jun 2004 09:52:15 -0500, Charles K. Clarkson wrote: Show us the example from the book. Or give us a link to it on line. (When you cut and paste code please prune excess spacing.) A sample file would be nice to. Here you go. Example: listing users To start with, lets produce a list of all of the real names of all of the users on the system. As that would be a little too simple well introduce a couple of refinements. First, included in the list of users in /etc/passwd are a number of special accounts that arent for real users. These will include root (the superuser), lp (a user ID which is often used to carry out printer administration tasks) and a number of other task-oriented users. Assuming that we can detect these uses by the fact that their full names will be empty, well exclude them from the output. Secondly, in the original file, the full names are in the format forename surname. Well print them out as surname, forename, and sort them in surname order. Heres the script: use strict; my $users = read_passwd(); my @names; foreach (keys %{$users}) { next unless $users-{$_}{fullname}; my ($forename, $surname) = split(/\s+/, $users-{$_}{fullname}, 2); push @names, $surname, $forename; } print map { $_\n } sort @names;Example: reading /etc/passwd Lets start by writing a routine to read the data into internal data structures. This routine can then be used by any of the following examples. As always, for flexibility, well assume that the data is coming in via STDIN. sub read_passwd { my %users; my @fields = qw/name pword uid gid fullname home shell/; while (STDIN) { chomp; my %rec; @[EMAIL PROTECTED]) = split(/:/); $users{$rec-{name}} = \%rec; } return \%users; }-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Help with Data Structures
From: William Martell mailto:[EMAIL PROTECTED] wrote: : Here you go. Try this then: my $users = read_passwd(); my @names; foreach (keys %{$users}) { next unless $users-{$_}{fullname}; my ($forename, $surname) = split(/\s+/, $users-{$_}{fullname}, 2); push @names, $surname, $forename; } print map { $_\n } sort @names; sub read_passwd { my %users; my @fields = qw/name pword uid gid fullname home shell/; while (DATA) { chomp; my %rec; @[EMAIL PROTECTED] = split(/:/); $users{$rec{name}} = \%rec; } return \%users; } __END__ 0:1:2:3:Porky Pig:5:6 7:8:9:10:Bug's Bunny:12:13 14:15:16:17:Donald Duck:19:20 21:22:23:24:Tweety Bird:26:27 To use a file, open it and replace DATA with that file handle: sub read_passwd { my $file = shift; my %users; my @fields = qw/name pword uid gid fullname home shell/; # don't clobber open file handles local *FH; open FH, $file or die Cannot open $file: $!; while ( FH ) { chomp; my %rec; @[EMAIL PROTECTED] = split(/:/); $users{$rec{name}} = \%rec; } return \%users; } HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Help with Data Structures
William Martell wrote: Hello Group, Hello, I am trying to get this code to run. I got it from the book Data Munging with Perl. I cut and pasted the code, then changed the input from stdin to FILE. I supplied a text file for the passwords (I am on a Windows 2k box). But I can't get it correct. I think the line @[EMAIL PROTECTED]) = split(/:/); is incorrect ^ ^ Yes it is incorrect. You have a left brace and a right parenthesis when it should be left and right braces. @rec{ @fields } = split /:/; syntactically but I could be wrong. Could anyone help me understand this problem. Thanks in Advance! 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
Your recommendations to tie multi-level data structures
All, I would like the ability to tie a database and use it as a multi-level data structure, like $tied{hash}{subhash} $tied{hash}{array}[0] Etc... Any recommendations for this? I've heard I should check out the MLDBM module. How have others conquered this issue? Is it possible? Thanks, Phil -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Your recommendations to tie multi-level data structures
Phil Schaechter wrote: All, I would like the ability to tie a database and use it as a multi-level data structure, like $tied{hash}{subhash} $tied{hash}{array}[0] Etc... Any recommendations for this? I've heard I should check out the MLDBM module. How have others conquered this issue? Is it possible? Yes, use MLDBM. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: copying complex data structures
use Storable; $arrayref_one = dclone( $arrayref_two ); aha thanks :) it's those details that make a program so much easier to make.. i noticed another poster asking about reference notations- i think that this and Object Oriented programming have given me the most headaches on a conceptual level. while i've gotten a handle on references (i hope) tha OO stuff still gets to me *laugh* again thanks, willy -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
copying complex data structures
the following will not work: $arrayref_one = $arrayreftwo; #it's just making a new name for the same #reference. the following works fine: foreach my $a (0..$what){ foreach my $b (0..$why){ $arrayref_one-[$a]-[$b] = $arrayref_two-[$a]-[$b]; } } it would be nice to have something to copy complex data structures that isn't going to be a code maintenance headache- the above is dependent on the data structure being a 2 dimensional array of particular size. i don't want that restriction. any help would be apreciated :) thanks, willy -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: copying complex data structures
On Dec 11, 2003, at 9:37 AM, West, William M wrote: the following will not work: $arrayref_one = $arrayreftwo; #it's just making a new name for the same #reference. the following works fine: foreach my $a (0..$what){ foreach my $b (0..$why){ $arrayref_one-[$a]-[$b] = $arrayref_two-[$a]-[$b]; } } it would be nice to have something to copy complex data structures that isn't going to be a code maintenance headache- the above is dependent on the data structure being a 2 dimensional array of particular size. i don't want that restriction. any help would be apreciated :) From the Perl Cookbook (2nd Edition), recipe 11.12: use Storable; $arrayref_one = dclone( $arrayref_two ); James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: copying complex data structures
Try clone method of Storable module from CPAN. There is also Clone module out there. HTH, José. -Original Message- From: West, William M [mailto:[EMAIL PROTECTED] Sent: Thursday, December 11, 2003 4:37 PM To: [EMAIL PROTECTED] Subject: copying complex data structures the following will not work: $arrayref_one = $arrayreftwo; #it's just making a new name for the same #reference. the following works fine: foreach my $a (0..$what){ foreach my $b (0..$why){ $arrayref_one-[$a]-[$b] = $arrayref_two-[$a]-[$b]; } } it would be nice to have something to copy complex data structures that isn't going to be a code maintenance headache- the above is dependent on the data structure being a 2 dimensional array of particular size. i don't want that restriction. any help would be apreciated :) thanks, willy -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response DISCLAIMER This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer. Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Creating uniqueness in complex data structures
The log files I am parsing have threads (a T followed by several alphanumeric numbers) associated with each line of text. I want to push each value of $2 (which is a server name) into an anonymous array. This works fine in the following code. if ($_ =~ /(T[0-9A-F]+) MSM SCTS\((.+)\)/) { my $thread = $1; push @{$server{$thread}}, $2 unless grep $_ eq $2, @{$server{$thread}}; } The problem I just realized is that sometimes a duplicate thread number exists and it overwrites the information it previously had in it. How do I create a unique data structure that can prevent this from happening? --Paul
Re: Creating uniqueness in complex data structures
On Nov 23, 2003, at 6:36 PM, Paul Harwood wrote: The log files I am parsing have threads (a T followed by several alphanumeric numbers) associated with each line of text. I want to push each value of $2 (which is a server name) into an anonymous array. This works fine in the following code. if ($_ =~ /(T[0-9A-F]+) MSM SCTS\((.+)\)/) { my $thread = $1; push @{$server{$thread}}, $2 unless grep $_ eq $2, @{$server{$thread}}; } The problem I just realized is that sometimes a duplicate thread number exists and it overwrites the information it previously had in it. How do I create a unique data structure that can prevent this from happening? I wouldn't think this could be happening IF the thread number already contained an array reference. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Creating uniqueness in complex data structures
The problem is that the value in $thread can be duplicated therefore it will write over anything else contained there. That's the problem I am having. -Original Message- From: James Edward Gray II [mailto:[EMAIL PROTECTED] Posted At: Monday, November 24, 2003 6:18 AM Posted To: Perl Conversation: Creating uniqueness in complex data structures Subject: Re: Creating uniqueness in complex data structures On Nov 23, 2003, at 6:36 PM, Paul Harwood wrote: The log files I am parsing have threads (a T followed by several alphanumeric numbers) associated with each line of text. I want to push each value of $2 (which is a server name) into an anonymous array. This works fine in the following code. if ($_ =~ /(T[0-9A-F]+) MSM SCTS\((.+)\)/) { my $thread = $1; push @{$server{$thread}}, $2 unless grep $_ eq $2, @{$server{$thread}}; } The problem I just realized is that sometimes a duplicate thread number exists and it overwrites the information it previously had in it. How do I create a unique data structure that can prevent this from happening? I wouldn't think this could be happening IF the thread number already contained an array reference. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Creating uniqueness in complex data structures
On Nov 24, Paul Harwood said: The problem is that the value in $thread can be duplicated therefore it will write over anything else contained there. That's the problem I am having. -Original Message- if ($_ =~ /(T[0-9A-F]+) MSM SCTS\((.+)\)/) { my $thread = $1; push @{$server{$thread}}, $2 unless grep $_ eq $2, @{$server{$thread}}; } There's no overwriting going on. You're using push(), which means that you'll never lose data, you'll only get more and more. Here's a similarly-functioning block of code: my @stuff = ( 'T123 Here is a message', 'T237 Here's a different one', 'T123 Here's one for the first thread', 'T237 Here's a different one', # -- duplicate, not entered ); for (@stuff) { chomp; my ($thread, $data) = split ' ', $_, 2; push @{ $server{$thread} }, $data unless grep $_ eq $data, @{ $server{$thread} }; } At the end of this, $server{T123} has two messages, and $server{T237} has only one. Explain how you think your code has a problem. -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ stu what does y/// stand for? tenderpuss why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Creating uniqueness in complex data structures
OK, maybe I am confusing myself. :) -Original Message- From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED] Sent: Monday, November 24, 2003 9:27 AM To: Paul Harwood Cc: James Edward Gray II; Beginner Perl Subject: RE: Creating uniqueness in complex data structures On Nov 24, Paul Harwood said: The problem is that the value in $thread can be duplicated therefore it will write over anything else contained there. That's the problem I am having. -Original Message- if ($_ =~ /(T[0-9A-F]+) MSM SCTS\((.+)\)/) { my $thread = $1; push @{$server{$thread}}, $2 unless grep $_ eq $2, @{$server{$thread}}; } There's no overwriting going on. You're using push(), which means that you'll never lose data, you'll only get more and more. Here's a similarly-functioning block of code: my @stuff = ( 'T123 Here is a message', 'T237 Here's a different one', 'T123 Here's one for the first thread', 'T237 Here's a different one', # -- duplicate, not entered ); for (@stuff) { chomp; my ($thread, $data) = split ' ', $_, 2; push @{ $server{$thread} }, $data unless grep $_ eq $data, @{ $server{$thread} }; } At the end of this, $server{T123} has two messages, and $server{T237} has only one. Explain how you think your code has a problem. -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ stu what does y/// stand for? tenderpuss why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Data Structures in Perl
HI, I must be missing something, after following Gupta's advice the particular object printed fine using this code: $obj-order_number ($order_num); print obj: . $obj-order_number($order_num) . \n; push @order, $obj; for each of the 15 orders which I have shown below. Now, at the end of my program I run a for loop: for (my $w=0;$w=$#order;$w++) { print Order Number: . $order[$w]-order_number() . \n; } to print out all of the 15 differnet order numbers and all I get is the last order number printed 15 times. What am I missing besides sleep. TIA. Trevor C:\maverickperl miva_struc_class.pl # of Rows in Order: 25 rows in the 1 order Order Number is 3225 w is 1 obj:3225 # of Rows in Order: 27 rows in the 2 order Order Number is 3218 w is 2 obj:3218 # of Rows in Order: 25 rows in the 3 order Order Number is 3223 w is 3 obj:3223 # of Rows in Order: 26 rows in the 4 order Order Number is 5010 w is 4 obj:5010 # of Rows in Order: 29 rows in the 5 order Order Number is 5006 w is 5 obj:5006 # of Rows in Order: 26 rows in the 6 order Order Number is 5010 w is 6 obj:5010 # of Rows in Order: 26 rows in the 7 order Order Number is 5003 w is 7 obj:5003 # of Rows in Order: 27 rows in the 8 order Order Number is 4971 w is 8 obj:4971 # of Rows in Order: 26 rows in the 9 order Order Number is 4931 w is 9 obj:4931 # of Rows in Order: 27 rows in the 10 order Order Number is 4925 w is 10 obj:4925 # of Rows in Order: 25 rows in the 11 order Order Number is 4893 w is 11 obj:4893 # of Rows in Order: 26 rows in the 12 order Order Number is 4864 w is 12 obj:4864 # of Rows in Order: 25 rows in the 13 order Order Number is 4870 w is 13 obj:4870 # of Rows in Order: 29 rows in the 14 order Order Number is 4873 w is 14 obj:4873 # of Rows in Order: 25 rows in the 15 order Order Number is 3280 w is 15 obj:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 Order Number:3280 C:\maverick -Original Message- From: Gupta, Sharad [mailto:[EMAIL PROTECTED] Sent: Sunday, July 06, 2003 4:55 PM To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: RE: Data Structures in Perl From ur example i assume that: my @order is an array collecting all of the objects with each order. So, instead of my @order = new miva_order; we mean to say: my @orders; my $obj = new miva_order; $obj-order_number(get_order_num_via_regex()); push @orders,$obj; for (my $w=0;$w=$#orders;$w++) { print $order[$w]-order_number(); } Note that in the for loop above i am starting $w with 0 and not 1. If u start with 1 and ur @order have only 1 element, it won't print anything because the index start with '0' -Sharad -Original Message- From: Trevor Morrison [mailto:[EMAIL PROTECTED] Sent: Sunday, July 06, 2003 10:37 AM To: [EMAIL PROTECTED] Subject: Data Structures in Perl Hi, I am new to using data structures in Perl, but have played with them in C (a long time ago). I have downloaded the Class::Struct::Fields module on my W2K system and all works well. I am finding the documentation a little terse though. I am trying to read into my Perl program orders that was placed through an on-line shopping cart that are sent to an Outlook mailbox. I export the email to a file that I read into my Perl program. Since each order is basically the same as far as content is concerned there are some variation. I would like to use a data structure to store each orders information in. Now, I have read through the Programming Perl manual (Chapter 9) on Data structures several times and searched the web for the information that I need, but cannot find it. If I claim my data structure like so: use Class::Struct::FIELDS; struct (miva_order = { order_number='$' , date='$' , bill_name ='$' , ship_name ='$' , ship_email_address ='$' , ship_phone_number ='$' , ship_business_name ='$' , ship_to_street ='$' , ship_to_city='$' , ship_to_state ='$' , ship_to_zip ='$' , bill_to_street ='$' , bill_to_city='$' , bill_to_state ='$' , bill_to_zip ='$' , quantity='$' , code='$' , shipping_method ='$' , shipping_amount
how to create/use data structures in perl ?
Hi, How to create data structures in perl ? I have following data structure in C...and i have to create similar data structure in perl and use... how to create data strutures in perl and how to use ? typedef struct { charPrefix[8]; charPrint; charName[80]; charModTime[32]; int Len; } fileinfo_; Thanks -Madhu __ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: how to create/use data structures in perl ?
Madhu Reddy wrote: Hi, How to create data structures in perl ? I have following data structure in C...and i have to create similar data structure in perl and use... how to create data strutures in perl and how to use ? perldoc -f pack perldoc -f unpack typedef struct { charPrefix[8]; charPrint; charName[80]; charModTime[32]; int Len; } fileinfo_; Eg. #!/usr/bin/perl use strict; use warnings; my $format = 'Z8 c Z80 Z32 i'; my $fileinfo_instance = pack ($format, 'project_', 1, 'SomeName', 'ModTime', 40); my ($prefix, $print, $name, $modtime, $length) = unpack ($format, $fileinfo_instance); print Prefix = $prefix\n; print Print = $print\n; print Name= $name\n; print Modtime = $modtime\n; print Length = $length\n; Thanks -Madhu __ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Data Structures in Perl
Hi, I am new to using data structures in Perl, but have played with them in C (a long time ago). I have downloaded the Class::Struct::Fields module on my W2K system and all works well. I am finding the documentation a little terse though. I am trying to read into my Perl program orders that was placed through an on-line shopping cart that are sent to an Outlook mailbox. I export the email to a file that I read into my Perl program. Since each order is basically the same as far as content is concerned there are some variation. I would like to use a data structure to store each orders information in. Now, I have read through the Programming Perl manual (Chapter 9) on Data structures several times and searched the web for the information that I need, but cannot find it. If I claim my data structure like so: use Class::Struct::FIELDS; struct (miva_order = { order_number='$' , date='$' , bill_name ='$' , ship_name ='$' , ship_email_address ='$' , ship_phone_number ='$' , ship_business_name ='$' , ship_to_street ='$' , ship_to_city='$' , ship_to_state ='$' , ship_to_zip ='$' , bill_to_street ='$' , bill_to_city='$' , bill_to_state ='$' , bill_to_zip ='$' , quantity='$' , code='$' , shipping_method ='$' , shipping_amount ='$' , sales_tax ='$' , notes ='$' } ); my @order = new miva_order; I want to fill the fields saying $order-order_number($order_numb) where the $orders numb is pulled from the email using regular expressions. Now, what I want to be able to do is say : for (my $w=1; $w=$number_of_orders; w++) { print $order[$w]-order_number ; } and have all the order numbers that are associated with the each of the orders print out; and it does not work this way. This is where my problem is, I am coding wrong and I cannot see it. Any help is appreciated. Trevor -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Data Structures in Perl
From ur example i assume that: my @order is an array collecting all of the objects with each order. So, instead of my @order = new miva_order; we mean to say: my @orders; my $obj = new miva_order; $obj-order_number(get_order_num_via_regex()); push @orders,$obj; for (my $w=0;$w=$#orders;$w++) { print $order[$w]-order_number(); } Note that in the for loop above i am starting $w with 0 and not 1. If u start with 1 and ur @order have only 1 element, it won't print anything because the index start with '0' -Sharad -Original Message- From: Trevor Morrison [mailto:[EMAIL PROTECTED] Sent: Sunday, July 06, 2003 10:37 AM To: [EMAIL PROTECTED] Subject: Data Structures in Perl Hi, I am new to using data structures in Perl, but have played with them in C (a long time ago). I have downloaded the Class::Struct::Fields module on my W2K system and all works well. I am finding the documentation a little terse though. I am trying to read into my Perl program orders that was placed through an on-line shopping cart that are sent to an Outlook mailbox. I export the email to a file that I read into my Perl program. Since each order is basically the same as far as content is concerned there are some variation. I would like to use a data structure to store each orders information in. Now, I have read through the Programming Perl manual (Chapter 9) on Data structures several times and searched the web for the information that I need, but cannot find it. If I claim my data structure like so: use Class::Struct::FIELDS; struct (miva_order = { order_number='$' , date='$' , bill_name ='$' , ship_name ='$' , ship_email_address ='$' , ship_phone_number ='$' , ship_business_name ='$' , ship_to_street ='$' , ship_to_city='$' , ship_to_state ='$' , ship_to_zip ='$' , bill_to_street ='$' , bill_to_city='$' , bill_to_state ='$' , bill_to_zip ='$' , quantity='$' , code='$' , shipping_method ='$' , shipping_amount ='$' , sales_tax ='$' , notes ='$' } ); my @order = new miva_order; I want to fill the fields saying $order-order_number($order_numb) where the $orders numb is pulled from the email using regular expressions. Now, what I want to be able to do is say : for (my $w=1; $w=$number_of_orders; w++) { print $order[$w]-order_number ; } and have all the order numbers that are associated with the each of the orders print out; and it does not work this way. This is where my problem is, I am coding wrong and I cannot see it. Any help is appreciated. Trevor -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Data Structures in Perl
Gupta, Sharad [EMAIL PROTECTED] writes: From ur example i assume that: my @order is an array collecting all of the objects with each order. So, instead of my @order = new miva_order; we mean to say: my @orders; my $obj = new miva_order; $obj-order_number(get_order_num_via_regex()); push @orders,$obj; for (my $w=0;$w=$#orders;$w++) { print $order[$w]-order_number(); ^ 'order', or 'orders'? } The Perlish way to do the above is: foreach my $order (@orders) { print $order-order_number(); } This iterates over each element in the array @orders, and places them in turn in a variable called '$order'. Do it this way, and you don't have to worry about the index of the array at all. -RN -- Robin Norwood Red Hat, Inc. The Sage does nothing, yet nothing remains undone. -Lao Tzu, Te Tao Ching -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: data structures one more try
Rob, Good call! You can print out the data in @bags and @bag_quantity using Data::Dumper.. print Dumper [EMAIL PROTECTED]; http://www.coraconnection.com/paul_s/pages/catalog.html Check this out, 'order' second and third bag in the first row. and hit the CGI and see the results, bizarre! Thanks for your help, Regards Dave ( kora musician / audiophile / web master @ cora connection / DFW, TX, USA) and web designer for Afropop Worldwide http://www.afropop.org/community/contributors.php?ID=8 == Cora Connection: Your West African Music Source Resources, Recordings, Instruments More! http://www.coraconnection.com/ == == Cora Connection: Your West African Music Source Resources, Recordings, Instruments More! http://www.coraconnection.com/ == -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: data structures one more try
Rob and all the rest reading this, Here's what fixed the problem. The check boxes are only sent to CGI.pm if checked, while all the input fields are sent regardless. So @bag_quantity was filled with '0's ... get rid of 'o's @bag_quantity = grep(/[^0]/, @bag_quantity); ...this now seems to work Thanks, Dave ( kora musician / audiophile / web master @ cora connection / DFW, TX, USA) and web designer for Afropop Worldwide http://www.afropop.org/community/contributors.php?ID=8 == Cora Connection: Your West African Music Source Resources, Recordings, Instruments More! http://www.coraconnection.com/ == -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
data structures
I am trying to build a data structure, and am tripping up here. any suggestions? Thx! Dave !/usr/bin/perl -w snip... @bags = param('handbag'); get all of the bags styles @bag_quantity = param('quantity'); get all of the bags quantity foreach my $bag (@bags){ ($bag_name,$imgName) = split (/,/,$bag); push %bags_ordered,$bag_name; push %bags_ordered{$bag_name}{image_name} = $imgName; } foreach my $q (@bag_quantity){ push %bags_ordered{$bag_name}{$bag_quantity} =$q; } Data structure: %bags_ordered = ( bag-style1 = { price = 10, image_name = 'FIO-142b-small', quantity= 5 }, bag-style2 = { price = 12, image_name = 'GUC-208-small', quantity= 5, }, ); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: data structures
any suggestions? I'll try. @bags = param('handbag'); get all of the bags styles Missing comment... @bags = param('handbag'); # get all of the bags styles push %bags_ordered,$bag_name; You can't push onto a hash, only an array. So either %bags_ordered need to be an array, or you mean something else. Maybe this is what you mean... use strict; # a big bonus for debugging use Data::Dumper; # for testing my %bags_ordered; my @bags = param('handbag'); # get all of the bags styles my @bag_quantity = param('quantity'); # get all of the bags quantity for (my $i = 0; $i @bags; $i++) { # split the bag item my ($bag_name, $imgName) = split (/,/, $bags[$i]); # create an empty hash if this is a new bag $bags_ordered{$bag_name} = {} unless exists $bags_ordered{$bag_name}; # set the image name $bags_ordered{$bag_name}-{image_name} = $imgName; # set the quantity. should it be additive with +=? $bags_ordered{$bag_name}-{quantity} = $bag_quantity[$i]; } # print the structure for testing using Data::Dumper print Dumper \%bags_ordered; Rob -Original Message- From: David Gilden [mailto:[EMAIL PROTECTED] Sent: Tuesday, March 04, 2003 4:57 PM To: [EMAIL PROTECTED] Subject: data structures I am trying to build a data structure, and am tripping up here. any suggestions? Thx! Dave !/usr/bin/perl -w snip... @bags = param('handbag'); get all of the bags styles @bag_quantity = param('quantity'); get all of the bags quantity foreach my $bag (@bags){ ($bag_name,$imgName) = split (/,/,$bag); push %bags_ordered,$bag_name; push %bags_ordered{$bag_name}{image_name} = $imgName; } foreach my $q (@bag_quantity){ push %bags_ordered{$bag_name}{$bag_quantity} =$q; } Data structure: %bags_ordered = ( bag-style1 = { price = 10, image_name = 'FIO-142b-small', quantity= 5 }, bag-style2 = { price = 12, image_name = 'GUC-208-small', quantity= 5, }, ); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: data structures / CGI.pm
Good afternon, I am not seeing consistent results from my script below. It seems that sometimes it works and other times I get '0's in the quantity field. As I don't write PERL often enough this is probably poorly written code! Thanks for any help. Dave HTML at: http://www.coraconnection.com/paul_s/pages/catalog.htm #!/usr/bin/perl -w use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use strict; my (@bags,@bag_quantity); my ($bag_name,$imgName); my (%bags_ordered,%prices); my $n=0; my $bags_ordered; use Data::Dumper; # Build data structure @bags = param('handbag'); # get all of the bags styles @bag_quantity = param('quantity'); # get all of the bags quantity foreach my $bag (@bags){ ($bag_name,$imgName) = split (/,/,$bag); $bags_ordered{$bag_name} = {} unless exists $bags_ordered{$bag_name}; # thanks rob! $bags_ordered{$bag_name}-{image_name} =$imgName; $bags_ordered{$bag_name}-{quantity} = $bag_quantity[$n]; $n++; } ## I don't think '-' really needed # will the order be correct, don think so! Prices amounts with out '$' # %prices = ( 'bag-style-dots' = 12, 'bag-style-dogs' =5, 'bag-style-cats' =44, 'bag-style-leather' =15, 'bag-style-fur' =23, 'bag-style-stripes' =12, 'bag-style1' =14, 'bag-style2' =6, 'bag-style3' =51, ); # #%bags_ordered = ( #bag-style1 = { price = 10, # image_name = 'FIO-142b-small', #quantity= 5 # }, # #bag-style2 = { price = 12, # image_name = 'GUC-208-small', # quantity= 5, # }, # #); print header; # print HTML print START_HTML; !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd; html lang=en head titleCatalog/title style type=text/css media=all !-- [EMAIL PROTECTED] ../paul_s/css/list.css; -- /style style type=text/css media=screen !-- #catalog td {text-align:center; } -- /style link rel=stylesheet href=/paul_s/css/main.css type=text/css /head body bgcolor=white text=black img src=../paul_s/images/banner.jpg alt= tmp banner width=720 height=76 border=1 h1 align=centerComplete your order/h1 pmore text on how complete your order../p START_HTML [EMAIL PROTECTED] = param(); #foreach my $name (@test){ #my $val = param($name); # print qq|p\$name $name \$val $val/p\n|; # } print Dumper \%bags_ordered; print hr noshade\n; print qq|table border=1 width=600\ntr\n|; print qq|th colspan=2Description/ththQuantity/ththPrice/th\n|; print /tr\n; foreach my $bag (keys %bags_ordered){ print tr\n; print qq|tdimg src=../paul_s/images/| . $bags_ordered{$bag}{image_name} . qq|.jpg alt=$imgName width=120 height=179 alt= bag /td\n|; print td$bag/td; print td$bags_ordered{$bag}{quantity}/tdtd\$$prices{$bag}/td\n; print /tr\n; } print trtdnbsp;/tdtdsubtotal/td\n; print trtdnbsp;/tdtd fill in /td\n; print /table\n; #print ORDER_HTML; #ORDER_HTML # print end html tags print end_html; # print start_form( -method = 'post' , -action =finnish-not done.pl); exit; == Cora Connection: Your West African Music Source Resources, Recordings, Instruments More! http://www.coraconnection.com/ == -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: STILL NEEW SOME HELP: Using data structures to find similarities and differences
Aimal Pashtoonmal wrote: Hello, I have three files, for two different cites. The first file contains names of men with their faves, in columns. The second file is the same but for their partner. The third file is a list of similar partners (partner selection based on data fed into a perl script): FILE_1 and FILE_2 column headings: NAMEtabAGEtabFAVE MOVIEtabFAVE ACTOR(IN THAT MOVIE)tabCHOICE OF NICK NAME(FOR THAT ACTOR)tabGUESS LENGTH OF ACTING CAREER(STARTtabEND)tabFAVE HOL DESTN. FILE_1 data: DAVEtab36tabBATMANtabJ.NICKELSONtabWALL FILLER FACEtab38tab2003tabIBIZA FILE_2 data: JOtab28tabGIJANEtabD.MOOREtabTRACEYtab89tab2003tabIBIZA FILE_3 data: DAVEtabJO EG. OF TYPE OF DATA I INTEND TO EXTRACT: for each set of partners i am trying count the number of times the holiday desnn. is the same and different; for each person in file 1, how many times does the seclected partner choose the same actor, and then find the averaage length of career. The problem I have is since I am not well versed with perl, which data structure do I use, I am not finding this easy. CAN ANYONE HELP PLEASE? All I need is to be pointed in the right direction. ALL WILL BE APPRECIATED. Cheers, aim. Hi Aimal, Can you post any of what you have tried to do to sort out the issues here. Choosing appropriate data structures actually comes in a later part of the process, after you have decided what you want to do with the information in general terms. If you wish to program, you must take responsibility for examining the data, and planning a strategy for how to use it. No one else can do that for you. I can give you a couple hints: The first line of the two data files contains meta-information. You will want to handle this separately, and use the information contained there to set up the structure to hold the actual data. This might suggest a hash as the overall structure, using the column names as keys. Anyway, try to make at least some start on this on your own, and you will find more help here. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: STILL NEEW SOME HELP: Using data structures to find similarities and differences
Hello, I have three files, for two different cites. The first file contains names of men with their faves, in columns. The second file is the same but for their partner. The third file is a list of similar partners (partner selection based on data fed into a perl script): FILE_1 and FILE_2 column headings: NAMEtabAGEtabFAVE MOVIEtabFAVE ACTOR(IN THAT MOVIE)tabCHOICE OF NICK NAME(FOR THAT ACTOR)tabGUESS LENGTH OF ACTING CAREER(STARTtabEND)tabFAVE HOL DESTN. FILE_1 data: DAVEtab36tabBATMANtabJ.NICKELSONtabWALL FILLER FACEtab38tab2003tabIBIZA FILE_2 data: JOtab28tabGIJANEtabD.MOOREtabTRACEYtab89tab2003tabIBIZA FILE_3 data: DAVEtabJO EG. OF TYPE OF DATA I INTEND TO EXTRACT: for each set of partners i am trying count the number of times the holiday desnn. is the same and different; for each person in file 1, how many times does the seclected partner choose the same actor, and then find the averaage length of career. The problem I have is since I am not well versed with perl, which data structure do I use, I am not finding this easy. CAN ANYONE HELP PLEASE? All I need is to be pointed in the right direction. ALL WILL BE APPRECIATED. Cheers, aim. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Storing complex Data structures ?
You can also store your complexe data structures in a xml file by using XML::Simple module. http://search.cpan.org/author/GRANTM/XML-Simple-1.08/Simple.pm José. -Original Message- From: Rowan Reid [mailto:[EMAIL PROTECTED]] Sent: Tuesday, September 10, 2002 8:39 PM To: 'Perl beginners' Subject: RE: Storing complex Data structures ? No. You access the data through %COMPANY_DB. That's the whole point of tie(). The object $db is used only if you need to call additional methods of MLDBM on it. You really don't even need to save $db, because you can always get it via: tied(%COMPANY_DB) This is what I did previously, but whenever I closed the data file And tried to re-open it the data could nto be retrieved. I was under The impression that DB-File was able to store referenced data. perldoc -f tie perldoc -f tied perldoc perltie -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] DISCLAIMER This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer. Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Storing complex Data structures ?
What is the correct way to access and store complex data structures to a file. I.e. hashes with several references. The method I have been using is as follows tie (%COMPANY_DB,'MLDBM','studio3.db',O_RDWR|O_CREAT,0777,$DB_BTREE) || die $!; Also DB_File. I'm realizing from previous help that I have to access this as an Object as in $db = tie (%COMPANY_DB,'MLDBM','studio3.db',O_RDWR|O_CREAT,0777,$DB_BTREE) || die $!; Where in my %COMPANY_DB %COMPANY_DB -@employee -n - %info name='name' address =@address And so forth what would be the correct way to access contents of the data structure using the object $db ? Thanks in advance. Rowan Reid Job Captain, Systems Administrator STUDIO 3 ARCHITECTS 909 982 1717 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Storing complex Data structures ?
On Tue, Sep 10, 2002 at 11:17:33AM -0700, Rowan Reid wrote: What is the correct way to access and store complex data structures to a file. I.e. hashes with several references. This is Perl. There is no correct way. But you could take a look at Data::Dumper and Storable for other solutions. You might also like to investigate YAML. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Storing complex Data structures ?
-Original Message- From: Rowan Reid [mailto:[EMAIL PROTECTED]] Sent: Tuesday, September 10, 2002 2:18 PM To: 'Perl beginners' Subject: Storing complex Data structures ? What is the correct way to access and store complex data structures to a file. I.e. hashes with several references. The method I have been using is as follows tie (%COMPANY_DB,'MLDBM','studio3.db',O_RDWR|O_CREAT,0777,$DB_BTREE) || die $!; Also DB_File. I'm realizing from previous help that I have to access this as an Object as in $db = tie (%COMPANY_DB,'MLDBM','studio3.db',O_RDWR|O_CREAT,0777,$DB_BTRE E) || die $!; Where in my %COMPANY_DB %COMPANY_DB -@employee -n - %info name='name' address =@address And so forth what would be the correct way to access contents of the data structure using the object $db ? No. You access the data through %COMPANY_DB. That's the whole point of tie(). The object $db is used only if you need to call additional methods of MLDBM on it. You really don't even need to save $db, because you can always get it via: tied(%COMPANY_DB) perldoc -f tie perldoc -f tied perldoc perltie -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Storing complex Data structures ?
No. You access the data through %COMPANY_DB. That's the whole point of tie(). The object $db is used only if you need to call additional methods of MLDBM on it. You really don't even need to save $db, because you can always get it via: tied(%COMPANY_DB) This is what I did previously, but whenever I closed the data file And tried to re-open it the data could nto be retrieved. I was under The impression that DB-File was able to store referenced data. perldoc -f tie perldoc -f tied perldoc perltie -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: undef of nested data structures
-Original Message- From: Nikola Janceski [mailto:[EMAIL PROTECTED]] Sent: Thursday, August 15, 2002 12:44 PM To: Beginners (E-mail) Subject: undef of nested data structures I am wondering how undef works. perldoc -f undef I know that undef will undefine a variable if used like undef($scalar); Yes. I also know that it doesn't actually free up the memory but tells Perl that it's now available to be recycled for other data. Well, you probably should't worry about memory allocation details. Perl takes care of all those details behind the scenes. but what about nested data (ie. hashes of hashes, arrays of arrays, etc.). if $ref is a reference to a nested structure (let's say a hash of hashes). will - undef $ref; - undefine the enitre nested data structure if no other variables contain references to any part of it? (assuming no cyclical references) undef($ref) undefines the scalar $ref. Now, if whatever $ref was pointing to had no other references to it, then yes, that data would be freed. But this is a side effect of the undef(), not something special about undef() itself. The same thing would happen if you assigned foo to $ref, so that $ref no longer contained a reference to some data. Data is freed when the last reference to it is removed. You can think of undef like this: undef($scalar); is the same as: $scalar = undef; undef(@array); is the same as: @array = (); undef(%hash); is the same as: %hash = (); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: undef of nested data structures
From: Nikola Janceski [EMAIL PROTECTED] I am wondering how undef works. I know that undef will undefine a variable if used like undef($scalar); I also know that it doesn't actually free up the memory but tells Perl that it's now available to be recycled for other data. but what about nested data (ie. hashes of hashes, arrays of arrays, etc.). if $ref is a reference to a nested structure (let's say a hash of hashes). will - undef $ref; - undefine the enitre nested data structure if no other variables contain references to any part of it? (assuming no cyclical references) Perl as usual does what you need. If there are no cyclic references the whole structure will be garbage collected (freed) and the memory will be reused. To create even cyclic references that may be garbage collected use the weakref() function from Scalar::Util. I'm not feeling like explaining garbage collection (I know how it works, but would screw the explanation.) so please try to find something on reference counting garbage collection. Jenda === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: undef of nested data structures
As a side note, you can play around with Devel::Peek to see how many things are referencing a variable. # perl -MDevel::Peek -e '$foo=foo;$bar=\$foo;$zog=\$foo;Dump($foo);$bar=bar;Dump($foo);print $foo;'; SV = PV(0x80f2424) at 0x810b3cc REFCNT = 3 FLAGS = (POK,pPOK) PV = 0x80f10a0 foo\0 CUR = 3 LEN = 4 SV = PV(0x80f2424) at 0x810b3cc REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x80f10a0 foo\0 CUR = 3 LEN = 4 foo There, you can see $foo has a REFCNT (ref count) of 3 ($foo, $bar and $zog), then a REFCNT of 2 after the value of $bar is re-assigned. Of course, the value in $foo is still 'foo'; # perl -MDevel::Peek -e '$foo=foo;$bar=\$foo;$zog=\$foo;Dump($foo);$$bar=bar;Dump($foo);print $foo;'; SV = PV(0x80f2424) at 0x810b3cc REFCNT = 3 FLAGS = (POK,pPOK) PV = 0x80f10a0 foo\0 CUR = 3 LEN = 4 SV = PV(0x80f2424) at 0x810b3cc REFCNT = 3 FLAGS = (POK,pPOK) PV = 0x80f10a0 bar\0 CUR = 3 LEN = 4 bar Here, the REFCNT, as expected, stays the same. And the value in $foo is changed. If you are within code and want to make sure you don't undef things which have refs to it, you can use: # perl -MDevel::Peek=SvREFCNT -e \ '$foo=foo;$bar=\$foo;$zog=\$foo;print SvREFCNT($foo);'; 3 Which will tell you how many refs there are. Of course, if it is 1, then nothing else should be referencing $foo aside $foo. This doesn't answer the question in any way, but thought some may be interested in this side-note. Cheers, Kevin On Thu, Aug 15, 2002 at 01:52:23PM -0400, Bob Showalter ([EMAIL PROTECTED]) said something similar to: -Original Message- From: Nikola Janceski [mailto:[EMAIL PROTECTED]] Sent: Thursday, August 15, 2002 12:44 PM To: Beginners (E-mail) Subject: undef of nested data structures I am wondering how undef works. perldoc -f undef I know that undef will undefine a variable if used like undef($scalar); Yes. I also know that it doesn't actually free up the memory but tells Perl that it's now available to be recycled for other data. Well, you probably should't worry about memory allocation details. Perl takes care of all those details behind the scenes. but what about nested data (ie. hashes of hashes, arrays of arrays, etc.). if $ref is a reference to a nested structure (let's say a hash of hashes). will - undef $ref; - undefine the enitre nested data structure if no other variables contain references to any part of it? (assuming no cyclical references) undef($ref) undefines the scalar $ref. Now, if whatever $ref was pointing to had no other references to it, then yes, that data would be freed. But this is a side effect of the undef(), not something special about undef() itself. The same thing would happen if you assigned foo to $ref, so that $ref no longer contained a reference to some data. Data is freed when the last reference to it is removed. You can think of undef like this: undef($scalar); is the same as: $scalar = undef; undef(@array); is the same as: @array = (); undef(%hash); is the same as: %hash = (); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- [Writing CGI Applications with Perl - http://perlcgi-book.com] Children are naive -- they trust everyone. School is bad enough, but, if you put a child anywhere in the vicinity of a church, you're asking for trouble. -- Frank Zappa -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
newbie: data structures
How would I store (and access) a table of information...assuming there is a unique key 'email'...ie - These are the column names: email firstName lastName phone address1 [EMAIL PROTECTED] Joe Blow 415-555- 123 Mayberry [EMAIL PROTECTED] John Doe 619-555-5551 456 Happy Ln. etc...etc... (also some of the fields will be null from time to time). __ Do You Yahoo!? Yahoo! Health - Feel better, live better http://health.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: newbie: data structures
This is a very broad question, so giving a specific answer is difficult to say the least, but since no one else has responded I will provide at least this much You are going to want to be looking at using a hash of hashes essentially you create a hash where each key is the email address (which you will be looking up on) and the value associated with that key is itself a hash. Here is one place to start: http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/hash_of_hashes.html Or there are examples of these types of structures in the Camel, Advanced Perl Programming, and possibly the other perl related O'Reilly books. Good luck. http://danconia.org Anthony E. wrote: How would I store (and access) a table of information...assuming there is a unique key 'email'...ie - These are the column names: email firstName lastName phone address1 [EMAIL PROTECTED] Joe Blow 415-555- 123 Mayberry [EMAIL PROTECTED] John Doe 619-555-5551 456 Happy Ln. etc...etc... (also some of the fields will be null from time to time). __ Do You Yahoo!? Yahoo! Health - Feel better, live better http://health.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: newbie: data structures
ur mail title says data structure but ur problem doe not seem to be of the nature. please be a little more clear. store and retreive, there are as many options as u want. what do u exactly r lookign for??? regards vicki On Wed, 24 Jul 2002 Anthony E. wrote : How would I store (and access) a table of information...assuming there is a unique key 'email'...ie - These are the column names: email firstName lastName phone address1 [EMAIL PROTECTED] Joe Blow 415-555- 123 Mayberry [EMAIL PROTECTED] John Doe 619-555-5551 456 Happy Ln. etc...etc... (also some of the fields will be null from time to time). __ Do You Yahoo!? Yahoo! Health - Feel better, live better http://health.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck. Thanks Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
Oops..your Requirements was different.. To achieve, @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] you need to do push (@$stats, @$totals, $loads); I am assuming User, cpu, mem, tot_cpu, tot_mem are again reference to an array Cheers Shihir -Original Message- From: Shishir K. Singh Sent: Tuesday, June 18, 2002 10:55 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck. Thanks Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
this won't work, will just push one array onto another. thanks -Original Message- From: Shishir K. Singh [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:55 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
-Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck Is there a correspondence between stats and totals, such that the nth entry in stats matches the nth entry in totals? If so, you would want to say something like: push @{$stats-[$_]}, @{$totals-[$_]}, @$loads for 0 .. $#$stats; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
I am assuming User, cpu, mem, tot_cpu, tot_mem are again reference to an array woops let me clarify. $stats is a ref to an array of anon arrays. so '$stats-[0]' contains a record lie [someuser, 5.5, 10.2] same goes for $totals $loads just refs a single anon array: $loads = [load1, load2] thanks Cheers Shihir -Original Message- From: Shishir K. Singh Sent: Tuesday, June 18, 2002 10:55 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck. Thanks Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
Do you mean to say push (@$stats, @$totals, $loads); didn't work ?? -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 11:23 AM To: Shishir K. Singh; Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array I am assuming User, cpu, mem, tot_cpu, tot_mem are again reference to an array woops let me clarify. $stats is a ref to an array of anon arrays. so '$stats-[0]' contains a record lie [someuser, 5.5, 10.2] same goes for $totals $loads just refs a single anon array: $loads = [load1, load2] thanks Cheers Shihir -Original Message- From: Shishir K. Singh Sent: Tuesday, June 18, 2002 10:55 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck. Thanks Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
push (@$stats, @$totals, $loads); didn't work ?? haven't tried yet. get back to you in a bit thanks again -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
Is there a correspondence between stats and totals, such that the nth entry in stats matches the nth entry in totals? If so, you would want to say something like: push @{$stats-[$_]}, @{$totals-[$_]}, @$loads for 0 .. $#$stats; no, there is not. i just want to combine the records pointed to by each array into one record (in a new array of records). going to try Shihir's suggestion in a few minutes. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
-Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 11:46 AM To: [EMAIL PROTECTED] Subject: RE: combining data structures into one array Is there a correspondence between stats and totals, such that the nth entry in stats matches the nth entry in totals? If so, you would want to say something like: push @{$stats-[$_]}, @{$totals-[$_]}, @$loads for 0 .. $#$stats; no, there is not. i just want to combine the records pointed to by each array into one record (in a new array of records). Well then, I can't figure out how you plan to combine the data. How about an example with some real data? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
Shishir and Bob, still can't get it to work. also decided we can leave out $loads, so basically i want to combine $stats and $totals. as bob suggested, here is sample data: ($stats, $totals, $loads) = gets_stats(); when the get_stats() func is called $stats will contain something like this: $stats = [ [ oracle, 6.8, 11.2 ], [ ksh, 1.8, 1.2 ], etc ]; and $totals will have something like: $totals = [ [ 15.8, 17.2 ], [ 3.7, 3.9 ], etc ]; so i want to end up with something like: @stats = ( [oracle, 6.8, 11.2,15.8, 17.2 ], [ksh, 1.8, 1.2, 3.7, 3.9 ], etc... ); thanks. -Original Message- From: Shishir K. Singh [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 11:04 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array Oops..your Requirements was different.. To achieve, @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] you need to do push (@$stats, @$totals, $loads); I am assuming User, cpu, mem, tot_cpu, tot_mem are again reference to an array Cheers Shihir -Original Message- From: Shishir K. Singh Sent: Tuesday, June 18, 2002 10:55 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck. Thanks Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: combining data structures into one array
Hello Jim, how about something along these lines: for(0..$#{$stats}) { push @stats, $stats-[$_],$totals-[$_]; } Shawn - Original Message - From: Kipp, James [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, June 18, 2002 11:40 AM Subject: RE: combining data structures into one array Shishir and Bob, still can't get it to work. also decided we can leave out $loads, so basically i want to combine $stats and $totals. as bob suggested, here is sample data: ($stats, $totals, $loads) = gets_stats(); when the get_stats() func is called $stats will contain something like this: $stats = [ [ oracle, 6.8, 11.2 ], [ ksh, 1.8, 1.2 ], etc ]; and $totals will have something like: $totals = [ [ 15.8, 17.2 ], [ 3.7, 3.9 ], etc ]; so i want to end up with something like: @stats = ( [oracle, 6.8, 11.2,15.8, 17.2 ], [ksh, 1.8, 1.2, 3.7, 3.9 ], etc... ); thanks. -Original Message- From: Shishir K. Singh [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 11:04 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array Oops..your Requirements was different.. To achieve, @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] you need to do push (@$stats, @$totals, $loads); I am assuming User, cpu, mem, tot_cpu, tot_mem are again reference to an array Cheers Shihir -Original Message- From: Shishir K. Singh Sent: Tuesday, June 18, 2002 10:55 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck. Thanks Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: combining data structures into one array
On Tuesday, June 18, 2002, at 07:32 , Kipp, James wrote: I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); would it be possible to expose this gets_stats() function - and/or re-think how it deals with generating data... ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
Oh Simple...What threw me off is that I though you wanted to use the rest of the arrays in the same format as $stats which is a reference to an anonymous array of anonymous arrays. $stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2]; Now I see that you want something like this @stats = (user, cpu, mem, tot_cpu, tot_mem, load1, load2); You can do this # my @myVars = (); push (@myVars, @$stats, @$totals, $loads); ## -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 12:40 PM To: [EMAIL PROTECTED] Subject: RE: combining data structures into one array Shishir and Bob, still can't get it to work. also decided we can leave out $loads, so basically i want to combine $stats and $totals. as bob suggested, here is sample data: ($stats, $totals, $loads) = gets_stats(); when the get_stats() func is called $stats will contain something like this: $stats = [ [ oracle, 6.8, 11.2 ], [ ksh, 1.8, 1.2 ], etc ]; and $totals will have something like: $totals = [ [ 15.8, 17.2 ], [ 3.7, 3.9 ], etc ]; so i want to end up with something like: @stats = ( [oracle, 6.8, 11.2,15.8, 17.2 ], [ksh, 1.8, 1.2, 3.7, 3.9 ], etc... ); thanks. -Original Message- From: Shishir K. Singh [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 11:04 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array Oops..your Requirements was different.. To achieve, @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] you need to do push (@$stats, @$totals, $loads); I am assuming User, cpu, mem, tot_cpu, tot_mem are again reference to an array Cheers Shihir -Original Message- From: Shishir K. Singh Sent: Tuesday, June 18, 2002 10:55 AM To: Kipp, James; [EMAIL PROTECTED] Subject: RE: combining data structures into one array push (@myVar, @$_) for @$stats; push (@myVar, @$_) for @$totals; push (@myVar, $_) for @$loads; -Original Message- From: Kipp, James [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 18, 2002 10:32 AM To: [EMAIL PROTECTED] Subject: combining data structures into one array I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); $stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each record of each array into one. here is how the structures look: $stats - @array - [user, cpu, mem] $totals - @array - [tot_cpu, tot_mem] $loads - [load1, load2] so i would like to itereate through the records of each of these arrays and end up with @stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2] [ ..another record...] [ ..another record...] ...etc I have tried a few things, but no luck. Thanks Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: combining data structures into one array
I have a subroutine that returns 3 array refs. so i have: my ($stats, $totals, $loads) = gets_stats(); would it be possible to expose this gets_stats() function - and/or re-think how it deals with generating data... of course, that is what i am doing now :-). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]