Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by ahmad (Hermit)
on Feb 01, 2010 at 07:58 UTC ( #820728=note: print w/replies, xml ) Need Help??


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

This would work, You'll get hash of arrays

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $HASH; my $Title; while ( <DATA> ) { chomp; next if ! $_; if ( /^\d+\.(.*)/sg ) { $Title = $1; $HASH->{$Title} = []; next; } if ( exists $HASH->{$Title} ) { push @{$HASH->{$Title}} , $_; } } print Dumper($HASH); __DATA__ 1.TITLE OF FIRST RECIPE abstract Recipe 1. Recipe 2. Recipe ... Procedure... 2.TITLE OF SECOND RECIPE ...

Replies are listed 'Best First'.
Re^2: Regexp and reading a file n-lines at time
by jwkrahn (Monsignor) on Feb 01, 2010 at 10:42 UTC
    if ( /^\d+\.(.*)/sg ) {

    Your pattern is anchored at the beginning of the string so the /g option is superfluous.    The string in $_ contains only one line so the use of the /s option is also superfluous.

Re^2: Regexp and reading a file n-lines at time
by epimenidecretese (Acolyte) on Feb 01, 2010 at 08:32 UTC

    Yes,ahmad's solution works perfectly.

    Now,I just have to understand it completely and tag the output.

    Could you give me a hint where to look for(in the documentation) to understand the code inside the while block ?

    Thank you very much.

Re^2: Regexp and reading a file n-lines at time
by epimenidecretese (Acolyte) on Feb 03, 2010 at 05:13 UTC

    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.

      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://820728]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2020-01-20 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?