Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Regexp and reading a file n-lines at time

by epimenidecretese (Acolyte)
on Feb 03, 2010 at 10:13 UTC ( #821116=note: print w/replies, xml ) Need Help??


in reply to Re: Regexp and reading a file n-lines at time
in thread Regexp and reading a file n-lines at time

I got interesting results with hash of arrays,but I don't know how to access the single titles and then each element of the arrays into them.

I think I'm working with anonymous hashes and arrays,right ? In this case,how do I access to an anonymous hash ?

 $HASH->{$Title} = []; This is just here to initialize an empty array ?

I'm looking for something like:

my $abstract=shift @{$HASH->{$Title}->{???}}#here I need to access eac +h single title in order to get the first element of each (anonymous) +hash print "<abstract>"."$abstract"."</abstract>\n"; #here I should print all the element between the first and the last o +ne of the array in each single hash my $procedure=pop @{$HASH->{$Title}}#same as before print "<procedure>"."$procedure"."</procedure>\n";

One of Crete's own prophets has said it: 'Cretans are always liars, evil brutes, lazy gluttons'.
He has surely told the truth.

Replies are listed 'Best First'.
Re^3: Regexp and reading a file n-lines at time
by epimenidecretese (Acolyte) on Feb 03, 2010 at 14:22 UTC

    I finally got it!

    Here is my solution and it works perfectly:

    foreach $Title (keys %{$HASH}) { print $OUT "<recipe>".$Title."</recipe>\n"; my $abstract=shift @{$HASH->{$Title}}; print $OUT "<abstract>".$abstract."</abstract>\n"; my $procedure=pop @{$HASH->{$Title}}; foreach my $ingredient (@{$HASH->{$Title}}) { print $OUT "<ingredient>".$recipe."</ingredient>\n"; } print $OUT "<procedure>".$procedure."</procedure>\n"; }

    Thank you very much,guys!I think I'm going to love this place.

    One of Crete's own prophets has said it: 'Cretans are always liars, evil brutes, lazy gluttons'.
    He has surely told the truth.

      You may want to group your print statements; and you don't need to concatenate strings and variables (like "<title>".$Title."</title>") - you can print them together in one double-quoted string ("<title>$Title</title>").

      Consider:

      foreach $Title (keys %{$HASH}) { my $abstract = shift @{$HASH->{$Title}}; my $procedure = pop @{$HASH->{$Title}}; print $OUT "<title>$Title</title>\n", "<abstract>$abstract</abstract>\n", (map { "<recipe>$_</recipe>\n" }@{$HASH->{$Title}}), "<procedure>$procedure</procedure>\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://821116]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2019-12-06 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (154 votes). Check out past polls.

    Notices?