[26819] in Perl-Users-Digest
Perl-Users Digest, Issue: 8853 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jan 15 14:05:28 2006
Date: Sun, 15 Jan 2006 11:05:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 15 Jan 2006 Volume: 10 Number: 8853
Today's topics:
create a hierarchical list from a text file <friendly@yorku.ca>
Re: mechanize follow_link () on an image <mark.clementsREMOVETHIS@wanadoo.fr>
reading a file to the end regardless of result <nospam@home.com>
Re: reading a file to the end regardless of result (Anno Siegel)
Re: reading a file to the end regardless of result <mark.clementsREMOVETHIS@wanadoo.fr>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 15 Jan 2006 11:12:25 -0500
From: Michael Friendly <friendly@yorku.ca>
Subject: create a hierarchical list from a text file
Message-Id: <dqds9a$is3$1@sunburst.ccs.yorku.ca>
I need to create hierarchical menu files from text input in a simple form
of comma delimited lines with fields (topic, url, description) like
Top1,,Desc1
Item1
Item11,url11,Description11
Item12,url12,Description12
Item13,url12,Description13
Item2,url2,Description2
Item21,url21,Description21
Item3,url3,Description3
Item31,url31,Description31
Item32,url32,Description32
Top2,,Desc2
Item1,url1,Description1
Item11,url11,Description11
Item12,url12,Description12
Item2,url2,Description2
Item21,url21,Description21
where the number of leading tabs in each item implicitly indicates level
in the tree. The output must be in the form of an explicitly structured,
comma-separated list, [ item, item, ... item ] where each item is either
a simple item or another list, e.g.,
[ item, [ item, item, ... item ], ... item ].
Missing fields in an item are represented by 'null', so the example
would appear as:
[
['Top1', null, null
, ['Item1', null, null
, ['Item11', 'url11', 'Description11']
, ['Item12', 'url12', 'Description12']
, ['Item13', 'url12', 'Description13']
]
, ['Item2', 'url2', 'Description2'
, ['Item21', 'url21', 'Description21']
]
, ['Item3', 'url3', 'Description3'
, ['Item31', 'url31', 'Description31']
, ['Item32', 'url32', 'Description32']
]
]
, ['Top2', null, null
, ['Item1', 'url1', 'Description1'
, ['Item11', 'url11', 'Description11']
, ['Item12', 'url12', 'Description12']
]
, ['Item2', 'url2', 'Description2'
, ['Item21', 'url21', 'Description21']
]
]
]
I'm reading the items and creating a list of hashes with the subroutine
below, but I can't figure out how to get from there to my desired
output. There must be some perl modules I can use. Can someone help?
sub read_items {
$file = shift;
open(IN, $file) || die("cant open $file\n");
$nitems = 0;
undef @items;
while( $line = <IN>) {
chomp($line);
next if $line =~ /^#/;
$line =~ s/(\t*)//;
$level = length($1); # level in tree
next unless $line;
$nitems++;
($title, $url, $desc) = split(/,\s*/, $line);
$desc =~ s/\'/\\'/g; # escape quotes in e.g., Murphy's Law
$item = {
level => $level,
title => $title,
url => $url,
desc => $desc,
};
push @items, $item;
}
close IN;
print STDERR "Read $nitems items\n";
return @items;
}
--
Michael Friendly Email: friendly@yorku.ca
Professor, Psychology Dept.
York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT M3J 1P3 CANADA
------------------------------
Date: Sun, 15 Jan 2006 12:01:28 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: mechanize follow_link () on an image
Message-Id: <43ca2b8e$0$21291$8fcfb975@news.wanadoo.fr>
Nospam wrote:
> "Mark Clements" <mark.clementsREMOVETHIS@wanadoo.fr> wrote in message
>> try this:
>>
>> use strict;
>> use warnings;
>>
>> use Data::Dumper;
Note that this line (use Data::Dumper) isn't necessary for the purposes
of this exercise. I had left it in by mistake.
>> use WWW::Mechanize;
>>
>> use constant START => "http://www.warservers.com/f/viewtopic~t~19029.htm";
>>
>> my $mech = WWW::Mechanize->new();
>>
>> $mech->get( START );
>> print $mech->uri."\n";
>> $mech->follow_link( url_regex => qr(posting~mode~reply));
>> print $mech->uri."\n";
>
> thanks the print out follows the link
Not exactly: follow_link follows the link.
print $mech->uri();
merely shows the current url.
> I am wondering if I had a series of urls in a text file (c:\text1.txt"), how
> I would amend use constant START so it goes through each url in the text
> file
You read the file in line by line, assigning input to a variable ,and
passing that to follow_link( url => $url).
What exactly is it you are trying to do?
Mark
------------------------------
Date: Sun, 15 Jan 2006 16:40:31 GMT
From: "Nospam" <nospam@home.com>
Subject: reading a file to the end regardless of result
Message-Id: <3Wuyf.1216$0N1.1002@newsfe5-win.ntli.net>
is there a command to continue with a process even if the last line didn't
have the value you were after?
ie if I was reading each line in a file and placing the output on the
console, and I wanted to continue even if there were some whitespaces what
would I have to amend in this prog:
open(file, "file.txt");
while( )
{
my $filevalue = <file>;
print $filevalue;
}
------------------------------
Date: 15 Jan 2006 16:50:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: reading a file to the end regardless of result
Message-Id: <dqdug3$npe$1@mamenchi.zrz.TU-Berlin.DE>
Nospam <nospam@home.com> wrote in comp.lang.perl.misc:
> is there a command to continue with a process even if the last line didn't
> have the value you were after?
>
> ie if I was reading each line in a file and placing the output on the
> console, and I wanted to continue even if there were some whitespaces what
Whitespace?
> would I have to amend in this prog:
>
> open(file, "file.txt");
>
> while( )
> {
> my $filevalue = <file>;
>
> print $filevalue;
> }
You may want binmode(). Look it up.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Sun, 15 Jan 2006 19:00:03 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: reading a file to the end regardless of result
Message-Id: <43ca8daa$0$19701$8fcfb975@news.wanadoo.fr>
Nospam wrote:
> is there a command to continue with a process even if the last line didn't
> have the value you were after?
>
> ie if I was reading each line in a file and placing the output on the
> console, and I wanted to continue even if there were some whitespaces what
> would I have to amend in this prog:
>
> open(file, "file.txt");
You *need* to check the return value of all your system calls.
It's traditional to use capital letters for filehandles (if you aren't
using lexically scoped variables for them).
my $filename = "file.txt";
open ( FILE, "<$filename")
|| die "could not open file $filename for read: error=$!";
>
> while( )
> {
> my $filevalue = <file>;
>
> print $filevalue;
> }
>
This isn't going to terminate. You (probably) want
while(my $filevalue = <file>){
chomp $filevalue;
print "$filevalue\n";
}
Mark
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8853
***************************************