Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > For Each Loop

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-10-08, 12:20
Librarysystem Librarysystem is offline
Registered User
 
Join Date: Apr 2008
Posts: 9
For Each Loop

HI,
I use the following code, which works, but i only have 7 lines and it prints 34 lines (repeating some). Can you help me figure out what i'm doing wrong? we sort by shelf key 13 but print the call number 14


#!/usr/local/bin/perl -w
$data_file="youthbooksc.txt";
open(DAT, $data_file);

@L=();
%Sortbyshelfkey=();

while(<DAT>)
{
chomp;

@L=split(/\|/, $_);
@{$Sortbyshelfkey{$L[13]}}=@L;

foreach my $f (sort keys %Sortbyshelfkey)
{
print "Call #: $Sortbyshelfkey{$f}[14] \n"; #
}

}
Reply With Quote
  #2 (permalink)  
Old 07-12-08, 01:13
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Fort Polk, LA
Posts: 500
First off, it's printing 74 with reps because you're printing each time you read a line. Look at how your curly braces are nested.

To make your code a little easier to debug, use some lexicals and use strict.

I don't have the actual data, but try these general changes:

Code:
use strict; my $data_file="youthbooksc.txt"; open(my $dat, $data_file); my(%Sortbyshelfkey); while(<$dat>) { chomp; # No reason to declare @L outside the scope that it's actually used. my(@L)=split(/\|/, $_); # This does the same thing your code did but makes it clear that # you're creating a reference to an anonymous array. $Sortbyshelfkey{$L[13]}=[@L]; } foreach my $f (sort keys %Sortbyshelfkey) { print "Call #: $Sortbyshelfkey{$f}->[14] \n"; # And this makes it clear that you're dereferencing that array. }
Reply With Quote
  #3 (permalink)  
Old 07-14-08, 08:46
Librarysystem Librarysystem is offline
Registered User
 
Join Date: Apr 2008
Posts: 9
thanks,
yes, it wasn't so much repeating as starting with 1st line, then 2nd line (with the new one sorted), 3rd line etc.

thats solves it.
-mw
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On