Ask a Question related to PERL Miscellaneous, Design and Development.
-
Mihai Maris #1
hash problems
I have a problem with hash tables.
I created a hash but I can't retrieve the elements from the hash in the same order.
Ex:
%hash = ("a" => 20, "b" => 30, "c" => 40);
print key %hash; # this prints "cba" or "bac", etc.
Exists a posibility to retrieve the key in the same order like i putted in the
hash.
Thank you in advance !
Mihai Maris Guest
-
Hash
Hi, How set up functions hash in php4.3.2 thanks _________________________________________________________________ Charla con tus amigos en... -
Hash in AS 2.0
So I was about to write a hash algorythm for flash and it appears brandon hall beat me by 2 years. Since there isn't anything here, in the flash... -
hash of hash of array slices
This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
Another reference question (hash of hash references)
beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes...... -
Brian McCauley #2
Re: hash problems
Mihai Maris <mihai.maris@alcatel.ro> does not think that reading the
FAQ would be a valuable way to spend his time, and would much rather
get us to do it for him so writes:
This is FAQ: "How can I make my hash remember the order I put elements> Exists a posibility to retrieve the key in the same order like i putted in the
> hash.
into it?"
If you are going to do anything in advance it should be to read the FAQ.> Thank you in advance !
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest
-
Mihai Maris #3
Re: hash problems
First of all : I'm a beginner in Perl. And I found this a shortest way to find the
answeear to my problem.
And I even didn't know about this FAQs and I also don't have so much time to
navigate throw.
Brian McCauley wrote:
> Mihai Maris <mihai.maris@alcatel.ro> does not think that reading the
> FAQ would be a valuable way to spend his time, and would much rather
> get us to do it for him so writes:
>>> > Exists a posibility to retrieve the key in the same order like i putted in the
> > hash.
> This is FAQ: "How can I make my hash remember the order I put elements
> into it?"
>>> > Thank you in advance !
> If you are going to do anything in advance it should be to read the FAQ.
>
> --
> \\ ( )
> . _\\__[oo
> .__/ \\ /\@
> . l___\\
> # ll l\\
> ###LL LL\\Mihai Maris Guest
-
Uri Guttman #4
Re: hash problems
>>>>> "MM" == Mihai Maris <mihai.maris@alcatel.ro> writes:
MM> First of all : I'm a beginner in Perl. And I found this a
MM> shortest way to find the answeear to my problem. And I even
MM> didn't know about this FAQs and I also don't have so much time to
MM> navigate throw.
and you think usenet is the fastest way to get answers? you have a major
disconnect there. usenet is usually the slowest way. and this is not a
helpdesk, but a discussion group. it is best to post code or problems
here and not simple questions.
uri
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Uri Guttman Guest
-
Tad McClellan #5
Re: hash problems
Mihai Maris <mihai.maris@alcatel.ro> wrote:
> First of all : I'm a beginner in Perl.
Nothing wrong with that.
And an expert has just shown you how to get fast answers to
common Perl questions.
Getting fast answers to questions that are very likely to occur
to you is a great benefit.
You (could) have learned something valuable here...
> And I found this a shortest way to find the
> answeear to my problem.
Posting to Usenet is the *longest* way to find an answer. It can take
hours or days or even forever.
Finding the answer in the docs that came with the software you are
using, or in a newsgroup archive will much faster than typing up
a question and hoping that someone sometime will answer it.
> And I even didn't know about this FAQs
Then a well-socialized response would have been:
I didn't know about that, thanks.
rather than whining about it.
> and I also don't have so much time to
> navigate throw.
But we do?
Asking thousands of people to read the docs to you is inefficient
and wasteful.
You have done serious damage to your ability to get help with any
future Perl questions that you might have. So long.
*plonk*
[snip TOFU]
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Mihai Maris #6
Re: hash problems
I think I didn't have been well understood :
I KNOW THAT IN THIS FORUM ARE A LOT OF EXPERIENCED PROGRAMERS IN PERL.
I THOUGHT MAYBE THEY ALREADY ENCOUNTER THIS PROBLEM AND THEY JUST
CAN GIVE ME ANSWEAR.
IN THIS TIME WHEN I AM WAITING FOR THE ANSWEAR I CAN CONTINUE MY REGULAR JOB.
I WAS NOT TRYING TO SAY/MEAN FOR PEOPLE TO LOOK FOR ME IN THE FILES/FAQ/MANUALS .
SORRY FOR ANY ENCOVENIENCE.
Tad McClellan wrote:
> Mihai Maris <mihai.maris@alcatel.ro> wrote:
>>> > First of all : I'm a beginner in Perl.
> Nothing wrong with that.
>
> And an expert has just shown you how to get fast answers to
> common Perl questions.
>
> Getting fast answers to questions that are very likely to occur
> to you is a great benefit.
>
> You (could) have learned something valuable here...
>>> > And I found this a shortest way to find the
> > answeear to my problem.
> Posting to Usenet is the *longest* way to find an answer. It can take
> hours or days or even forever.
>
> Finding the answer in the docs that came with the software you are
> using, or in a newsgroup archive will much faster than typing up
> a question and hoping that someone sometime will answer it.
>>> > And I even didn't know about this FAQs
> Then a well-socialized response would have been:
>
> I didn't know about that, thanks.
>
> rather than whining about it.
>>> > and I also don't have so much time to
> > navigate throw.
> But we do?
>
> Asking thousands of people to read the docs to you is inefficient
> and wasteful.
>
> You have done serious damage to your ability to get help with any
> future Perl questions that you might have. So long.
>
> *plonk*
>
> [snip TOFU]
>
> --
> Tad McClellan SGML consulting
> [email]tadmc@augustmail.com[/email] Perl programming
> Fort Worth, TexasMihai Maris Guest
-
Uri Guttman #7
Re: hash problems
>>>>> "MM" == Mihai Maris <mihai.maris@alcatel.ro> writes:
MM> I think I didn't have been well understood :
you were well understood.
MM> I KNOW THAT IN THIS FORUM ARE A LOT OF EXPERIENCED PROGRAMERS IN
MM> PERL. I THOUGHT MAYBE THEY ALREADY ENCOUNTER THIS PROBLEM AND
MM> THEY JUST CAN GIVE ME ANSWEAR. IN THIS TIME WHEN I AM WAITING FOR
MM> THE ANSWEAR I CAN CONTINUE MY REGULAR JOB. I WAS NOT TRYING TO
MM> SAY/MEAN FOR PEOPLE TO LOOK FOR ME IN THE FILES/FAQ/MANUALS .
yes, and we all know the answer to your question which is in the FAQ. it
is a VERY COMMON QUESTION SO IT WAS WRITTEN UP IN THE FREQUENTLY ASKED
QUESTIONS FILE WHICH YOU CAN SEARCH THROUGH TO FIND YOUR ANSWER.
MM> SORRY FOR ANY ENCOVENIENCE.
WAS IT SO HARD FOR YOU TO SEARCH THE FAQ FOR THIS? OR USE GOOGLE.COM? OR
PERLDOC.COM? WAS THAT INCONVENIENT FOR YOU? OR IS JUST YOUR BEING
INCONVENIENT TO HUNDREDS OF PEOPLE MORE IMPORTANT?
whew, all that caps is hard on my shift fingers. :)
uri
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Uri Guttman Guest
-
Keith Keller #8
Re: hash problems
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
In article <3F25F872.1C75F859@alcatel.ro>, Mihai Maris wrote:They gave you the answer. Did you look for it yet?>
> I think I didn't have been well understood :
>
> I KNOW THAT IN THIS FORUM ARE A LOT OF EXPERIENCED PROGRAMERS IN PERL.
> I THOUGHT MAYBE THEY ALREADY ENCOUNTER THIS PROBLEM AND THEY JUST
> CAN GIVE ME ANSWEAR.
The answer is in the FAQ so that people can continue their regular job rather> IN THIS TIME WHEN I AM WAITING FOR THE ANSWEAR I CAN CONTINUE MY REGULAR JOB.
than spend all their time answering the same questions every hour.
Since the answer is in the files/faq/manuals, why would anyone *not* tell you> I WAS NOT TRYING TO SAY/MEAN FOR PEOPLE TO LOOK FOR ME IN THE FILES/FAQ/MANUALS .
to look there?
Thus far you seem like the person most inconvenienced by your reluctance to> SORRY FOR ANY ENCOVENIENCE.
read the docs.
Good luck finding your answer.
[snip TOFU, again]
- --keith
- --
[email]kkeller-mmmspam@wombat.san-francisco.ca.us[/email]
(try just my userid to email me)
alt.os.linux.slackware FAQ: [url]http://wombat.san-francisco.ca.us/cgi-bin/fom[/url]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see [url]http://www.gnupg.org[/url]
iEYEARECAAYFAj8mACoACgkQhVcNCxZ5ID8iiwCbB4TGO+HVuB KefSJPlzLEWJ6e
/C8AoJe5IR+4VEIRKzQ/flzi9zLl80cz
=9YWD
-----END PGP SIGNATURE-----
Keith Keller Guest
-
Jdavis #9
hash problems
Hello,
I have a script that gets items and corresponding price from a web
site. all goes well until i put the item and price in a hash. When i try
to print key and value from the hash it comes out all weired. Could some
one take a look and see what im doing wrong.
thanks,
--
jdavis <jd@taproot.bz>
p.s. sorry about the wrap...its my mail client :(
#! /usr/bin/perl -w
use LWP::Simple;
$item_string = '\<\!\-\- \#\#\# Display Item\'s Name and make a
hyperlink \#\#\# \-\-\>';
$price_string = 'color="red"';
$URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
$content = get($URL);
# put each line of $content into a array
@content = split(/\n/, $content);
@content_reversed = reverse @content;
while(@content_reversed){
$_ = pop(@content_reversed);
if(/$item_string/){
$the_item = pop(@content_reversed);
($trash,$clean_item) = split(/\<b\>/, $the_item);
print "ITEM = $clean_item\n";
do{
$_ = pop(@content_reversed);
}until(/$price_string/); ## this is the price line
($trash,$ruff_price) = split(/\<font color\="red"\>/,$_);
($clean_price,$trash) = split(/\<\/font\>\<\/font\>/,$ruff_price);
print "PRICE = $clean_price\n";
$price_items{$clean_item} = $clean_price;
}
}
while(($k, $v) = each %price_items){
print "K = $k";
print "V = $v\n";
}
Jdavis Guest
-
Wiggins D'Anconia #10
Re: hash problems
jdavis wrote:
#! /usr/bin/perl -w> Hello,
> I have a script that gets items and corresponding price from a web
> site. all goes well until i put the item and price in a hash. When i try
> to print key and value from the hash it comes out all weired. Could some
> one take a look and see what im doing wrong.
>
> thanks,
use LWP::Simple;
$item_string = '\<\!\-\- \#\#\# Display Item\'s Name and make a
hyperlink \#\#\# \-\-\>';
$price_string = 'color="red"';
$URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
$content = get($URL);
# put each line of $content into a array
@content = split(/\n/, $content);
@content_reversed = reverse @content;
while(@content_reversed){
$_ = pop(@content_reversed);
if(/$item_string/){
$the_item = pop(@content_reversed);
($trash,$clean_item) = split(/\<b\>/, $the_item);
print "ITEM = $clean_item\n";
do{
$_ = pop(@content_reversed);
}until(/$price_string/); ## this is the price line
($trash,$ruff_price) = split(/\<font color\="red"\>/,$_);
($clean_price,$trash) = split(/\<\/font\>\<\/font\>/,$ruff_price);
print "PRICE = $clean_price\n";
$price_items{$clean_item} = $clean_price;
}
}
while(($k, $v) = each %price_items){
print "K = $k";
print "V = $v\n";
}
"Comes out all weird" is a very poor bug description. I am assuming you
are running this at a terminal window?? Rather than apologizing for the
message wrapping, you should instead apologize for not using strict.
And the above code is the reason for my earlier ramblings about the $_
and why it is nice to have named variables to yell at, especially when
performing functions like pop. Now having ranted enough, let's look at
your current problem...
It appears that there is an extra control character on the end of the
'clean_item' that is screwing up the output. Initially I knew something
was up because I used Data::Dumper to check your structure which was
fine, and its' output was wacky, so I had it redirected to a text file
which opened in Vim shows that your structure is correct but also
displays the Ctrl+M character. Where he is coming from I have no idea
and honestly don't care much, but you can successfully lop him off with:
$clean_item =~ s/\015//g;
In just the right spot...
[url]http://danconia.org[/url]
p.s. I do assume you have permission to screen scrape the contents.....
This can be seen as a denial of service attack, especially with a couple
hundred beginner's all firing up lwp::simple to get the html to debug
the problem... over... and... over... and .... over...
Wiggins D'Anconia Guest
-
Charles K. Clarkson #11
RE: hash problems
jdavis <jd@taproot.bz> wrote:
:
: Hello,
: I have a script that gets items and corresponding price from
: a web site. all goes well until i put the item and price in
: a hash. When i try to print key and value from the hash it
: comes out all weired. Could some one take a look and see
: what im doing wrong.
You're defining an error as "comes out all weird".
You're not using strict.
You're escaping characters unnecessarily.
You're using regexes to parse html.
: p.s. sorry about the wrap...its my mail client :(
You could change your tabs to spaces and wrap the long
lines yourself for more clarity.
As Wiggins warned, you do have permission to scrape
this page, right? I placed the page content in a file for
testing. Read the HTML::TokeParser POD for instructions
to parse a scalar.
use strict;
use warnings;
use Data::Dumper;
use HTML::TokeParser;
my $page = HTML::TokeParser->new("foo3.html") ||
die "Can't open: $!";
my %items;
while ( my $token = $page->get_token ) {
next unless
$token->[0] = 'C'
&& $token->[1] =~
/### Display Item's Name and make a hyperlink ###/;
$page->get_tag( 'b' );
my $item_name = $page->get_trimmed_text( '/b' );
$items{ $item_name } = fetch_price( $page );
}
sub fetch_price {
my $page = shift;
while ( my $token = $page->get_tag( 'font' ) ) {
return
$page->get_trimmed_text( '/font' )
if
defined $token->[1]{color}
&& $token->[1]{color} eq 'red';
}
}
print Dumper \%items;
__END__
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
Charles K. Clarkson Guest
-
John W. Krahn #12
Re: hash problems
Jdavis wrote:
Hello,>
> Hello,
Certainly.> I have a script that gets items and corresponding price from a web
> site. all goes well until i put the item and price in a hash. When i try
> to print key and value from the hash it comes out all weired. Could some
> one take a look and see what im doing wrong.
use strict;> #! /usr/bin/perl -w
You don't need to escape those characters, they are not special in> use LWP::Simple;
>
> $item_string = '\<\!\-\- \#\#\# Display Item\'s Name and make a
> hyperlink \#\#\# \-\-\>';
strings or regular expressions.
my $item_string = '<!-- ### Display Item\'s Name and make a hyperlink
### -->';
And since you are using this as a regular expression you can compile it
here instead of later.
my $item_string = qr/<!-- ### Display Item's Name and make a hyperlink
### -->/;
my $price_string = qr/color="red"/;> $price_string = 'color="red"';
You don't need a $trash variable, you can use undef instead or use a> $URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
> $content = get($URL);
>
> # put each line of $content into a array
> @content = split(/\n/, $content);
>
> @content_reversed = reverse @content;
>
> while(@content_reversed){
> $_ = pop(@content_reversed);
> if(/$item_string/){
> $the_item = pop(@content_reversed);
> ($trash,$clean_item) = split(/\<b\>/, $the_item);
list slice on the split results. You don't need to backslash '<' or
'>'.
( undef, $clean_item ) = split /<b>/, $the_item;
Or:
$clean_item = (split /<b>/, $the_item)[1];
You don't need the do{} block as you only have a single statement.> print "ITEM = $clean_item\n";
>
> do{
> $_ = pop(@content_reversed);
> }until(/$price_string/); ## this is the price line
$_ = pop @content_reversed until /$price_string/; ## this
is the price line
$ruff_price = (split /<font color="red">/)[1];> ($trash,$ruff_price) = split(/\<font color\="red"\>/,$_);
> ($clean_price,$trash) = split(/\<\/font\>\<\/font\>/,$ruff_price);
$clean_price = (split /<\/font><\/font>/, $ruff_price)[0];
Your problem is that $clean_item (your hash key) has a "\015" (CR)> print "PRICE = $clean_price\n";
> $price_items{$clean_item} = $clean_price;
> }
> }
>
> while(($k, $v) = each %price_items){
> print "K = $k";
> print "V = $v\n";
> }
character at the end. When you print "K = $k" the carriage return moves
the cursor to the beginning of the line and "V = $v\n" prints over the
previous output. The HTTP standard (RFC2616) defines CR LF as the
end-of-line marker and you are only removing the LF character.
A more simplified version of your code would be:
#! /usr/bin/perl -w
use strict;
use LWP::Simple;
my $URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
my $content = get( $URL );
my %price_items = $content =~ m{
(?-x:<!-- ### Display Item's Name and make a hyperlink ### -->)
.+?
(?i:<b>)
\s* (\S.*?\S) \s* $
.+?
(?i-x:<font color="red">)
(\$\d[,.\d]+\d)
(?i:</font>)
}smxg;
while ( my ( $k, $v ) = each %price_items ) {
print "K = $k V = $v\n";
}
__END__
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Jdavis #13
Re: hash problems
On Fri, 2003-12-12 at 23:58, John W. Krahn wrote:
thanks all those who replied. I appreciate the help :)> Jdavis wrote:>> >
> > Hello,
> Hello,
>>> > I have a script that gets items and corresponding price from a web
> > site. all goes well until i put the item and price in a hash. When i try
> > to print key and value from the hash it comes out all weired. Could some
> > one take a look and see what im doing wrong.
> Certainly.
>
>>> > #! /usr/bin/perl -w
> use strict;
>>> > use LWP::Simple;
> >
> > $item_string = '\<\!\-\- \#\#\# Display Item\'s Name and make a
> > hyperlink \#\#\# \-\-\>';
> You don't need to escape those characters, they are not special in
> strings or regular expressions.
>
> my $item_string = '<!-- ### Display Item\'s Name and make a hyperlink
> ### -->';
>
> And since you are using this as a regular expression you can compile it
> here instead of later.
>
> my $item_string = qr/<!-- ### Display Item's Name and make a hyperlink
> ### -->/;
>
>>> > $price_string = 'color="red"';
> my $price_string = qr/color="red"/;
>
>>> > $URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
> > $content = get($URL);
> >
> > # put each line of $content into a array
> > @content = split(/\n/, $content);
> >
> > @content_reversed = reverse @content;
> >
> > while(@content_reversed){
> > $_ = pop(@content_reversed);
> > if(/$item_string/){
> > $the_item = pop(@content_reversed);
> > ($trash,$clean_item) = split(/\<b\>/, $the_item);
> You don't need a $trash variable, you can use undef instead or use a
> list slice on the split results. You don't need to backslash '<' or
> '>'.
>
> ( undef, $clean_item ) = split /<b>/, $the_item;
>
> Or:
>
> $clean_item = (split /<b>/, $the_item)[1];
>
>>> > print "ITEM = $clean_item\n";
> >
> > do{
> > $_ = pop(@content_reversed);
> > }until(/$price_string/); ## this is the price line
> You don't need the do{} block as you only have a single statement.
>
> $_ = pop @content_reversed until /$price_string/; ## this
> is the price line
>
>>> > ($trash,$ruff_price) = split(/\<font color\="red"\>/,$_);
> > ($clean_price,$trash) = split(/\<\/font\>\<\/font\>/,$ruff_price);
> $ruff_price = (split /<font color="red">/)[1];
> $clean_price = (split /<\/font><\/font>/, $ruff_price)[0];
>
>>> > print "PRICE = $clean_price\n";
> > $price_items{$clean_item} = $clean_price;
> > }
> > }
> >
> > while(($k, $v) = each %price_items){
> > print "K = $k";
> > print "V = $v\n";
> > }
> Your problem is that $clean_item (your hash key) has a "\015" (CR)
> character at the end. When you print "K = $k" the carriage return moves
> the cursor to the beginning of the line and "V = $v\n" prints over the
> previous output. The HTTP standard (RFC2616) defines CR LF as the
> end-of-line marker and you are only removing the LF character.
>
> A more simplified version of your code would be:
>
> #! /usr/bin/perl -w
> use strict;
>
> use LWP::Simple;
>
> my $URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
> my $content = get( $URL );
>
> my %price_items = $content =~ m{
> (?-x:<!-- ### Display Item's Name and make a hyperlink ### -->)
> .+?
> (?i:<b>)
> \s* (\S.*?\S) \s* $
> .+?
> (?i-x:<font color="red">)
> (\$\d[,.\d]+\d)
> (?i:</font>)
> }smxg;
>
> while ( my ( $k, $v ) = each %price_items ) {
> print "K = $k V = $v\n";
> }
>
> __END__
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
I will look into use Strict.
thanks again,
--
jdavis <jd@taproot.bz>
Jdavis Guest



Reply With Quote

